//jleblanc 12.02.06 (based on Rob Faludi rob@faludi.com) //SEND with Sleep //Variables////////////////////////////////////// // serial out is on port 1 // serial in is on port 0 int switchPin = 12;// a digital input is on port 12 int ledPin = 13;// a status light is on port 13 int sleepPin = 2; //Used to control sleep // a byte to send out data: char thisByte = 0; void setup () { // set pins to input and output appropriately pinMode(ledPin, OUTPUT); pinMode(switchPin, INPUT); pinMode(sleepPin, OUTPUT); // start up the serial connection with 9600-8-n-1-true (non-inverted): Serial.begin(9600); // blink the status LED blinkLED(ledPin, 3); // for some reason it seems to help to send an arbitrary character first //then pause for the guard time before requesting command mode 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 if (returnedOK() == 'T') { // if an OK was received then continue } else { setup(); // otherwise go back and try setup again } //See Rob's original code for explanation on this command to the XBee //I have added the SM command to indicate which sleep mode to use Serial.println("ATID3330,DH0,DL1,SM1,CN"); // wait for a response from the XBee for 2000 ms, or start // over with setup if no valid response comes if (returnedOK() == 'T') { // if an OK was received then continue } else { setup(); // otherwise go back and try setup again } } void loop () { //Wake up the XBee digitalWrite(sleepPin, LOW); delay(15); //pause to let it wake up // read the switch: thisByte = digitalRead(switchPin); // convert it to a readable ASCII value, send it out the serial port: Serial.print(thisByte, DEC); delay(10); //Put the XBee to sleep digitalWrite(sleepPin, HIGH); delay(10000); //Wait 10 seconds } //USED FUNCTIONS///////////////////////////////////////////////// char returnedOK () { // this function checks the response on the serial port to see if it was an "OK" or not char incomingChar[3]; char okString[] = "OK"; char result = 'n'; int startTime = millis(); while (millis() - startTime < 2000 && result == 'n') { // use a timeout of 10 seconds if (Serial.available() > 1) { // read three incoming bytes which should be "O", "K", and a linefeed: for (int i=0; i<3; i++) { incomingChar[i] = Serial.read(); } if ( strstr(incomingChar, okString) != NULL ) { // check to see if the respose is "OK" // if (incomingChar[0] == 'O' && incomingChar[1] == 'K') { // check to see if the first two characters are "OK" result = 'T'; // return T if "OK" was the response } else { result = 'F'; // otherwise return F } } } return result; } void blinkLED(int targetPin, int numBlinks) { // this function blinks the status LED light as many times as requested for (int i=0; i