Pages: 1 [2]   Go Down
Author Topic: Serial Programming question  (Read 1317 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
   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)
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50452
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
        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.
« Last Edit: December 07, 2012, 06:30:03 pm by cyberhawk9 » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50452
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
while(Serial.available() < 1) { // Do nothing }
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26525
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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"    smiley-wink
Logged

"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.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50452
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50452
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: 1 [2]   Go Up
Jump to: