I have a project which needs to sleep most of the time, and when a button gets pressed, it does its thing which is producing a fading sound wave (using interrupts) until it's time to sleep again.
The goal is to reduce work load on the battery so the item can last as long as possible.
#include <avr/sleep.h>
void setup(){
/*givePin! This is the pin that will spark the waking up!*/
pinMode(givePin,INPUT);
noInterrupts();
set_sleep_mode(SLEEP_MODE_IDLE);
/*changes to timers registers*/
interrupts();
}
void loop(){
ampCurrentlyOn = digitalRead(givePin);
ampPulse = (ampCurrentlyOn && !ampPreviouslyOn) * 255 //Reset
+ !(ampCurrentlyOn && !ampPreviouslyOn) * ampPulse//Same
- bool(ampPulse); //Decay
ampPreviouslyOn = ampCurrentlyOn;
if(!ampPulse){//Once that value reaches 0 time to GOTO bed;
/*This! Is this what I need to do?*/
cli();
sleep_mode();
sei();
}
}
Am I doing in the right direction? Could any pin work? How do I get the chip to understand a pin set to digital HIGH to wake up from nap time?
Thank you!
attachInterrupt(digitalPinToInterrupt(givePin), [](){}, FALLING);
? givePin must be pin capable of external interrupt.(pin 2 or 3 on Uno/Nano) ([](){}
is empty anonymous function)cli
before sleep, or it will never wake up. I also think you need to callsleep_enable();
.sleep_mode()
takes care of callingsleep_enable()
.