This seems so simple...? XBee code question

Hello,

This seems like a simple thing. I have two XBees, two XBee explorers and two Arduino Unos. When a button is pushed, connecting pin 8 to ground, that XBee is supposed to send "333" to the other. I'm a newbie, can anyone check over the code?

/*
When a button is pushed, send "333" from one XBee to the other
*/

#define SENSOR_PIN 8
//Pins 0/1 are used for serial.

void setup(){
    pinMode(SENSOR_PIN, INPUT);
        digitalWrite(SENSOR_PIN, HIGH); //turn on internal 20 k pullup resistor so the open input state is HIGH.
        Serial.begin(9600);
}

void loop(){
    if(digitalRead(SENSOR_PIN == LOW)) //read switch input
        Serial.print("333");
}

Your code should be like this...

    if(digitalRead(SENSOR_PIN) == LOW) //read switch input

Hi,

When I tried it that way, I got this error:

BRAIN:14: error: expected `)' before 'Serial'

When I enter it the previous way, with the double bracket, I don't get the error message.

if(digitalRead(SENSOR_PIN == LOW)) //read switch input

Then you are doing something else wrong.

The code posted by bubulindo is both valid and the correct way to perform the if-statement. When you added the ")" after SENSOR_PIN, did you remember to remove the ")" from the end of the line?

Your code of:

if(digitalRead(SENSOR_PIN == LOW))

evaluates to:

if (digitalRead(8 == 0)) if (digitalRead(FALSE)) which is also: if (digitalRead(0))

In C the comparison occurs first, then that is evaluated to a 0, meaning if the code actually runs it is probably reading Pin 0, not Pin 8.

Hi,

Thank you very much for taking the time to reply. I do really appreciate it. When I use the code below, it does work, but it just sends a steady stream of 33333333333 etc. So there is definitely something else wrong then.

/*
When a button is pushed, send "333" from one XBee to the other
*/

#define SENSOR_PIN 8
//Pins 0/1 are used for serial.

void setup(){
    pinMode(SENSOR_PIN, INPUT);
        digitalWrite(SENSOR_PIN, HIGH); //turn on internal 20 k pullup resistor so the open input state is HIGH.
        Serial.begin(9600);
}

void loop(){
    if(digitalRead(SENSOR_PIN == LOW)) //read switch input
        Serial.print("333");
}

So there is definitely something else wrong then.

Clearly there is a difference between the program behavior and your expectations. It could be your expectations that are wrong.

If you reread the replies above, you will see that you were asked to explain how your switch is wired.

You might also look at your code, and see that is does nothing if the switch is one state (whether that is pressed or released depends on how your switch is wired) and something if the switch is in the other state.

Making the program do something, even of that something is as simple as printing 222 in the other state, will tell you if the program is correctly reading the switch state.

arduinokov: Thank you very much for taking the time to reply. I do really appreciate it. When I use the code below, it does work, but it just sends a steady stream of 33333333333 etc. So there is definitely something else wrong then.

Well that code is still incorrect. Your if-statement continues to be wrong: if(digitalRead(SENSOR_PIN == LOW)) //read switch input it needs to read if (digitalRead(SENSOR_PIN) == LOW) //read switch input

Why have you not fixed this yet?

Even when corrected, as long as the switch is low the correct statement will be true and 333 will continue to print. That is what you have told the code to do.

When I use the code below, it does work, but it just sends a steady stream of 33333333333 etc. So there is definitely something else wrong then.

if(digitalRead(SENSOR_PIN == LOW)) //read switch input
        Serial.print("333");

Dissect the code. SENSOR PIN has the value 8. Therefore, "SENSOR_PIN == LOW" is false (8 != 0). false is the same as zero, so if(digitalRead(0)) is not zero, print "333". What is connected to digital pin 0?

When I enter it the previous way, with the double bracket, I don't get the error message.

if(digitalRead(SENSOR_PIN == LOW)) //read switch input

Lots of things that compile (i.e. they are syntactically correct), may still not be what was intended i.e. semantically correct.

Thanks for the help everyone. I will study all the replies and see if I can get it to work. I appreciate the opportunity to learn.

:slight_smile:

Hi,

I think I have it sorted out. There seems to be one small syntax error I can't find.

/*When a button is pushed, send "333" from one XBee to the other
*/

int SENSOR_PIN = 8;   // base station trigger

void setup(){
        Serial.begin(9600); // opens serial port, sets data rate to 9600 bps    
        pinMode(SENSOR_PIN, INPUT);
        digitalWrite(SENSOR_PIN, HIGH); //turn on internal 20 k pullup resistor so the open input state is HIGH.
        
}

void loop(){
    if(digitalRead(SENSOR_PIN == LOW) // pin was triggered
        Serial.println("333");

}

:blush:

    if(digitalRead(SENSOR_PIN == LOW) // pin was triggered

Count the number of open and close parentheses. They need to match.

...and they need to be in the right place.

Hi,

I have counted and I can't find it. Newbie blindness. :(

if(digitalRead(SENSOR_PIN == LOW) // pin was triggered
There are 2 open parentheses - one after the if keyword and one after the digitalRead function name. There is one close parentheses, at the end.

It should be:
if(digitalRead(SENSOR_PIN) == LOW)

Hi,

The whole sketch works perfectly now. I can’t believe I missed the one “)”. And I studied a whole bunch of examples! These little things are sometimes so hard for a newbie. Thanks so much for taking the time to point it out so clearly. It’s a great learning experience.

Thanks!!!
:smiley: