Problem controlling digital pins over Xbee with two Megas

Hi all.

I am trying to set up a wireless controller using 16 buttons on one mega to trigger corresponding pins on a second mega over Xbee.

What I am wanting/expecting to happen is basically the arduino button sketch but wireless.

Pressing a given button on the transmitter to trigger on/off on the receive side pin.

What I am getting is, the pin does seem to turn on when I press the button but it will not turn off again until I hit another button. The first pin turns off, hit button again, that pin turns on....etc..etc.

Basically things only seem to be half working. The pins do seem to turn on, they just wont turn back off on their own. Because of this, I am assuming it is a code issue. I just can't seem to figure out what it is.

Hardware, Arduino Mega on both ends, Xbee series 1 on both ends and standard momentary buttons on the transmitter side.

Transmit code

  int button1 = 23;
  int button2 = 25;
  int button3 = 27;
  int button4 = 29;
  int button5 = 31;
  int button6 = 33;
  int button7 = 35;
  int button8 = 37;
  int button9 = 39;
  int button10 = 41;
  int button11 = 43;
  int button12 = 45;
  int button13 = 47;
  int button14 = 49;
  int button15 = 51;
  int button16 = 53;
  
  int button1state = 0;
  int button2state = 0;
  int button3state = 0;
  int button4state = 0;
  int button5state = 0;
  int button6state = 0;
  int button7state = 0;
  int button8state = 0;
  int button9state = 0;
  int button10state = 0;
  int button11state = 0;
  int button12state = 0;
  int button13state = 0;
  int button14state = 0;
  int button15state = 0;
  int button16state = 0;
  
  int val;
  


void setup() 
{
  //Create Serial Object
  Serial.begin(38400);
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);
  pinMode(button4, INPUT_PULLUP);
  pinMode(button5, INPUT_PULLUP);
  pinMode(button6, INPUT_PULLUP);
  pinMode(button7, INPUT_PULLUP);
  pinMode(button8, INPUT_PULLUP);
  pinMode(button9, INPUT_PULLUP);
  pinMode(button10, INPUT_PULLUP);
  pinMode(button11, INPUT_PULLUP);
  pinMode(button12, INPUT_PULLUP);
  pinMode(button13, INPUT_PULLUP);
  pinMode(button14, INPUT_PULLUP);
  pinMode(button15, INPUT_PULLUP);
  pinMode(button16, INPUT_PULLUP);
    
}

void loop() {
button1state = digitalRead(button1);
button2state = digitalRead(button2);
button3state = digitalRead(button3);
button4state = digitalRead(button4);
button5state = digitalRead(button5);
button6state = digitalRead(button6);
button7state = digitalRead(button7);
button8state = digitalRead(button8);
button9state = digitalRead(button9);
button10state = digitalRead(button10);
button11state = digitalRead(button11);
button12state = digitalRead(button12);
button13state = digitalRead(button13);
button14state = digitalRead(button14);
button15state = digitalRead(button15);
button16state = digitalRead(button16);

    if (button1state == LOW) {
      val = 1;
    } 
    if (button2state == LOW) {
      val = 2;
    }
     if (button3state == LOW) {
      val = 3;
    } 
    if (button4state == LOW) {
      val = 4;
    }
     if (button5state == LOW) {
      val = 5;
    } 
    if (button6state == LOW) {
      val = 6;
    }
     if (button7state == LOW) {
      val = 7;
    } 
      if (button8state == LOW) {
      val = 8;
    }
     if (button9state == LOW) {
      val = 9;
    } 
    if (button10state == LOW) {
      val = 10;
    }
     if (button11state == LOW) {
      val = 11;
    } 
    if (button12state == LOW) {
      val = 12;
    }
     if (button13state == LOW) {
      val = 13;
    } 
    if (button14state == LOW) {
      val = 14;
    }
     if (button15state == LOW) {
      val = 15;
    } 
    if (button16state == LOW) {
      val = 16;
    }    
Serial.write(val);
delay(10);
}

Receive code

  int audio1 = 23;
  int audio2 = 25;
  int audio3 = 27;
  int audio4 = 29;
  int audio5 = 31;
  int audio6 = 33;
  int audio7 = 35;
  int audio8 = 37;
  int audio9 = 39;
  int audio10 = 41;
  int audio11 = 43;
  int audio12 = 45;
  int audio13 = 47;
  int audio14 = 49;
  int audio15 = 51;
  int audio16 = 53;

  int val;

void setup() {
    //Start Serial
  Serial.begin(38400);
  pinMode(audio1, OUTPUT);
  pinMode(audio2, OUTPUT);
  pinMode(audio3, OUTPUT);
  pinMode(audio4, OUTPUT);
  pinMode(audio5, OUTPUT);
  pinMode(audio6, OUTPUT);
  pinMode(audio7, OUTPUT);
  pinMode(audio8, OUTPUT);
  pinMode(audio9, OUTPUT);
  pinMode(audio10, OUTPUT);
  pinMode(audio11, OUTPUT);
  pinMode(audio12, OUTPUT);
  pinMode(audio13, OUTPUT);
  pinMode(audio14, OUTPUT);
  pinMode(audio15, OUTPUT);
  pinMode(audio16, OUTPUT);

digitalWrite(audio1, LOW);
digitalWrite(audio2, LOW);
digitalWrite(audio3, LOW);
digitalWrite(audio4, LOW);
digitalWrite(audio5, LOW);
digitalWrite(audio6, LOW);
digitalWrite(audio7, LOW);
digitalWrite(audio8, LOW);
digitalWrite(audio9, LOW);
digitalWrite(audio10, LOW);
digitalWrite(audio11, LOW);
digitalWrite(audio12, LOW);
digitalWrite(audio13, LOW);
digitalWrite(audio14, LOW);
digitalWrite(audio15, LOW);
digitalWrite(audio16, LOW);


}

void loop() { 
  while(Serial.available()){
    val = Serial.read(); 
  


  if(val == 1){
  digitalWrite(audio1, HIGH);
  }else{ 
  digitalWrite(audio1, LOW);
  }                                 
   if(val == 2){
  digitalWrite(audio2, HIGH);
  }else{
  digitalWrite(audio2, LOW);
   }
   if(val == 3){
  digitalWrite(audio3, HIGH);
  }else{
  digitalWrite(audio3, LOW);
   }
   if(val == 4){
  digitalWrite(audio4, HIGH);
  }else{
  digitalWrite(audio4, LOW);
   }
   if(val == 5){
  digitalWrite(audio5, HIGH);
  }else{
  digitalWrite(audio5, LOW);
   }
   if(val == 6){
  digitalWrite(audio6, HIGH);
  }else{
  digitalWrite(audio6, LOW);
   }
   if(val == 7){
  digitalWrite(audio7, HIGH);
  }else{
  digitalWrite(audio7, LOW);
   }
   if(val == 8){
  digitalWrite(audio8, HIGH);
  }else{
  digitalWrite(audio8, LOW);
   }
   if(val == 9){
  digitalWrite(audio9, HIGH);
  }else{
  digitalWrite(audio9, LOW);
   }
   if(val == 10){
  digitalWrite(audio10, HIGH);
  }else{
  digitalWrite(audio10, LOW);
   }
   if(val == 11){
  digitalWrite(audio11, HIGH);
  }else{
  digitalWrite(audio11, LOW);
   }
   if(val == 12){
  digitalWrite(audio12, HIGH);
  }else{
  digitalWrite(audio12, LOW);
   }
   if(val == 13){
  digitalWrite(audio13, HIGH);
  }else{
  digitalWrite(audio13, LOW);
   }
   if(val == 14){
  digitalWrite(audio14, HIGH);
  }else{
  digitalWrite(audio14, LOW);
   }
   if(val == 15){
  digitalWrite(audio15, HIGH);
  }else{
  digitalWrite(audio15, LOW);
   }
   if(val == 16){
  digitalWrite(audio16, HIGH);
  }else{
  digitalWrite(audio16, LOW);
   }

 }
}

I would greatly appreciate any thoughts and suggestions.

Tahnks Rick

@ PakRatJR:

Hi,

First off, may I know why the very uncommon Xbee baudrate? Are you the one that set that baudrate?

Second if you want to know if its the Xbee the problem try hooking up the Serial pin using wired first then when the functionality had been finalise then you add the Xbee later on .

Ashraf

Try adding else if () instead of just if () for buttons 2-16. Also, try disconnecting the Xbee and use your own serial monitor for the board and see if readings are even being printed.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. As you have control of the code at both ends I suggest you use the system in the 3rd example.

You could enormously shorten the code to act on the received values something like this

for (byte n = 0; n < numPins; n++) {
   digitalWrite(pinNum[n], LOW);
}
digitalWrite(pinNum[val], HIGH);

…R

Thanks for the replys.

Ashraf_Zolkopli:
@ PakRatJR:

Hi,

First off, may I know why the very uncommon Xbee baudrate? Are you the one that set that baudrate?

Second if you want to know if its the Xbee the problem try hooking up the Serial pin using wired first then when the functionality had been finalise then you add the Xbee later on .

Ashraf

The transmit side is modeled off a roughly similar setup someone else had done. The baudrate is what he was using and seemed to work.

I gave the wired setup a try and wasn’t actually able to get it to work.

chummer1010:
Try adding else if () instead of just if () for buttons 2-16. Also, try disconnecting the Xbee and use your own serial monitor for the board and see if readings are even being printed.

Gave this a try first as it seemed to be the easiest place to start. Unfortunately I was not able to get the code to compile. Error ‘else’ without a previous ‘if’

 if(val == 1){
  digitalWrite(audio1, HIGH);
  }else{ 
  digitalWrite(audio1, LOW);
  }                                 
  else if(val == 2){
  digitalWrite(audio2, HIGH);
  }else{
  digitalWrite(audio2, LOW);
   }
  else if(val == 3){
  digitalWrite(audio3, HIGH);
  }else{
  digitalWrite(audio3, LOW);
   }
  else if(val == 4){
  digitalWrite(audio4, HIGH);
  }else{
  digitalWrite(audio4, LOW);
   }
  else if(val == 5){
  digitalWrite(audio5, HIGH);
  }else{
  digitalWrite(audio5, LOW);
   }
  else if(val == 6){
  digitalWrite(audio6, HIGH);
  }else{
  digitalWrite(audio6, LOW);
   }

The readings are being printed. Starts out zero. When a hit a pin, the corresponding number will come up on the monitor…(button1 “1”, button2 “2” ect. but it never goes back to zero.

Robin2:
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. As you have control of the code at both ends I suggest you use the system in the 3rd example.

You could enormously shorten the code to act on the received values something like this

for (byte n = 0; n < numPins; n++) {

digitalWrite(pinNum[n], LOW);
}
digitalWrite(pinNum[val], HIGH);




...R

I am still looking at this. I think I understand what this is doing. I am learning as I go with this and anyway I can find to make things smaller would be nice. This is just one part of the overall project. I still have some LEDs and a motor to add in yet. :o lol

I am kinda starting to wonder if my boards are actually working right also. For some reason I can’t seem to get things to work consistently right now eather.

What I am trying next is I will plug the Rx Xbee into the computer and see if XCTU will read things.

Quick edit: With Rx Xbee on the computer, XCTU is showing the sent data as well. Same as the arduino serial monitor.