Master sending to Slave Robot when it shouldn't be?

Here’s the set up:

  1. breadboard attached to an UNO, which acts (or will) to control a slave robot by pushing buttons. Pretty simple. (code attached at end)

  2. slave robot, Leonardo, wireless shield, motorshield (code attached under the master’s)

Both using Xbee S1

The code on the slave works by receiving input from the master. When master pushes button 1, a blue LED lights on slave. When master pushes button 2, slave goes into autonomous mode and 2 things occur: (1) yellow LED blinks (2) motors drive forward. When button 2 is pressed again, blinking yellow LED turns off, as autonomous mode has ended. (yes - motor does NOT turn off, but that can be fixed later - not really an issue for here.)

Problem:
Ok, I’m not sure if this is a huge deal, because what I described above DOES work. However, I noticed that when it is in autonomous mode, the “TX” is rapidly blinking on the master, and the “RX” is blinking rapidly on the slave. When I pressed button 2 again to end that part of the loop, this blinking stops. (this would be more of a battery issue i think)

I wrote to the slave’s serial monitor, and apparently, the slave is receiving the letter “S” alot (even tho it is not doing anything with it). Master is not sending anything on the serial monitor, as far as I can tell.

Question:
Why is this occurring? I would think that there should not be any send / receive in this part of the code at all (once initiated). Like I said it does turn off when I switch out of the autonomous part of the loop.

Note: When I take out the motor parts, and only use the LEDs, there is no TX/RX blinking as described in the autonomous part, which further confuses me.

What I tried:
On the master, I tried putting the line “value2 = LOW;” after the “delay(200);” part of the code after button 2 was pushed, but that seemed to have no effect on the TX/RX issue with the motors as described.

Any ideas?

Master Code:

/*
 *  Master Xbee Remote
 *  When button1 is pressed on master's circuit, master's LED will be lit
      When button is pressed, it will also send data to slave Xbee to light it's blue LED
      
 *  When button2 is pushed, it will send command to slave to blink it's yellow LED
      and drive it's motors forward.  When button is pushed again, yellow LED turns OFF.
 */
 
int ledPin = 12;      // LED is connected to pin 12, STATUS

int button1 = 2;       // FORWARD  button
int button2 = 8;      //  BACKWARD button

int value1;            // variable for reading the pin status, FORWARD
int value2;           // variable for reading the pin status, BACKWARD

void setup() {
  
  Serial.begin(9600);
  
  pinMode(ledPin, OUTPUT);    // Set the LED pin as output

  pinMode(button1, INPUT);    // Set the button as input, FORWARD
  pinMode(button2, INPUT);    // Set the button as input, BACKWARD
}

void loop(){
   
  value1 = digitalRead(button1);   // FORWARD, read input value and store it in val
  value2 = digitalRead(button2);  //  BACKWARD, read input value and store it in val  
 
  if (value1 == HIGH) // FORWARD
    {               
      digitalWrite(ledPin, HIGH);   // turn LED on
      Serial.print('F');  // sends what's between the (' ') to partner xbee
      delay(20);
    }
  
        else if (value2 == HIGH) // select button
         {
           digitalWrite(ledPin, HIGH);   // turn LED on
           Serial.print('S');
           delay(200);  /* increased time so only 1 instance of 'S' is
                           sent to slave when button is pressed. A short
                           delay time showed that multiple instances were sent
                        */
                        
        // value2 = LOW; // seems to have no effect?????            
       }
     
  else // (value == LOW)
    {     
      
      digitalWrite(ledPin, LOW);    // turn LED off
//      Serial.print('L');  // sends what's between the (' ') to partner xbee
     delay(1);
    }
  
    
 }

Slave code:

/*****

* When button 1 from master RC is pushed:
  >> blue LED will light while pushed
  
* When button 2 from master RC is pushed:
  >> slave robot will enter autonomous mode, and do this:
     --> blink yellow LED
     --> drive motor forward
     
* When button 2 is pressed again:
   >> autonomous mode will be turned off
      --> yellow LED will stop blinking

*****/

#include <AFMotor.h>  // AF Motor shield library

// Motor setup
const int speed = 100; // percent of maximum speed

int pwm;

// define wheels (individual)

  // front wheels
  AF_DCMotor Motor_Left_Front(4, MOTOR34_1KHZ); // motor 4
  AF_DCMotor Motor_Right_Front(3, MOTOR34_1KHZ); // motor 3

  // rear wheels
  AF_DCMotor Motor_Left_Rear(1, MOTOR12_64KHZ); // motor 1
  AF_DCMotor Motor_Right_Rear(2, MOTOR12_64KHZ); // motor 2
  
/***********************************
            MOTOR FUNCTIONS 
***********************************/
      
// Forward, Backward, Release
void Drive_State(int DRIVE_STATE)
{
  
  if (DRIVE_STATE == FORWARD)
    {Serial1.println("FORWARD");}
    
    else if (DRIVE_STATE == BACKWARD)
      {Serial1.println("BACKWARD");}

    else  
      {Serial1.println("RELEASE");}

  
      // front wheels
      Motor_Left_Front.run(DRIVE_STATE);
      Motor_Right_Front.run(DRIVE_STATE);
      
      // rear wheels
      Motor_Left_Rear.run(DRIVE_STATE);
      Motor_Right_Rear.run(DRIVE_STATE);
  
      delay(10);
}
  
  

int ledPin = 23;      // LED is connected to pin 23, STATUS
int ledPin2 = 22;     // yellow LED, flickering LED

bool autonomous_code = false;  // default
int auto_code = -1;          // default

int incomingByte;      // a variable to read incoming serial data into

void setup() 
{
  Serial1.begin(9600);           // set up Serial library at 9600 bps

pinMode(ledPin, OUTPUT);    // Set the LED pin as output
pinMode(ledPin2, OUTPUT); 

//MOTOR STUFF
// scale percent into pwm range
pwm = map(speed, 0, 100, 0, 255);  // takes range of 0-255 and maps it to 0-100 for variable 'speed'
                                   // actual pwm value is define as const at beginning of code
                                   
// front wheels
Motor_Left_Front.setSpeed(pwm);
Motor_Right_Front.setSpeed(pwm);

// rear wheels
Motor_Left_Rear.setSpeed(pwm);
Motor_Right_Rear.setSpeed(pwm);

// make sure motors are not triggered at start
Drive_State(RELEASE);    // stop

}

void loop() 
{
Serial.println(incomingByte);

   if (Serial1.available() > 0)   // see if there's incoming serial data:
    {
   //   Serial.println(incomingByte);
      
      
      incomingByte = Serial1.read(); // read the oldest byte in the serial buffer:
   //   Serial.println("0");
      
        if (incomingByte == 'F')  // test button
          {
           digitalWrite(ledPin, HIGH);   // turn LED on          
           delay(10);
           digitalWrite(ledPin, LOW);   // turn LED on
     //      Serial.println("1")   ;       
            
          } 
          
        else if (incomingByte == 'S')  // autonomous mode button. toggle flickering LED
          {
           // digitalWrite(ledPin, HIGH);   // turn LED on
            auto_code = (auto_code * (-1));
        //    Serial.println("2");
                            
              if (auto_code > 0)  // turn autonomous code ON
        	{
                 autonomous_code = true;     // return to autonomous code
                 //digitalWrite(ledPin2, HIGH);   // turn LED on
          //       Serial.println("3");
                 }
                 
               else
                 {
                   autonomous_code = false; 
                   digitalWrite(ledPin2, LOW);    // turn LED off
              //     Serial.println("4");
                 }
                 
                 
          }
            
     }

else         
        {
      //      digitalWrite(ledPin, LOW);    // turn LED off
     //       digitalWrite(ledPin2, LOW);    // turn LED off
        }
      
if (autonomous_code == true)  // do autonomous code
{
    //MOTORS
    Drive_State(FORWARD);    // forward
    
   //LED stuff  
    digitalWrite(ledPin2, HIGH);
    delay(50);
    digitalWrite(ledPin2, LOW);
    delay(50);
    
   // Serial.println("5");
   
}


}
           delay(200);  /* increased time so only 1 instance of 'S' is
                           sent to slave when button is pressed. A short
                           delay time showed that multiple instances were sent
                        */

Nonsense. You need to look at the state change detection example. Using delay() between reading the state of the switch is a rather stupid way of handling switch presses.

  pinMode(button1, INPUT);    // Set the button as input, FORWARD
  pinMode(button2, INPUT);    // Set the button as input, BACKWARD

It is critical that the switches be wired correctly. I suspect that yours are not. How are they wired? Why are you not using the internal pullup resistors, instead of external ones? The internal pullup resistors make wiring switches so much simpler. One leg to ground. The other leg to a digital pin. Pressed == LOW, released == HIGH, just like the top of the switch. Of course, you also need to turn on the internal pull resistors, by using INPUT_PULLUP as the mode or by writing HIGH to the INPUT pin.

Ok, I adopted part of the state button example for my needs. I reduce the code for simplicity. 2 small issues:

(1) What I've noticed is that it does work as a switch 99% of the time. Occasionally, 2 instances of "S" will be sent to the slave when pushed from the RC button.

(2) Also, when the slave receives the "S" indicating that it should go forward, it DOES, but - there is a constant TX/RX from the master to the slave. The slave's serial monitor shows that "S" is constantly being received, but the master shows nothing on its serial monitor. (Yet when I push the button again, all stops).

Any ideas?

// These constants won't change 
const int ledPin = 12;  // LED is connected to pin 12, STATUS
const int button1 = 2;  // FORWARD  button

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button


void setup() 
{
  Serial.begin(9600);
  
  pinMode(ledPin, OUTPUT);    // Set the LED pin as output
  pinMode(button1, INPUT);    // Set the button as input, FORWARD
}

void loop()
{
   
// read the pushbutton input pin:
buttonState = digitalRead(button1);


  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) 
  {

    // if the state has changed, increment the counter
      if (buttonState == HIGH) // FORWARD
      {  
        
       // digitalWrite(ledPin, HIGH);
      Serial.print('S');  // sends what's between the (' ') to partner xbee
      } 
         
    else 
    {     
        digitalWrite(ledPin, LOW);    // turn LED off
    }
  }

// save current state as the last state for next time through loop
lastButtonState = buttonState;
  
}

With out seeing your wiring, the pin that is used for the “S” component may be floating high. Perhaps you should set your button pins high then pull them low like in the below code.

//zoomkat servo button test 12-29-2011
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(170);
  }    
  
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(10);
  }
}

Attached is the basic of my wiring for my push button in fritzing. I really can’t understand what I’m doing wrong here. I’m using a 10K resistor attached to ground, with a input pin going to PIN 2 (as stated in the code).