I am trying to write one code that takes data from serial
ie. 0x4E, 0x20, 0x--, 0x0D.
0x-- may be any hex in --
The user will input these hex one by one and raspi will take all of them in buffer. Whenever 1st, 2nd and 4th data is matched, the 3rd one will be printed in serial ie.
Serial.print(3rd); //Decimal value
else Serial.print() // may be 00
I was trying with this code:
byte buffer[4];
int cnt = 0;
void setup()
{
Serial.begin(9600);
Serial.println("Start");
}
void loop(void)
{
// read max 12 bytes
if ((cnt <= 4) && Serial.available()) buffer[cnt++] = Serial.read();
if (cnt == 4) // all bytes read...
{
// assume the arrays are the same
boolean same = true;
// the ans array holds the right answer
byte ans[]={0x4E,0x20,0x01,0x0D};
for (int i=0; (i<= 4) && same; i++) same = (buffer[i] == ans[i]);
if (same)
{
Serial.println("Yes");
} else {
Serial.println("Enter another number");
}
Serial.flush();
cnt = 0;
}
//Serial.print("Done");
// do something
}
But I was not able to do it. Anyone have a suggestion?