Pages: [1]   Go Down
Author Topic: if statements and bi-directional bytes  (Read 1236 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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

Beaverton, OR
Offline Offline
Newbie
*
Karma: 0
Posts: 25
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try:

Serial.print(254, BYTE);

8-Dale
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Per Arduino ad Astra

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: June 04, 2010, 01:52:52 am by lloyddean » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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);
  }
}
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Minnesota USA
Offline Offline
Sr. Member
****
Karma: 1
Posts: 323
Made it mahself outta sand 'n wahr.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

If startbyte has global or static scope, it will be initialised to zero.
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.

Pages: [1]   Go Up
Jump to: