Graham Hepworð
Graham Hepworð

Reputation: 53

Why is C printing outputs late?

I am currently running a piece of C code on my Raspberry Pi computer. It is a random number generator that reads from a Geiger counter connected to GPIO digital input 18. It makes randomized bits(see code) and prints the bits in sets of 8. Also, every 30 seconds, it prints the current level of observed radiation. The code seems to be working fine, except when the radiation source is taken away. The random numbers are generated less quickly, but it also seems to slow down the rest of the tasks. The message printed at the beginning of the program does not show up until one random number is generated. When this happens, no number shows, but there is a newline added without a number. Even while the program is running, the level of radiation appears to print every 30 seconds, but also on the next random number. Why is C executing the code in the wrong order?

#include <wiringPi.h>//library for I/O
#include <stdlib.h>
int main(int argc, char *argv[])
{
    int lastRead;//if this is first time observing current pulse
    int pulseCount = 0;//number of total pulses from Geiger counter
    long timing[4] = {0,0,0,0};//values to compare to produce one bit
    int bits[8] = {0,0,0,0,0,0,0,0};//the newest number
    int bitCount = 0;//counts how many random bits have been made
    int i = 0;
    float startTime = 0;//start of the clock
    float currentSec = 0;
    float currentMin = 0;
    float cpm = 0;
    long elapsedTime = 0;

    wiringPiSetupGpio();//establish physical connection
    pinMode(18, INPUT);//set pin 18 to be input
    printf("random\tradiation");

    while(1)
    {
        if( millis()-startTime >= 30000)//30 sec passed?
        {
            startTime = millis();
            currentSec = startTime/1000;
            currentMin = currentSec/60;
            cpm = pulseCount/currentMin;//calculate counts/min in several steps
            printf("\t%f", cpm);//output counts/min
        }
        if( digitalRead(18) == HIGH )//pin is reading high
        {
            if(lastRead==0)//is not reading the same pulse again
            {
                lastRead = 1;//pulse has been identified
                timing[pulseCount%4] = millis();//save the time
                pulseCount++;//pulse detected

                if( pulseCount%4 == 0 )//if times have been collected
                {
                    if( timing[1]-timing[0] > timing[3]-timing[2] )//make a random bit
                    {
                        bits[bitCount%8] = 0;//nth bit of set of 8 is 0
                    }
                    else {
                        bits[bitCount%8] = 1;//it is one
                    }
                    bitCount++;//note that a bit was added

                    if( bitCount%8 == 0 )//every 8 bits made
                    {
                        printf("\n");

                        for( i = 0; i < 8; i++)//print them on a new line
                        {
                            printf("%d", bits[i]);
                        }//for
                    }//if(bitCount%8==0)
                }//if(pulseCount%4==0)
            }//if(lastRead==0)
        }//if(digitalRead(18)==TRUE)
        else {
            lastRead = 0;//ready to read new pulse
        }//else
    }//while
}//main()

Upvotes: 2

Views: 121

Answers (1)

Barmar
Barmar

Reputation: 780994

By default, output on stdout is line-buffered when it's writing to a terminal. This means that the output will be held in memory until you print a newline or call fflush(stdout) (or the output buffer fills up -- that's typically 4K or 8K characters).

So put fflush(stdout) at the places where you want the accumulated output to be displayed. Or use setbuf(stdout, NULL) to disable buffering entirely.

Upvotes: 9

Related Questions