/* Metronome Reader Program * for Collaborative Mesh Networking class * by Rob Faludi * www.faludi.com * version 1.0.0 */ int ledPin = 13; // declare pin for the LED int beat = 0; // used to store the current beat number unsigned long timeCode = 0; // used to store the current time code value void setup() { pinMode(ledPin, OUTPUT); // initializes LED output Serial.begin(9600); // turn on the serial port blinkLED(ledPin,2,100); setupXBee(); Serial.flush(); // clear the serial buffer at the start of the program } void loop() { if (Serial.available() > 0 ) { // check that a byte has come in char inByte = Serial.read(); // read the byte if (inByte == '*') { // if this was the start byte while (Serial.available() < 7) { ; // do nothing while waiting for all five bytes of information } beat = Serial.read(); // read the beat number timeCode = Serial.read(); // read the first byte of the time code timeCode = timeCode + (Serial.read() << 8); // read the second byte of the time code, shift it up and add to the existing number timeCode = timeCode + (Serial.read() << 16); // read the third byte of the time code, shift it up and add to the existing number timeCode = timeCode + (Serial.read() << 24); // read the fourth byte of the time code, shift it up and add to the existing number Serial.flush(); // get rid of the carriage return and linefeed characters, to clear the input for the next beat /* // show the reading on the main serial port --> NOT RECOMMENDED IN YOUR REAL CODE! Serial.print("beat: "); Serial.print(beat); Serial.print(" tc: "); Serial.print(timeCode); Serial.print("\n\r"); */ blinkLED(ledPin,1,20); // blink the LED once } } } void setupXBee() { boolean success = false; int ctr = 0; while (success == false && ctr < 100) { // blink the status LED blinkLED(ledPin, 2, 250); // an arbitrary byte to wake up the XBee //Serial.print("X"); //delay(1100); // put the XBee in command mode Serial.print("+++"); delay(1100); // wait for a response from the XBee for 2000 ms, or start // over with setup if no valid response comes // set the PAN (personal area network) ID number // set the MY (16 bit address) // set the Destination High to 0x0 // set the Destination Low (16 bit address) // exit command mode (using a Serial.printLN to end the command with a linefeed) Serial.println("ATRE,ID333A,MY100,DH0,DL101,CN"); if (checkFor("OK", 1000)) { // if an OK was received then continue success = true; } else { success = false; } ctr++; } } ///////////////////////////////// UTILITY FUNCTIONS ////////////////////////////////////////// // this function checks for a specific response on the serial port // it accepts a string to look for and a timeout in milliseconds int checkFor(char* desiredResponse, int timeout) { int result = 0; int length = 40; char incomingResponse[41]; memset(incomingResponse,0,length); // initialize all incomingResponse string positions to null char inByte = NULL; long startTime = millis();//makes the start time = to now char* ptr_incomingResponse = incomingResponse; // while we haven't timed out or gotten back the string that we are looking for while (millis() - startTime < timeout && strstr(incomingResponse,desiredResponse) == NULL ) { //strstr compares strings if (Serial.available() > 0) { // if there are any bytes waiting to be read inByte = Serial.read(); // read one byte if (incomingResponse > ptr_incomingResponse-length) { // if we haven't read in 80 characters yet *ptr_incomingResponse = inByte; // put the byte into the current position in the string ptr_incomingResponse++; // advance to the next position in the string } else { //move the last char to be next to last, and so forth until we reach the end of the array. for (int i = 0; i < length; i++) { incomingResponse[i] = incomingResponse[i+1]; } incomingResponse[length-1] = inByte; // put the byte into the current position in the string } } } if (strstr(incomingResponse,desiredResponse) != NULL ) { // if the desired string is found result = 1; } else { result = 0; } return result; } // this function blinks the an LED light as many times as requested void blinkLED(int targetPin, int numBlinks, int blinkRate) { for (int i=0; i