Go Down

Topic: Serial Programming question (Read 1 time) previous topic - next topic

Now I am trying to read 4bytes of data coming from the ATMega32.
1-sync,2-t,3-h,4-f
I inserted that while loop after every Serial1.read()
Code: [Select]

   if(Serial1.available()>0)
  {
    sync=Serial1.read();
    //delay(500);
   
    if(sync==0xAA)
    {
      Serial.print("Sensor reading \n");
      while(!Serial1.available())
      digitalWrite(13,HIGH);
      //if(Serial1.read()==B00011010) //Address 1=B00011010
      //{
        while(!Serial1.available());
        temp=Serial1.read();
        while(!Serial1.available());
        hu=Serial1.read();
        while(!Serial1.available());
        fcs=Serial1.read();
        digitalWrite(13,LOW);
        if((temp+hu)==fcs)
        {
          Serial.print("Temperature 1 \t");
          Serial.print(temp);
          Serial.print("Humidity 1 \t\n");
          Serial.print(hu);
        }
        else
        Serial.print("Sensor1 Error\n");
      //}
     }
   }


It is very slow(not main problem as the sensor updates the data once every second, but still the data received is lot slower and jitter is also high ) and the error rate is too high. Is there any programming improvement to reduce the errors and jitter(speed is secondary)

PaulS

Code: [Select]
        while(!Serial1.available());
Serial.available() is not a boolean function. Do not use it like one!

#17
Dec 08, 2012, 12:27 am Last Edit: Dec 08, 2012, 12:30 am by cyberhawk9 Reason: 1

Code: [Select]
        while(!Serial1.available());
Serial.available() is not a boolean function. Do not use it like one!

For now it is serving the purpose.
Could you suggest a better alternate instruction to make the arduino wait until the the byte of data is available.

PaulS

Quote
Could you suggest a better alternate instruction to make the arduino wait until the the byte of data is available.

Yes. The function returns the number of bytes available to read, NOT just whether there is data to read. If you want to wait for one byte, say so:
Code: [Select]
while(Serial.available() < 1) { // Do nothing }

AWOL

Quote
Serial.available() is not a boolean function. Do not use it like one!

The function returns zero (aka false) or not zero (aka true).
In C, that's as good as boolean. It just about embodies the essence, the spirit of C.

Quote
Do not use it like one!

Could be read as "Do not use it like one. Not"    ;)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Quote
In C, that's as good as boolean. It just about embodies the essence, the spirit of C.

I still think that it is better to be clear in the code what you are waiting for. Changing my example to wait for 4 characters, instead, would be trivial. Changing OPs code to wait for 4 characters is much more involved.

I will change that.
Are there any suggestions to reduce the jitter and errors through programming?

PaulS

Quote
Are there any suggestions to reduce the jitter and errors through programming?

No. Jitters and error are caused either by the sender getting incorrect data to send, or by interference between the two Arduinos due to excessive distance/environmental factors.

You still haven't answered the question about why you need two Arduinos, nor have you described how far apart they are or what kind of environment they are operating in.

Go Up