April 5, 2008
WikiXbee Final Receiver
#wiki
Xbee Final Receiver
// xigbee xport receive one.
#define bit2400Delay 396
#define halfBit2400Delay 198
int sickBtn = 12; // digital
int calmBtn = 13; // digital
int sadBtn = 0; // analog
int angryBtn = 1;
int energeticBtn = 2;
int happyBtn = 3;
int lovedBtn = 4;
int tiredBtn = 5;
******DECLARATIONS FOR XPORT******
#define disconnected 0
#define connecting 1
#define sendData 2
#define endConnection 3
byte done = 0;
//state which helps to do a while loop inside of connection case/switch statement
int status = 0;
//our status while we are in the case/switch statement
byte alreadyPressed = 0;
//flag to check if we pressed the button before.
//to avoid multiple presses.
byte inByte = -1;
//our serial data holder.
byte k = 0;
************************************
// flags that checks if our button is called previously.
byte sickCalled, calmCalled, sadCalled, angryCalled;
byte energeticCalled, happyCalled, lovedCalled, tiredCalled;
byte switchState = 0;
// software serial
byte rx = 7;
byte tx = 8;
byte SWval = 0;
// PWM pins
int redPin = 9; // RED LED
int greenPin = 10; // GREEN LED
int bluePin = 11; // BLUE LED
int redVal, greenVal, blueVal;
void setup() {
Serial.begin(2400);
// software serial
pinMode(rx, INPUT);
pinMode(tx, OUTPUT);
// digitals needs to be declared as inputs
pinMode(sickBtn, INPUT);
pinMode(calmBtn, INPUT);
// Tricolor legs at PWM pins.
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
digitalWrite(tx,HIGH);
setDestination();
}
void loop() {
// check if there is anything coming from the serial
if (Serial.available() > 0) {
SWval = Serial.read();
// Serial.print(SWval);
// Serial.print(SWval);
switchState = SWval;
}
else {
switchState = checkPressed();
}
if((switchState == '1') && (sickCalled == 0)) {
dumpValue('1', '1');
goSick();
}
else if((switchState == '2') && (calmCalled == 0)) {
dumpValue('2', '1');
goCalm();
}
else if ((switchState == '3') && (sadCalled == 0)) {
dumpValue('3','1');
goSad();
}
else if ((switchState == '4') && (angryCalled == 0)) {
dumpValue('4','1');
goAngry();
}
else if ((switchState == '5') && (energeticCalled == 0)) {
dumpValue('5','1');
goEnergetic();
}
else if ((switchState == '6') && (happyCalled == 0)) {
dumpValue('6','1');
goHappy();
}
else if ((switchState == '7') && (lovedCalled == 0)) {
dumpValue('7','1');
goLoved();
}
else if ((switchState == '8') && (tiredCalled == 0)) {
dumpValue('8','1');
goTired();
}
if((switchState == 1) && (sickCalled == 0)) {
Serial.print('1'); // goes to other xbee!
dumpValue('1','2' );
goSick();
}
else if((switchState == 2) && (calmCalled == 0)) {
Serial.print('2'); * * goes to other xbee!
dumpValue('2','2');
goCalm();
}
else if ((switchState == 3) && (sadCalled == 0)) {
Serial.print('3'); * * goes to other xbee!
dumpValue('3','2');
goSad();
}
else if ((switchState == 4) && (angryCalled == 0)) {
Serial.print('4'); * * goes to other xbee!
dumpValue('4','2');
goAngry();
}
else if ((switchState == 5) && (energeticCalled == 0)) {
Serial.print('5'); * * goes to other xbee!
dumpValue('5','2');
goEnergetic();
}
else if ((switchState == 6) && (happyCalled == 0)) {
Serial.print('6'); * * goes to other xbee!
dumpValue('6','2');
goHappy();
}
else if ((switchState == 7) && (lovedCalled == 0)) {
Serial.print('7'); * * goes to other xbee!
dumpValue('7','2');
goLoved();
}
else if ((switchState == 8) && (tiredCalled == 0)) {
Serial.print('8'); * * goes to other xbee!
dumpValue('8','2');
goTired();
}
}
void dumpValue(byte var1, byte var2) {
// flags
done = 0;
alreadyPressed = 1;
while(done == 0) {
switch(status) {
case disconnected:
// Serial.println("I am in disconnected case");
deviceConnect();
break;
case connecting:
// Serial.println("I am in connecting case");
serverConnect();
break;
case sendData:
httpRequest(var1, var2);
break;
case endConnection:
// make all states to the default ones so we
// can call them once we need them again.
status = 0;
// setting the status to disconnected to prepare next time when the
// button is pressed starts from the beginning.
done = 1;
// done equals true meaning we completed our cycle to send information.
alreadyPressed = 0;
// setting the alreadyPressed to false since we can press the buttons again.
k = 0;
break;
}
}
}
void deviceConnect() {
// fill in your server's numerical address below:
if(k==0) {
// Serial.println("C128.122.253.189/80");
SWprint('C');
SWprint('1');
SWprint('2');
SWprint('8');
SWprint('.');
SWprint('1');
SWprint('2');
SWprint('2');
SWprint('.');
SWprint('2');
SWprint('5');
SWprint('3');
SWprint('.');
SWprint('1');
SWprint('8');
SWprint('9');
SWprint('/');
SWprint('8');
SWprint('0');
SWprint('\r');
k++;
}
status = connecting;
}
void serverConnect() {
// wait for sometime
delay(50);
status = sendData;
}
void httpRequest(byte var1, byte var2) {
SWprint('G');
SWprint('E');
SWprint('T');
SWprint(' ');
SWprint('/');
SWprint('~');
SWprint('i');
SWprint('k');
SWprint('5');
SWprint('0');
SWprint('1');
SWprint('/');
SWprint('d');
SWprint('a');
SWprint('t');
SWprint('a');
SWprint('/');
SWprint('d');
SWprint('i');
SWprint('e');
SWprint('.');
SWprint('p');
SWprint('h');
SWprint('p');
SWprint('?');
SWprint('v');
SWprint('a');
SWprint('r');
SWprint('1');
SWprint('=');
SWprint(var1);
SWprint('&');
SWprint('v');
SWprint('a');
SWprint('r');
SWprint('2');
SWprint('=');
SWprint(var2);
SWprint(' ');
SWprint('H');
SWprint('T');
SWprint('T');
SWprint('P');
SWprint('/');
SWprint('1');
SWprint('.');
SWprint('1');
SWprint(10);
SWprint('H');
SWprint('O');
SWprint('S');
SWprint('T');
SWprint(':');
SWprint('i');
SWprint('t');
SWprint('p');
SWprint('.');
SWprint('n');
SWprint('y');
SWprint('u');
SWprint('.');
SWprint('e');
SWprint('d');
SWprint('u');
SWprint(10);
SWprint(10);
/*
Serial.print("GET /~ik501/data/die.php?var1=");
Serial.print(var_, DEC);
Serial.println(" HTTP/1.1");
Serial.println("HOST: itp.nyu.edu");
Serial.println();
*/
status = endConnection;
}
void goSick() {
sickCalled = 1;
calmCalled = 0;
sadCalled = 0;
angryCalled = 0;
energeticCalled = 0;
happyCalled = 0;
lovedCalled = 0;
tiredCalled = 0;
int redDest = 194;
int greenDest = 254;
int blueDest = 0;
goZero(redDest, greenDest, blueDest);
}
void goCalm() {
sickCalled = 0;
calmCalled = 1;
sadCalled = 0;
angryCalled = 0;
energeticCalled = 0;
happyCalled = 0;
lovedCalled = 0;
tiredCalled = 0;
int redDest = 20;
int greenDest = 254;
int blueDest = 254;
goZero(redDest, greenDest, blueDest);
}
void goSad() {
sickCalled = 0;
calmCalled = 0;
sadCalled = 1;
angryCalled = 0;
energeticCalled = 0;
happyCalled = 0;
lovedCalled = 0;
tiredCalled = 0;
int redDest = 28;
int greenDest = 0;
int blueDest = 54;
goZero(redDest, greenDest, blueDest);
}
void goAngry() {
sickCalled = 0;
calmCalled = 0;
sadCalled = 0;
angryCalled = 1;
energeticCalled = 0;
happyCalled = 0;
lovedCalled = 0;
tiredCalled = 0;
int redDest = 232;
int greenDest = 0;
int blueDest = 0;
goZero(redDest, greenDest, blueDest);
}
void goEnergetic() {
sickCalled = 0;
calmCalled = 0;
sadCalled = 0;
angryCalled = 0;
energeticCalled = 1;
happyCalled = 0;
lovedCalled = 0;
tiredCalled = 0;
int redDest = 254;
int greenDest = 92;
int blueDest = 0;
goZero(redDest, greenDest, blueDest);
}
void goHappy() {
sickCalled = 0;
calmCalled = 0;
sadCalled = 0;
angryCalled = 0;
energeticCalled = 0;
happyCalled = 1;
lovedCalled = 0;
tiredCalled = 0;
int redDest = 254;
int greenDest = 254;
int blueDest = 32;
goZero(redDest, greenDest, blueDest);
}
void goLoved() {
sickCalled = 0;
calmCalled = 0;
sadCalled = 0;
angryCalled = 0;
energeticCalled = 0;
happyCalled = 0;
lovedCalled = 1;
tiredCalled = 0;
int redDest = 254;
int greenDest = 8;
int blueDest = 88;
goZero(redDest, greenDest, blueDest);
}
void goTired() {
sickCalled = 0;
calmCalled = 0;
sadCalled = 0;
angryCalled = 0;
energeticCalled = 0;
happyCalled = 0;
lovedCalled = 0;
tiredCalled = 1;
int redDest = 2;
int greenDest = 2;
int blueDest = 56;
goZero(redDest, greenDest, blueDest);
}
void goZero(int destR, int destG, int destB) {
int counter = 4;
while((redVal + greenVal + blueVal) > counter + 1) {
if(redVal <= counter) {
redVal = 1;
}
else {
redVal -= counter;
}
if(greenVal <= counter) {
greenVal = 1;
}
else {
greenVal -= counter;
}
if(blueVal <= counter) {
blueVal = 1;
}
else {
blueVal -= counter;
}
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
}
goCurrent(destR, destG, destB);
// if you go negatives with values they reverse like in a color wheel and go white.
}
void goCurrent(int destR, int destG, int destB) {
int counter = 2;
while(redVal+greenVal+blueVal < destR+destG+destB ) {
if(redVal >= destR) {
redVal = destR;
}
else {
redVal += counter;
}
if(greenVal >= destG) {
greenVal = destG;
}
else {
greenVal += counter;
}
if(blueVal >= destB) {
blueVal = destB;
}
else {
blueVal += counter;
}
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
}
}
byte checkPressed () {
int sick = digitalRead(sickBtn);
int calm = digitalRead(calmBtn);
int sad = analogRead(sadBtn);
int angry = analogRead(angryBtn);
int energetic = analogRead(energeticBtn);
int happy = analogRead(happyBtn);
int loved = analogRead(lovedBtn);
int tired = analogRead(tiredBtn);
/*
int sickBtn = 12; // digital
int calmBtn = 13; // digital
int sadBtn = 0; // analog
int angryBtn = 1;
int energeticBtn = 2;
int happyBtn = 3;
int lovedBtn = 4;
int tiredBtn = 5;
*/
if(sick == 1) {
return 1;
}
if(calm == 1) {
return 2;
}
if(sad >= 812) {
return 3;
}
if(angry >= 812) {
return 4;
}
if(energetic >= 812) {
return 5;
}
if(happy >= 812) {
return 6;
}
if(loved >= 812) {
return 7;
}
if(tired >= 812) {
return 8;
}
else {
}
}
void setDestination() {
Serial.print('X');
delay(1100);
Serial.print('+'); //debugging hello
Serial.print('+');
Serial.print('+');
char thisByte = 0;
while (thisByte != '\r') {
thisByte = Serial.read();
}
Serial.print('A');
Serial.print('T');
Serial.print('I');
Serial.print('D');
Serial.print('2');
Serial.print('5');
Serial.print('8');
Serial.print('2');
Serial.print(',');
Serial.print('M');
Serial.print('Y');
Serial.print('1');
Serial.print(',');
Serial.print('D');
Serial.print('H');
Serial.print('0');
Serial.print(',');
Serial.print('D');
Serial.print('L');
Serial.print('2');
Serial.print(',');
Serial.print('C');
Serial.print('N');
while (thisByte != '\r') {
thisByte = Serial.read();
Serial.print(thisByte);
}
Serial.print('\r');
Serial.print('D');
Serial.print('O');
Serial.print('N');
Serial.print('E');
}
void SWprint(int data)
{
byte mask;
//startbit
digitalWrite(tx,LOW);
delayMicroseconds(bit2400Delay);
for (mask = 0x01; mask>0; mask <<= 1) {
if (data & mask){ // choose bit
digitalWrite(tx,HIGH); // send 1
}
else{
digitalWrite(tx,LOW); // send 0
}
delayMicroseconds(bit2400Delay);
}
//stop bit
digitalWrite(tx, HIGH);
delayMicroseconds(bit2400Delay);
}
int SWread()
{
byte val = 0;
while (digitalRead(rx));
//wait for start bit
if (digitalRead(rx) == LOW) {
delayMicroseconds(halfBit2400Delay);
for (int offset = 0; offset < 8; offset++) {
delayMicroseconds(bit2400Delay);
val |= digitalRead(rx) << offset;
}
//wait for stop bit + extra
delayMicroseconds(bit2400Delay);
delayMicroseconds(bit2400Delay);
return val;
}
}
Continue Reading
Back to Archive