Skip to content

Instantly share code, notes, and snippets.

@OrganicIrradiation
Created March 23, 2015 10:30
Show Gist options
  • Save OrganicIrradiation/b31a6699d3f7c41a9ae2 to your computer and use it in GitHub Desktop.
Save OrganicIrradiation/b31a6699d3f7c41a9ae2 to your computer and use it in GitHub Desktop.
Arduino FIO Low Power Setup
/*
* SuperSleepyFio Example SuperSleepyFio.ino
* Steven A Cholewiak - www.semifluid.com
*
* This sketch takes advantage of the XBee's hibernation mode as
* well as the Ardunio Fio's Power Save Mode to grossly reduce power
* consumption. Purely an example of low power Arduino Fio usage.
*
*/
#include <avr/wdt.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
const int ledPin = 13;
const int XBeeSleep = 2; // Connect to XBee DTR for hibernation mode
const int waitPeriod = 8; // Number of 8 second cycles before waking
// up XBee and sending data (8*8 = 64 seconds)
void sleepNow()
{
/* Now is the time to set the sleep mode. In the Atmega8 datasheet
* http://www.atmel.com/dyn/resources/prod_documents/doc2486.pdf on page 35
* there is a list of sleep modes which explains which clocks and
* wake up sources are available in which sleep modes.
*
* In the avr/sleep.h file, the call names of these sleep modus are to be found:
*
* The 5 different modes are:
* SLEEP_MODE_IDLE -the least power savings
* SLEEP_MODE_ADC
* SLEEP_MODE_PWR_SAVE
* SLEEP_MODE_STANDBY
* SLEEP_MODE_PWR_DOWN -the most power savings
*
* the power reduction management <avr/power.h> is described in
* http://www.nongnu.org/avr-libc/user-manual/group__avr__power.html
*/
set_sleep_mode(SLEEP_MODE_PWR_SAVE); // Sleep mode is set here
sleep_enable(); // Enables the sleep bit in the mcucr register
// so sleep is possible. just a safety pin
sleep_mode(); // Here the device is actually put to sleep!!
// THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
sleep_disable(); // Dirst thing after waking from sleep:
// disable sleep...
}
ISR (WDT_vect) { // WDT Wakeup
cli();
wdt_disable();
sei();
}
// Variable Definition
volatile int MeasurementID = 1;
volatile int timeKeeper = 0;
volatile char XBeeIDH[8];
volatile char XBeeIDL[8];
void setup() {
Serial.begin(57600);
pinMode(XBeeSleep, OUTPUT);
digitalWrite(XBeeSleep, 0); // Enable XBee
digitalWrite(ledPin, 1); // Turn on Notification LED
delay(4000); // 4 second LED blink, good for wireless programming
digitalWrite(ledPin, 0); // Turn off Notification LED
digitalWrite(XBeeSleep, 1); // Disable XBee
}
void loop() {
if (timeKeeper == (waitPeriod-1)) { // Transmit every 8*8 (64) seconds
digitalWrite(XBeeSleep, 0); // Enable XBee
delay(50); // Wait for XBee Wakeup
Serial.println( MeasurementID, DEC );
MeasurementID++;
digitalWrite(ledPin, 1); // Turn on Notification LED
delay(50); // Blink LED
digitalWrite(ledPin, 0); // Turn off Notification LED
digitalWrite(XBeeSleep, 1); // Disable XBee
timeKeeper = 0; // Reset Timekeeper
} else {
digitalWrite(ledPin, 1); // Turn on Notification LED
delay(1); // Blink LED very quickly
digitalWrite(ledPin, 0); // Turn off Notification LED
timeKeeper++; // Increment timekeeper variable
}
wdt_reset(); // Get ready to go to sleep...
watchdogEnable(); // Turn on the watchdog timer
sleepNow(); // Go to sleep, watchdog timer will wake later
}
void watchdogEnable() { // Turn on watchdog timer; interrupt mode every 8.0s
cli();
MCUSR = 0;
WDTCSR |= B00011000;
//WDTCSR = B01000111; // 2 Second Timeout
//WDTCSR = B01100000; // 4 Second Timeout
WDTCSR = B01100001; // 8 Second Timeout
sei();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment