/* Battery Test Serial Writer * Sends a serial heartbeat that indicates that battery power * is still sufficient. When the battery dies, the heartbeat will stop */ int ledPin = 13; // select the pin for the LED void setup() { pinMode(ledPin,OUTPUT); // declare the LED's pin as output beginSerial(9600); // connect to the serial port blinkLED(ledPin, 2, 100); setupXBee(); } void loop () { int printValue = 65; // int printValue = analogRead(0)/4; Serial.print(printValue,BYTE); delay(1000); } void setupXBee() { boolean success = false; int ctr = 0; while (success == false && ctr < 100) { // blink the status LED blinkLED(ledPin, 2, 250); delay(1100); // put the XBee in command mode Serial.print("+++"); delay(1100); // 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,ID3333,MY0,DH0,DL1,CN"); if (checkFor("OK", 1000)) { // if an OK was received then continue // debug.println("SetupOK"); success = true; // ready to leave the while loop } else { //debug.println("NoSetup"); success = false; // continue with the while loop } ctr++; // increment the counter so we don't keep trying forever } } ///////////////////////////////// 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 = 10; char incomingResponse[length +1]; 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 } } } // debug.println(incomingResponse); 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(byte targetPin, int numBlinks, int blinkRate) { for (int i=0; i