Beacon Pendants: Code

//Beacon Locator Thing V 1.0  A slamming together of Rob Faludi’s RSSI Reader, and Tom Igoes Analog Duplex Sender Code
//by Chris Cerrito

//FOR XBEE2
//******************************************************************************int
int RSSI = 0; // holds the Recieved Signal Strength Value, the value used to range find the Xbees
int address = 0; //holds the 16 bit address of the incoming signal
int lastSender = 0; // last address we received an API packet from

//******************************************************************************#define

#define txLed 2 //indicates outgoing data
#define rxLed 3 //indicates incoming data
#define RedChannel 11 //red pwm pin
#define GreenChannel 9 //green pwm pin
#define BlueChannel 10 //blue pwm pin

int RedChanVal = 0;
int GreenChanVal = 0;
int BlueChanVal = 0;

unsigned long RedTimeOut = 0;
unsigned long GreenTimeOut = 0;
unsigned long StartTime = 0;

//*********************************************************************************

void setup() {
Serial.begin(9600); //Lets talk.
pinMode(txLed, OUTPUT);
pinMode(rxLed, OUTPUT);
pinMode(RedChannel, OUTPUT);
pinMode(GreenChannel, OUTPUT);
pinMode(BlueChannel, OUTPUT);
setDestination();
}

//***********************************************************************************WORKS?

void setDestination(){
blink(2);               //blink to indicate that the destination code is about to run
delay(1200);
Serial.print(”+++”); //throw Xbee into command mode
delay(500);
char thisByte = 0;
while (thisByte != ‘\r’) {           //Xbee will reply with an OK/r…wait for the ?r before printing
if (Serial.available() > 0) {      //if theres something in the serial, read it.
thisByte = Serial.read();
}
}
Serial.print(”ATDH0, DLFFFF\r”); //sets the address. Puts in the Xbee in Broadcast Mode—Anyone can listen
delay(10);
Serial.print(”ATMY0001\r”); //gives the Xbee an Address of 1. Use single digit addresses. CHANGE TO A DIFFERENT address fr each UNIT.
delay(10);
Serial.print(”ATID1124\r”); //set the PAN ID
delay(10);
Serial.print(”ATAP1\r”); //put the Xbee into API mode
delay(10);
Serial.print(”ATCN\r”); //take the Xbee out of command mode.
delay(1200);
blink(2);

}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++works

void blink(int howManyTimes) {
for (int i=0; i< howManyTimes; i++) {
digitalWrite(txLed, HIGH);
delay(200);
digitalWrite(txLed, LOW);
delay(1200);
}
}

//********************************************//Reads an API string

void APIread() {
if (Serial.available() > 13) {   //checks to make sure that we have received at least one frame worth of bytes
int inByte = Serial.read();    //reads the incoming API string
if (inByte == 0×7E) {          // (first byte)looks for tilda (ox7E) place holder…marks the beginning of the string
for (int i=0;i<4;i++) {
int junk = Serial.read();  // (next four bytes)  reads and discards the next four bytes that we’re not going to use
}

address = Serial.read();       // the sixth byte is the low part of the address (ONLY USE ONE BYTE ADDRESSES!)
RSSI = Serial.read();          // the seventh byte is the RSSI value

for (int i=0;i<7;i++) {
int junk = Serial.read();     //reads and discards the last six bytes of the API string. We dont need them. Whats in them? beats the crap outta me.
}

AssignValues();                 //if there are values to assign from a successful serial read, assign them.
}
}

}

//*******************************************//

void AssignValues() {

if (address == 0003) {                              //if Xbee 3 is detected, give the red channel a value
GreenChanVal=(100-RSSI)*2;
GreenTimeOut=  millis();
}

if (address == 0002) {                         //if Xbee2 is detected, give the green channel a value
RedChanVal=(100-RSSI)*2;
RedTimeOut = millis();
}

if (address > 0) {                         //if an address is recieved (Showing that the Xbee has
digitalWrite(rxLed, HIGH);               //received and processed info.
}
else
{digitalWrite(rxLed, LOW);
}
}
//********************************************

void Lightup() {
analogWrite(GreenChannel, GreenChanVal);
analogWrite(RedChannel, RedChanVal);
}

//**********************************************

void Timeout() {

if ((millis() - GreenTimeOut) > 1000) {
GreenChanVal=0;
}

if ((millis() - RedTimeOut) > 1000) {
RedChanVal=0;
}
}

//*********************************************

void Sendjunk() {
digitalWrite(txLed, HIGH);
sendData(”X”,0xFFFF);    //SETS ADDRESS IS REDUNDANT
delay(50);
digitalWrite(txLed, LOW);
}

//*******************************************

void loop () {
StartTime = millis();
APIread();
Lightup();
Sendjunk();
Timeout();
Serial.print(”GreenChanVal”);
Serial.print(GreenChanVal);
Serial.print(”RedChanVal”);
Serial.print(RedChanVal);
}

Leave a Reply