My FIO is a sleep most of the time, however it wakes up occasionally to send a “1” via serial using two xbee modems.
The problem is, i’m seeing garbage on the serial line as soon as the xbee wakes up, before my “1” is sent.
http://img853.imageshack.us/img853/8072/84627384.png
You can see the “1” at the end of the lines.
I’ve configured the xbee to use pin sleep (hibernate). I have the DTR pin attached to Digital pin 6. And to wake the xbee I’m setting the pin low.
My code does the following:
-
Sleep and listen for an interrupt on D2
-
Interrupt wakes arduino, increments the counter and puts the unit back to sleep
-
When the counter reaches 10, we wake the Xbee, and send a “1”
-
Delay 200ms
-
Reset counter to 0, go back to sleep:
#include <avr/sleep.h>
#define photoPin 2 // pin used for waking up
#define ledPin 13 //on board LED
int blinkCount = 0;
#define XBEE_sleepPin 6 // connected to pin 9 of the XBEE
void wakeUpNow() // here the interrupt is handled after wakeup
{
}
void sleepNow() // here we put the arduino to sleep
{
// sleep mode is set here
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
// enables the sleep bit in the mcucr register
sleep_enable();
//switch off ADC
ADCSRA = ADCSRA & 0x7F; // clear ADEN
// switch off analog comparator
ACSR = ACSR & 0x7F;
// turn off the onboard LED during sleep (just incase it's turned on)
pinMode (ledPin, INPUT);
digitalWrite(ledPin,LOW);
//put the xbee to sleep. Need to verify.
pinMode (XBEE_sleepPin,INPUT); // put XBee to sleep
digitalWrite(XBEE_sleepPin,LOW); // disable our pullup reduce amps required
// use interrupt 0 (digital pin 2) and run the wakeUpNow function.
// When this pin is LOW, the interrupt is triggered and wakes up the board.
attachInterrupt(0,wakeUpNow, LOW);
// Good night Fio! zzZZzzz (We are now sleeping)
sleep_mode();
// THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
// first thing after waking from sleep, disable sleeping.
sleep_disable();
// Don't listen for an interrupt, we dont want to run the wake up code again
// incase the pin goes LOW
detachInterrupt(0);
// Do the things we want to do when we come out of sleep. Count the blink!
restoreFromSleep();
}
void restoreFromSleep()
{
//delay(200);
// Pin 13 is the onboard LED
// We want to use a visual cue so we know when a blink from the meter has happened
// This was "disabled" earlier when the Fio went to sleep.
pinMode(13, OUTPUT);
digitalWrite(13,HIGH);
// Add 1 to our counter
blinkCount++;
}
void setup() {
// Make sure the pin the photo detector is set to input mode
pinMode(photoPin, INPUT);
// Do you have your own pull up resistor? Comment this out. If not, you can use
// the built in one.
digitalWrite(photoPin, HIGH);
// Start serial communication (57600 baud)
Serial.begin(57600);
}
void loop()
{
// if we have 10 blinks (we used 0.01 kWh of electricity)
// send a "1" over the serial line
if (blinkCount == 10) {
// Wake up the Xbee. So we can send data to the server
pinMode(XBEE_sleepPin,OUTPUT); // control XBee Sleep
digitalWrite(XBEE_sleepPin,LOW); // wake-up XBee
// small delay to make sure that the xbee has associated with the server
delay(2000);
// send data
Serial.println("1");
blinkCount = 0;
}
delay(200);
sleepNow(); // sleep function called here
}
Anyone have any idea what I could be doing wrong? Thanks.