Wireless Gesture Controlled Robot
Example
Wireless Glove Code
In this part of the example, we'll have the glove send a character when the thumb and middle finger make contact. As long as the two fingers have contact, the robot will move forward, forward-left, back, or forward -right based on the orientation of your hand. When the custom button is not pressed, the buzzer will make a familiar 8-bit sound when waving your hand or "jabbing" the air. The RGB LED will light up based on the mode and orientation of the hand.
Copy the code, paste it into the Arduino IDE, select your board (Arduino/Genuino Uno), and COM port. Then upload the code to the glove.
language:c
// We'll use SoftwareSerial to communicate with the XBee:
#include <SoftwareSerial.h>
//For Atmega328P's
// XBee's DOUT (TX) is connected to pin 2 (Arduino's Software RX)
// XBee's DIN (RX) is connected to pin 3 (Arduino's Software TX)
SoftwareSerial XBee(2, 3); // RX, TX
//For Atmega2560, ATmega32U4, etc.
// XBee's DOUT (TX) is connected to pin 10 (Arduino's Software RX)
// XBee's DIN (RX) is connected to pin 11 (Arduino's Software TX)
//SoftwareSerial XBee(10, 11); // RX, TX
//set analog read pins
const int xPin = 2;//x=A2
const int yPin = 1;//y=A1
const int zPin = 0;//z=A0
//read the analog values from the accelerometer
int xRead = analogRead(xPin);
int yRead = analogRead(yPin);
int zRead = analogRead(zPin);
//LED Status Indicator
int ledR = 5;//hardware PWM
int ledG = 6;//hardware PWM
int ledB = 9; //hardware PWM
//Accelerate Button
#define ACCELERATE_BUTTON 4 // Pin used for accelerate button
const int ledPin1 = 13; //LED on the push button
boolean current_buttonACCELERATE_State;
void setup() {
// initialize the digital pins as an output for LEDs
pinMode(ledPin1, OUTPUT);
pinMode(ledR, OUTPUT);
pinMode(ledG, OUTPUT);
pinMode(ledB, OUTPUT);
analogReference(EXTERNAL);//reference 3.3V since using 3.3V accelerometer
pinMode(ACCELERATE_BUTTON, INPUT_PULLUP); // Enable pullup resistor for accelerate button D2
// Set up both ports at 9600 baud. This value is most important
// for the XBee. Make sure the baud rate matches the config
// setting of your XBee.
XBee.begin(9600);
for (int i = 0; i < 3; i++) {
digitalWrite(ledPin1, HIGH);
delay(50);
digitalWrite(ledPin1, LOW);
delay(50);
}
sequenceTest();//visually initialization
Serial.begin(9600);
Serial.println("Wireless XBee Glove Controller Initialized");
}
void loop() {
current_buttonACCELERATE_State = digitalRead(ACCELERATE_BUTTON);
//Read accelerometer axes using through the ADC
//Note: Check description at top for results based on Accelerometer Mode's Features
xRead = analogRead(xPin);
Serial.print("Analog xPin (A2) = ");
Serial.println(xRead);
yRead = analogRead(yPin);
Serial.print("Analog yPin (A1) = ");
Serial.println(yRead);
zRead = analogRead(zPin);
Serial.print("Analog zPin (A2) = ");
Serial.println(zRead);
Serial.println("");
//delay(500); //slow down the print to read, adjust as necessary for testing
if (current_buttonACCELERATE_State == LOW) {
if (xRead < 430) {
Serial.print("Drive Forward, xRead = ");
Serial.println(xRead);
Serial.println('A');
XBee.write('A');
greenON();
}
else if (xRead > 590) {
Serial.print("Drive Backward, xRead = ");
Serial.println(xRead);
Serial.println('C');
XBee.write('C');
blueON();
}
else if (yRead > 590) {
Serial.print("Drive Forward Right, yRead = ");
Serial.println(yRead);
Serial.println('B');
XBee.write('B');
cyanON();
}
else if (yRead < 430) {
Serial.print("Drive Forward Left, yRead = ");
Serial.println(yRead);
Serial.println('D');
XBee.write('D');
cyanON();
}
else {
Serial.println("Coast");
Serial.println('J');
XBee.write('J');
magentaON();
}
}
else {
if (xRead > 670) {
Serial.println("Coin Sound, xRead = ");
Serial.println(xRead);
Serial.println('X');
XBee.write('X');
allOFF();
delay(50);
yellowON();
delay(50);
}
if (zRead < 400) {
Serial.println("Fireball Sound, zRead = ");
Serial.println(zRead);
Serial.println('Y');
XBee.write('Y');
redON();
delay(50);
allOFF();
delay(50);
redON();
delay(50);
allOFF();
delay(50);
}
else {
Serial.println("Stop");
Serial.println('K');
XBee.write('K');
redON();
delay(750);
}
}
//show that we are sending a character
digitalWrite(ledPin1, HIGH);
delay(50);
digitalWrite(ledPin1, LOW);
delay(50);
}//end loop
void allOFF() {
analogWrite(ledR, 0);
analogWrite(ledG, 0);
analogWrite(ledB, 0);
}
void allON() {
analogWrite(ledR, 150);
analogWrite(ledG, 255);
analogWrite(ledB, 255);
}
void redON() {
analogWrite(ledR, 255);
analogWrite(ledG, 0);
analogWrite(ledB, 0);
}
void magentaON() {
analogWrite(ledR, 150);
analogWrite(ledG, 0);
analogWrite(ledB, 255);
}
void blueON() {
analogWrite(ledR, 0);
analogWrite(ledG, 0);
analogWrite(ledB, 255);
}
void cyanON() {
analogWrite(ledR, 0);
analogWrite(ledG, 255);
analogWrite(ledB, 255);
}
void greenON() {
analogWrite(ledR, 0);
analogWrite(ledG, 255);
analogWrite(ledB, 0);
}
void yellowON() {
analogWrite(ledR, 150);
analogWrite(ledG, 255);
analogWrite(ledB, 0);
}
void sequenceTest() {
redON();
delay(50);
magentaON();
delay(50);
blueON();
delay(50);
cyanON();
delay(50);
greenON();
delay(50);
yellowON();
delay(50);
allON();
delay(50);
allOFF();
delay(50);
}
Receiving XBee Robot Code
The commands to control the RedBot should be the same code that was used in the last example of the Wireless RC Robot with Arduino and XBees tutorial. Head over to Experiment 4.2: Adding Audio with the ATmega328P to upload code to the RedBot mainboard if you have not already.
What You Should See
After uploading, touch the metal snap pins between your thumb and middle finger to move the robot forward-left, forward, forward-right, or backward. The RGB LED will light up based on the orientation of your hand. Separate your fingers and punch the air as if there is a question mark block above you to hear coin sound effect! Wave your hand to see if you can make a fireball sound effect!