Go Down

Topic: Controlling robots using Esplora with XBEE? (Read 2422 times) previous topic - next topic

jeffmorris

http://21stdigitalhome.blogspot.com/2013/01/wireless-arduino-esplora-controls.html

I tried to modify the example code for my robot but I think that there's a problem. I press a button or move the joystick and the Serial Monitor for Arduino Mega shows two letters or numbers instead of one letter or number. I didn't add the code that controls the servos on the robot. How can I fix the problem?

Code: [Select]

//code for Arduino Esplora

#include <Esplora.h>
boolean buttonStates[8];
const byte buttons[] = {
  JOYSTICK_DOWN,
  JOYSTICK_LEFT,
  JOYSTICK_UP,
  JOYSTICK_RIGHT,
  SWITCH_RIGHT,
  SWITCH_LEFT,
  SWITCH_UP,
  SWITCH_DOWN,
};
const char keystrokes[] = {
  'D',
  'L',
  'U',
  'R',
  '4',
  '2',
  '3',
  '1'
};
void setup()
{
  Serial1.begin(115200);
}
void loop()
{
  for (byte thisButton=0; thisButton<8; thisButton++)
  {
    boolean lastState = buttonStates[thisButton];
    boolean newState = Esplora.readButton(buttons[thisButton]);
    if (lastState != newState)
    {
      Serial1.println(keystrokes[thisButton]);
    }
    buttonStates[thisButton] = newState;
  }
  if(Esplora.readJoystickButton()==LOW)
      Serial1.println("0");
  delay(50);
}

//code for Arduino Mega on robot
#include <SoftwareSerial.h>
SoftwareSerial Xbee(51,50);
#define DEBUG 0
char cmdChar;
void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  Xbee.begin(115200);
}
void loop()
{
  if(Xbee.available())
  {
    cmdChar = Xbee.read();
    if(DEBUG)
    {
       Serial.println(cmdChar);
    }
    switch(cmdChar)
    {
      case '1':
        Serial.println("Case 1");
        break;
      case '2':
        Serial.println("Case 2");
        break;
      case '3':
        Serial.println("Case 3");
        break;
      case '4':
        Serial.println("Case 4");
        break;
      case '0':
        Serial.println("Case 0");
        break;
      case 'D':
        Serial.println("Case D");
        break;
      case 'U':
        Serial.println("Case U");
        break;
      case 'L':
        Serial.println("Case L");
        break;
      case 'R':
        Serial.println("Case R");
        break;
    }
    delay(15);
  }
}

TheKitty

The joystick will continue to send commands if you hold it longer than the poll time.  You'll want to look at either responding to multiple commands or perhaps add code that if you receive double commands within a short time ignore the duplicate.

Go Up