Wireless Glove Controller

Pages
Contributors: bboyho
Favorited Favorite 9

Example 1: Sending and Receiving

Note: This example assumes you are using the latest version of the Arduino IDE on your desktop. If this is your first time using Arduino, please review our tutorial on installing the Arduino IDE.

If you've never connected an FTDI device to your computer before, you may need to install drivers for the USB-to-serial converter. Check out our How to Install FTDI Drivers tutorial for help with the installation.

Example 1a: Sending a Character with the Glove

In this part of the example, we'll have the glove send a character when a button is pressed. While you could just open a serial terminal to check if characters were received during testing, it would just not be as fun as blinking an LED wirelessly after a button press.

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


//Send
const int button1Pin = 4; //push button
const int status_LED = 13;  //LED on the push button

char send_CHAR = 'A'; //default send character

//initialize variables to read buttons
int button1State;

//variables to check for button1 state
boolean prev_button1State = false;
boolean current_button1State = false;

/*******************Setup Loop***************************/
void setup() {
  //Declare pin modes
  pinMode(button1Pin, INPUT_PULLUP); //use internal pullup resistor with send button
  pinMode (status_LED, OUTPUT);//LED to indicate when character has been sent

  //Status LED to see if the Transmitting XBee is initializing
  for (int i = 0; i < 3; i++) {
    digitalWrite(status_LED, HIGH);//set Status LED on
    delay(50);
    digitalWrite(status_LED, LOW); //set Status LED off
    delay(50);
  }

  //Declare serial connections for debugging
  Serial.begin(9600);
  Serial.println("Arduino Serial Ready");

  XBee.begin(9600);
  Serial.println("Glove Controller's XBee Ready to Communicate");

}//end setup()

void loop() {

  button1State = digitalRead(button1Pin);
  /***button1state
    - LOW or 0 means pressed
    - HIGH or 1 means not pressed
   ****/

  //if button is pressed, it will be pulled low
  if (button1State == LOW) {
    digitalWrite(status_LED, HIGH); //turn push button LED ON
    current_button1State = true; // button has been pressed once

    if (prev_button1State != current_button1State) //check to see if button is still being pressed
    {
      Serial.println("Button is pressed.");
      XBee.write(send_CHAR);//Sending a character
    }
    else {
      //do nothing because finger is still on button
    }
    prev_button1State = current_button1State;
  }

  //button has not been pressed, it will be high again
  else {
    current_button1State = false;
    digitalWrite(status_LED, LOW); // turn push button LED OFF

    prev_button1State = current_button1State;
  }
}//end loop()

Example 1b: Receiving XBee Node

In this part of the code, we'll have the receiving XBee blinking the LED connected to pin 13 as well. Copy the code below, paste it into the Arduino IDE, select your board (Arduino/Genuino Uno), and COM port. The Arduino should enumerate on a different COM port so make sure to adjust the COM port. Then upload the code to the receiving XBee's Arduino.

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


//Declare character 'c_data'
char c_data;

//LED to check if the LED is initialized.
const int status_LED = 13;

/*******************Setup Loop***************************/
void setup() {
  // put your setup code here, to run once:

  //Declare pin modes
  pinMode (status_LED, OUTPUT);//LED to indicate when character has been sent

  //Status LED to see if the Receiving XBee is initializing
  for (int i = 0; i < 3; i++) {
    digitalWrite(status_LED, HIGH);//set Status LED on
    delay(50);
    digitalWrite(status_LED, LOW); //set Status LED off
    delay(50);
  }

  //Declare serial connections for debugging
  Serial.begin(9600);
  Serial.println("Arduino Serial Ready");

  XBee.begin(9600);
  Serial.println("XBee Ready to Receive");

}//end setup()

void loop() {

  //Check if XBee is receiving data from other XBee

  if (XBee.available() || Serial.available()) {
    if (XBee.available()) {
      c_data = XBee.read();//store received value from XBee into variable

    }

    else if (Serial.available()) {
      c_data = Serial.read();//store received value from Serial Monitor into variable

    }

    //Check to see if character sent is letter A
    if (c_data == 'A') {
      digitalWrite(status_LED, HIGH); //turn ON Status LED
      Serial.println("Character Received, ");
      Serial.println(c_data);
    }

    else {
      //do nothing
    }
  }
  delay(100);
  digitalWrite(status_LED, LOW); //turn OFF Status LED

}//end loop()

What You Should See

After uploading, touch the metal snap pins between your thumb and middle finger. This should send one character from the glove to the receiving XBee. As a result, the LED on the glove will stay on as long as the button is pressed. The receiving XBee's LED will blink whenever a character is received. As part of the design, we'll only send a character once when the button is pressed. To send another character, move your thumb away from the middle finger and then touch the metal snap pins together again.

Glove Wirelesly Controlling an LED