ho preso un po' qua e un po la che vi sembra'
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>
volatile bool sveglio = false;
void setup() {
wdt_disable();
wdt_enable(WDTO_8S);
pinMode(2, INPUT_PULLUP); // pulsante quadro e risveglio
pinMode(5, INPUT_PULLUP); //pulsante avviamento
attachInterrupt(0, svegliati, LOW);
Serial.begin(115200);
pinMode(13, OUTPUT);
int A;
int B;
int C;
int D;
}
void svegliati() {
Serial.println("sveglio, quindi controllo le altre e valuto");
if (A<B) {
//se B e inferiore A mi sveglio
sveglio = true;
}
}
void enterSleep() {
Serial.print("Dormo....");
power_adc_disable(); /* Power down some things to save power */
power_spi_disable();
power_timer0_disable();
power_twi_disable();
set_sleep_mode(SLEEP_MODE_IDLE); /* sleep mode is set here */
cli();
/* so sleep is possible. just a safety pin */
sleep_enable(); /* enables the sleep bit in the mcucr register */
sei();
sleep_cpu(); /* here the device is actually put to sleep!! */
/* THE PROGRAM IS WOKEN BY TIMER1 ISR */
sleep_disable(); /* disable sleep */
power_all_enable(); /* restore all power */
}
void loop() {
if (C<D) {
//se D e inferiore A mi sveglio
sveglio = true;
}
if (sveglio == false){
//loop
Serial.println("LOOP");
flash();
}
if (sveglio == true) {
enterSleep(); // metto in sleep
while (1);
wdt_reset(); //resetto watchdog
}
}
void flash() {
if (digitalRead(6) == LOW) {
digitalWrite(13, HIGH);
}
else{
digitalWrite(13, LOW);
}
}