2

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!

3
  • 2
    you search for something like attachInterrupt(digitalPinToInterrupt(givePin), [](){}, FALLING);? givePin must be pin capable of external interrupt.(pin 2 or 3 on Uno/Nano) ([](){} is empty anonymous function)
    – Juraj
    Commented Aug 30, 2020 at 14:58
  • 1
    You shouldn't call cli before sleep, or it will never wake up. I also think you need to call sleep_enable();.
    – Gerben
    Commented Aug 30, 2020 at 18:26
  • 2
    @Gerben: sleep_mode() takes care of calling sleep_enable(). Commented Aug 31, 2020 at 18:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.