I'm curently playing with sleep modes. When I push the button, melody starts, push again go to deep down sleep and save 3v battery, push again wake up and melody starts again. But seems like it doesn't completely goes to sleep and can't wake up.
#include <OneButton.h>
#include <avr/sleep.h>
#include <avr/power.h>
#define cs6 1109
#define fs5 740
#define d6 1175
#define c6 1047
#define f5 698
#define b5 988
#define e5 659
#define bf5 932
#define ef5 622
#define b4 494
#define g5 784
#define led1 8
#define led2 7
const uint16_t halloween[] PROGMEM = { cs6, fs5, fs5, cs6, fs5, fs5, cs6, fs5, d6, fs5,
cs6, fs5, fs5, cs6, fs5, fs5, cs6, fs5, d6, fs5,
cs6, fs5, fs5, cs6, fs5, fs5, cs6, fs5, d6, fs5,
cs6, fs5, fs5, cs6, fs5, fs5, cs6, fs5, d6, fs5,
cs6, fs5, fs5, cs6, fs5, fs5, cs6, fs5, d6, fs5,
cs6, fs5, fs5, cs6, fs5, fs5, cs6, fs5, d6, fs5,
c6, f5, f5, c6, f5, f5, c6, f5, cs6, f5,
c6, f5, f5, c6, f5, f5, c6, f5, cs6, f5,
cs6, fs5, fs5, cs6, fs5, fs5, cs6, fs5, d6, fs5,
cs6, fs5, fs5, cs6, fs5, fs5, cs6, fs5, d6, fs5,
c6, f5, f5, c6, f5, f5, c6, f5, cs6, f5,
c6, f5, f5, c6, f5, f5, c6, f5, cs6, f5,
b5, e5, e5, b5, e5, e5, b5, e5, c6, e5,
b5, e5, e5, b5, e5, e5, b5, e5, c6, e5,
bf5, ef5, ef5, bf5, ef5, ef5, bf5, ef5, b5, ef5,
bf5, ef5, ef5, bf5, ef5, ef5, bf5, ef5, b5, ef5,
b5, e5, e5, b5, e5, e5, b5, e5, c6, e5,
b5, e5, e5, b5, e5, e5, b5, e5, c6, e5,
bf5, ef5, ef5, bf5, ef5, ef5, bf5, ef5, b5, ef5,
bf5, ef5, ef5, bf5, ef5, ef5, bf5, ef5, b5, ef5,
fs5, b4, b4, fs5, b4, b4, fs5, b4, g5, b4,
fs5, b4, b4, fs5, b4, b4, fs5, b4, g5, b4,
fs5, b4, b4, fs5, b4, b4, fs5, b4, g5, b4,
fs5, b4, b4, fs5, b4, b4, fs5, b4, g5, b4,
fs5, b4, b4, fs5, b4, b4, fs5, b4, g5, b4,
fs5, b4, b4, fs5, b4, b4, fs5, b4, g5, b4,
fs5, b4, b4, fs5, b4, b4, fs5, b4, g5, b4,
fs5, b4, b4, fs5, b4, b4, fs5, b4, g5, b4,
fs5, b4, b4, fs5, b4, b4, fs5, b4, g5, b4
};
typedef enum {
ACTION_OFF,
ACTION_ON
}
MyActions;
const int buzzer = PA1;
unsigned long previousMillis = 0;
unsigned long pauseBetweenNotes = 218;
int noteDurations = 6;
int thisNote=0;
int ledState = LOW;
OneButton button(10, true, true);
MyActions nextAction = ACTION_OFF; // no action when starting
void setup() {
pinMode(buzzer, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
attachInterrupt(digitalPinToInterrupt(10), wakeUpFromSleep, FALLING);
button.attachClick(myClickFunction);
}
void melody () {
unsigned long currentMillis = millis();
if (thisNote <= 290 && currentMillis - previousMillis >= pauseBetweenNotes)
{
previousMillis = currentMillis;
if (ledState == LOW) {
ledState = HIGH;
tone(buzzer, pgm_read_word(&halloween[thisNote]), 168);
pauseBetweenNotes = 167 * 0.65;
thisNote++;
}
else {
ledState = LOW;
}
digitalWrite(led1, ledState);
digitalWrite(led2, ledState);
}
}
void wakeUpFromSleep() {
// Rimuovi l'attivazione dell'interrupt per il pulsante
detachInterrupt(digitalPinToInterrupt(10));
sleep_disable();
power_all_enable();
}
void myClickFunction() {
if (nextAction == ACTION_OFF)
{
nextAction = ACTION_ON;
thisNote = 0;
pauseBetweenNotes = 0;
previousMillis = 0;
}
else
{
nextAction = ACTION_OFF;
digitalWrite(led1, LOW); // spegni i led
digitalWrite(led2, LOW);
noTone(buzzer);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_cpu();
}
}
void loop() {
button.tick();
if (nextAction == ACTION_OFF) {
wakeUpFromSleep();
}
else if (nextAction == ACTION_ON) {
melody();
}
}
attachInterrupt(digitalPinToInterrupt(10),cannot be fully understood without knowing which configuration of which board core you are using. – timemage Mar 12 '23 at 22:27attachInterruptsimply won't work with that pin. And why it won't wake with that pin. – timemage Mar 17 '23 at 23:39attachInterruptworks withINT#notPCINT#. These are different things. Importantly none of the PCINT will wake your chip from powerdown. – timemage Mar 19 '23 at 20:22