175 lines
5.5 KiB
C++
Executable File
175 lines
5.5 KiB
C++
Executable File
/*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version 2
|
|
* of the License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
*/
|
|
#include <avr/sleep.h>
|
|
#include <MsTimer2.h>
|
|
|
|
|
|
const int TrigPin = 4;
|
|
const int EchoPin = 5;
|
|
|
|
|
|
const unsigned long TIME = 512;
|
|
const unsigned long TWOTIME = TIME*2;
|
|
|
|
// --------
|
|
// Wake up
|
|
// --------
|
|
int wakePin = 2; // pin used for waking up
|
|
int sleepStatus = 0; // variable to store a request for sleep
|
|
int count = 0; // counter
|
|
|
|
// ----------
|
|
// Timer
|
|
// ----------
|
|
|
|
void flash() {
|
|
// Serial.println("flash");
|
|
static boolean output = HIGH;
|
|
|
|
digitalWrite(13, output);
|
|
|
|
//digitalWrite(wakePin, LOW);
|
|
|
|
output = !output;
|
|
|
|
sleepNow();
|
|
|
|
}
|
|
|
|
/******************************************************************/
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
|
|
// Wakeup
|
|
pinMode(wakePin, INPUT);
|
|
|
|
/* Now it is time to enable an interrupt. In the function call
|
|
* attachInterrupt(A, B, C)
|
|
* A can be either 0 or 1 for interrupts on pin 2 or 3.
|
|
*
|
|
* B Name of a function you want to execute while in interrupt A.
|
|
*
|
|
* C Trigger mode of the interrupt pin. can be:
|
|
* LOW a low level trigger
|
|
* CHANGE a change in level trigger
|
|
* RISING a rising edge of a level trigger
|
|
* FALLING a falling edge of a level trigger
|
|
*
|
|
* In all but the IDLE sleep modes only LOW can be used.
|
|
*/
|
|
|
|
attachInterrupt(0, wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function
|
|
// wakeUpNow when pin 2 gets LOW
|
|
|
|
|
|
pinMode(TrigPin, OUTPUT);
|
|
pinMode(EchoPin, INPUT);
|
|
|
|
|
|
//timer
|
|
pinMode(13, OUTPUT);
|
|
|
|
MsTimer2::set(5000, flash); // 500ms period
|
|
MsTimer2::start();
|
|
|
|
}
|
|
|
|
void wakeUpNow() // here the interrupt is handled after wakeup
|
|
{
|
|
// execute code here after wake-up before returning to the loop() function
|
|
// timers and code using timers (serial.print and more...) will not work here.
|
|
// we don't really need to execute any special functions here, since we
|
|
// just want the thing to wake up
|
|
// Serial.println("Reveil");
|
|
|
|
}
|
|
|
|
void sleepNow() // here we put the arduino to sleep
|
|
{
|
|
/* 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 mode.
|
|
*
|
|
* In the avr/sleep.h file, the call names of these sleep modes 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
|
|
*SLEEP_MODE_IDLE: 15 mA
|
|
SLEEP_MODE_ADC: 6.5 mA
|
|
SLEEP_MODE_PWR_SAVE: 1.62 mA
|
|
SLEEP_MODE_EXT_STANDBY: 1.62 mA
|
|
SLEEP_MODE_STANDBY : 0.84 mA
|
|
SLEEP_MODE_PWR_DOWN : 0.36 mA
|
|
* For now, we want as much power savings as possible, so we
|
|
* choose the according
|
|
* sleep mode: SLEEP_MODE_PWR_DOWN
|
|
*
|
|
*/
|
|
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
|
|
|
|
sleep_enable(); // enables the sleep bit in the mcucr register
|
|
// so sleep is possible. just a safety pin
|
|
|
|
/* Now it is time to enable an interrupt. We do it here so an
|
|
* accidentally pushed interrupt button doesn't interrupt
|
|
* our running program. if you want to be able to run
|
|
* interrupt code besides the sleep function, place it in
|
|
* setup() for example.
|
|
*
|
|
* In the function call attachInterrupt(A, B, C)
|
|
* A can be either 0 or 1 for interrupts on pin 2 or 3.
|
|
*
|
|
* B Name of a function you want to execute at interrupt for A.
|
|
*
|
|
* C Trigger mode of the interrupt pin. can be:
|
|
* LOW a low level triggers
|
|
* CHANGE a change in level triggers
|
|
* RISING a rising edge of a level triggers
|
|
* FALLING a falling edge of a level triggers
|
|
*
|
|
* In all but the IDLE sleep modes only LOW can be used.
|
|
*/
|
|
|
|
attachInterrupt(0,wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function
|
|
// wakeUpNow when pin 2 gets LOW
|
|
|
|
sleep_mode(); // here the device is actually put to sleep!!
|
|
// THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
|
|
|
|
sleep_disable(); // first thing after waking from sleep:
|
|
// disable sleep...
|
|
detachInterrupt(0); // disables interrupt 0 on pin 2 so the
|
|
// wakeUpNow code will not be executed
|
|
// during normal running time.
|
|
|
|
}
|
|
|
|
void loop () {
|
|
|
|
|
|
//sleepNow(); // sleep function called here
|
|
|
|
|
|
}
|
|
|
|
|