Serial Programming question

I used "\code" instead of "/code".

  if(Serial1.available())
  {
    sync=Serial1.read();
    //Serial.print(sync);
    if(sync==0xaa)
    {
      if(Serial1.available())
      {
        data=Serial1.read();

I can't see how that code can ever work. As soon as a character is available, you check to see if it is the sync byte. If it is, you check immediately whether another character is available. It won't be, you are sending data at 300 baud so it will take around 33ms for the next character to arrive. You need to wait until each character is available. You'll also need some way of knowing when you have reached the end of the message, so you can go back to waiting for the sync byte.

    if(sync==0xaa)
a
A
a
B
a
C
a
D
a
A

Are you aware that 'a' is not the same as 0xAA?

yes I am aware. 0xaa is not ASCII 'a'. I tried all the possibilities of using 'a', 0xaa, other formats, on both the ends(atmega and arduino as sync bit).

dc42:

  if(Serial1.available())

{
    sync=Serial1.read();
    //Serial.print(sync);
    if(sync==0xaa)
    {
      if(Serial1.available())
      {
        data=Serial1.read();




I can't see how that code can ever work. As soon as a character is available, you check to see if it is the sync byte. If it is, you check immediately whether another character is available. It won't be, you are sending data at 300 baud so it will take around 33ms for the next character to arrive. You need to wait until each character is available. You'll also need some way of knowing when you have reached the end of the message, so you can go back to waiting for the sync byte.

I thought inserting
if(Serial1.available()) before data=Serial1.read() would tell arduino to wait until the next byte is available.

cyberhawk9:
I thought inserting
if(Serial1.available()) before data=Serial1.read() would tell arduino to wait until the next byte is available.

You seem to be under the impression that Serial communication is fast; It's not. The second

if(Serial1.available())

will happen eons before the next serial byte is received. You either need to make it blocking, implement a state machine with some sort of end of packet marker and a buffer to store all of the data, or check for more than one byte is available the first time.

Arrch:
You seem to be under the impression that Serial communication is fast; It's not. The second

if(Serial1.available())

will happen eons before the next serial byte is received. You either need to make it blocking, implement a state machine with some sort of end of packet marker and a buffer to store all of the data, or check for more than one byte is available the first time.

So how do I make it wait till the byte after the sync byte arrives?

By looping until data is available.

AWOL:
By looping until data is available.

  if(Serial1.available())
  {
    sync=Serial1.read();
    //Serial.print(sync);
    if(sync==0x02)
    {
      while(~Serial1.available());
      data=Serial1.read();
      //fcs=Serial1.read();
      Serial.print(data);
      
    //if((data+fcs)==0)
    //{
     // Serial.print(data);
    //}
    }
    
  }

doesn't solve the problem

BITNOT Serial.available()
Is going to be true a lot of the time.
Try logical NOT.

cyberhawk9:
doesn't solve the problem

How about some debug prints?

if(Serial1.available())
  {
    sync=Serial1.read();
    if(sync==0x02)
    {
      ...
    }
    else
    {
      Serial.print("The byte I received was not a sync byte: ");
      Serial.println(sync);
    }
  }

It's also worth noting that Serial.available() doesn't return a boolean, it returns a number of bytes in the buffer. The proper way to write it is:

if (Serial.available() > 0)

Arrch:
How about some debug prints?

if(Serial1.available())

{
    sync=Serial1.read();
    if(sync==0x02)
    {
      ...
    }
    else
    {
      Serial.print("The byte I received was not a sync byte: ");
      Serial.println(sync);
    }
  }




It's also worth noting that Serial.available() doesn't return a boolean, it returns a number of bytes in the buffer. The proper way to write it is:



if (Serial.available() > 0)

I will remember those pointers next time. I was using LED on 13 for debugging. I will use debug prints next time.

For now this solved the problem

void setup()
{
  Serial.begin(9600);
  Serial1.begin(300);
}

void loop()
{
  int sync;
  char data,fcs;
  if(Serial1.available())
  {
    sync=Serial1.read();
    //Serial.print(sync);
    if(sync==0xAA)
    {
      while(!Serial1.available());
      data=Serial1.read();
      //fcs=Serial1.read();
      Serial.print(data);
      
    //if((data+fcs)==0)
    //{
     // Serial.print(data);
    //}
    }
    
  }
}

Thank you. I have been struggling with this problem for many hours now. Seeking help here got it solved in minutes

Now I will go on to test the RF receiver and transmitter module

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()

   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)

        while(!Serial1.available());

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

PaulS:

        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.

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:

while(Serial.available() < 1) { // Do nothing }

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.

Do not use it like one!

Could be read as "Do not use it like one. Not" :wink:

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?

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.