Skip to content

Instantly share code, notes, and snippets.

@bboyho
Last active March 20, 2019 07:08
Show Gist options
  • Save bboyho/b0e69c43ec943aee19507bb58c8b4b47 to your computer and use it in GitHub Desktop.
Save bboyho/b0e69c43ec943aee19507bb58c8b4b47 to your computer and use it in GitHub Desktop.
// 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);
}
@bboyho
Copy link
Author

bboyho commented Mar 7, 2019

Calibrate values so it does not have to be exactly 90 degrees on each side to move.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment