if statements and bi-directional bytes

I am having programming difficulty with trying to send bytes from the Arduino when certain bytes are received over serial. For example:

  • When byte 254 is received send 5 hard coded bytes

The code looks like:

void loop() {
  if (Serial.available() > 0)  // Wait for serial input 
    // Read the first byte
    startbyte = Serial.read();
    // If it's really the startbyte (254) ...
    if (startbyte == 254)  {
      Serial.write(254);
      Serial.write(123);
      Serial.write(123);
      Serial.write(1); 
      Serial.write(255);}

When I run the code and look at the console I get -1 being sent instead of the hard coded bytes. Any idea why (or better yet how to write the code properly)?

Try:

Serial.print(254, BYTE);

8-Dale

I tried that as well, but it didn't work. The Serial.write command will print in byte format and I have had success with it before. I think the If statement is off since I can print the bytes without the if statement (of course I need the if statement so I can respond when receiving the specific byte only).

Reading the documentation on the "Serial" objects "read" message (it's a C++ object) I see that "read" returns -1 if nothing was read, else, it returns the character read.

This implies, to me at least, that the "serial" object only handles ASCII values. ASCII values are 7-bits representing the values 0-127 thus you'll likely never receive the value 254.

Thanks, that looks like a good find. I will see if changing the code to reading 0 will make a difference.

This implies, to me at least, that the "serial" object only handles ASCII values.

This implies nothing of the sort.
The return type of "Serial.read" is "int".
If the serial receive buffer is empty -1 (0xFFFF) is returned, otherwise, the least-significant eight bits contain the least-recent byte received.
If that byte was 254, then 0x00FE is returned.

You should perhaps be looking at what is being transmitted.

I was about to say that after looking at the source for the 'Serial' object I now no longer think the return value, an 'int', is limited to 7-bits. But someone has chastised me already before this post.

Your code fragment doesn't seem to show the declaration of 'startbyte'. What is it declared as?

Here is the whole code with the declaration. I also tried byte startbyte rather than int startbyte, but it didn't make a difference.

byte startbyte;       // start byte, begin reading input


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

void loop() {
    if (Serial.available() > 0)  // Wait for serial input
    // Read the first byte
       startbyte = Serial.read();
    // If it's really the startbyte (254) ...
     if (startbyte == 254)  {
      Serial.write(254);
      Serial.write(123);
      Serial.write(123);
      Serial.write(1); 
      Serial.write(255); }
    if (startbyte == 255) {
      Serial.write (255);
  }
}

Well, I finally made it work by giving the Arduino 2 full seconds before receiving the first startbyte (I tried 100 milliseconds, but it didn't work). So it turns out it was a timing issue. I wasn't able to cycle through the second if statement for some reason, but that probably has to do with syntax such as using "else". And the 254 worked fine. Thank you for everyone's help!

I think in your definition of startbyte you want to initialize it. I'm not sure there's a guarantee that it isn't born with a value 254;

If startbyte has global or static scope, it will be initialised to zero.