How to use array elements in if condition?

Hi,

I am usind arduino as receiver for serial communication. I can handle receive data with arra, then i look array elements.

I wanted to compare array element in if condititions. When i got some value in array, i want to check next one and print msg.

I can get first valus successfully but next one is successfull when i increase the array.

char RFin_bytes[20];
void setup()
{
  Serial.begin(9600);
}

void loop()
{

#define MAX_MILLIS_TO_WAIT 2000  //or whatever
unsigned long starttime;

starttime = millis();

while ( (Serial.available()<20) && ((millis() - starttime) < MAX_MILLIS_TO_WAIT) )
{      
     // hang in this loop until we either get 20 bytes of data or 1 second
     // has gone by
}
if(Serial.available() < 20)
 {
           // the data didn't come in - handle that problem here
           // Serial.println("ERROR - Didn't get 20 bytes of data!");
 }
  else
    {
     
       for(int n=0; n<20; n++) {
          RFin_bytes[n] = Serial.read(); // Then: Get them. 
          Serial.print(RFin_bytes[n], DEC);
          Serial.print(" | ");
         
           [quote author=AWOL link=msg=3031955 date=1481051118]
[code]RFin_bytes[n++]

Now there’s a potentially dangerous thing to do. What if the first condition in the if is false?
[/quote]

I made a mistake when i post.

if (RFin_bytes[n] == 4 && RFin_bytes[n++] == 49) {
Serial.println(“Notice”);
}

}
Serial.println(" Done ");
}

}[/code]

Incaming message exampe;
byte message[] = {0x7e, 0x00, 0x12, 0x92, 0x00, 0x13, 0xa2, 0x00, 0x41, 0x47, 0x9e, 0x5a, 0x8d, 0x56, 0x01, 0x01, 0x00, 0x1e, 0x00, 0x00, 0x04, 0x31};

RFin_bytes[n++]

Now there's a potentially dangerous thing to do. What if the first condition in the if is false?

AWOL:

RFin_bytes[n++]

Now there's a potentially dangerous thing to do. What if the first condition in the if is false?

I made a mistake when i post. How can i use ?

if (RFin_bytes[n] == 4 && RFin_bytes[n++] == 49)

I'm sorry, I don't understand the question

AWOL:
I'm sorry, I don't understand the question

I want to check 2 byte data from serial port, i can get first value successfully, but when i try to combine increase in if condition not working.

Are you trying to test the following condition? (compare it to your version)

if (RFin_bytes[n] == 4 && RFin_bytes[n + 1] == 49) {

AWOL:
Are you trying to test the following condition? (compare it to your version)

if (RFin_bytes[n] == 4 && RFin_bytes[n + 1] == 49) {

Yes i tried, still same.

if(Serial.available() < 20)

Is dangerous too - are you sure the serial input buffer is that big? Why not read the bytes as they come and
process your buffer, which you have declared appropriately large, when its full?

@OP: You've now edited your original post, and now nothing here makes sense.
Well done.
I'm out.

AWOL:
@OP: You’ve now edited your original post, and now nothing here makes sense.
Well done.
I’m out.

I ve only edited if condition, thx.

MarkT:

if(Serial.available() < 20)

Is dangerous too - are you sure the serial input buffer is that big? Why not read the bytes as they come and
process your buffer, which you have declared appropriately large, when its full?

Serial sender device, sends 20 byte data every 2 seconds. I want to compare two incoming valu and print text.

samilbeden:
I ve only edited if condition, thx.

Read it again.

So what - your code is expecting there to be at least a 20 byte serial input buffer, which frankly I've
no idea if that's true on the various different Arduinos. What if its 16 bytes in a future release say,
or you move to a ATtiny?

You have a buffer you know is big enough, so read in to it whenever something is available, and
then you don't have to worry about overflowing the system buffer. There's even a built-in
way to do this using serialEvent() to help.

MarkT:
So what - your code is expecting there to be at least a 20 byte serial input buffer, which frankly I’ve
no idea if that’s true on the various different Arduinos. What if its 16 bytes in a future release say,
or you move to a ATtiny?

You have a buffer you know is big enough, so read in to it whenever something is available, and
then you don’t have to worry about overflowing the system buffer. There’s even a built-in
way to do this using serialEvent() to help.

Ok, i try to ask clear. Sender device sends 20 bytes o data when button pressed.

I want to check serial data and if comes two values (ex: 0x04 0x31).

Now i am using nodemcu board.