Dormir y despertar Arduino con la misma interrupción externa

Así es.
Y luego de algunos cambios en el código que me pasó surbyte, anduvo como necesitaba.
Pongo aquí el código:

// **** INCLUDES *****
#include "LowPower.h"

// Use pin 2 as wake up pin
const int wakeUpPin = 2;
bool flag = false; // false No despierta true despierta
bool estado, estadoAnt = true;
unsigned long tiempo;

void wakeUp()
{
    // Just a handler for the pin interrupt.
}

void setup()
{

    Serial.begin(9600);
    // Configure wake up pin as input.
    // This will consumes few uA of current.
    pinMode(wakeUpPin, INPUT_PULLUP);   // el ejemplo venia con INPUT

    Serial.println("Sistema iniciado.");
}

void loop() 
{
    estado = digitalRead(wakeUpPin);
    if (estado != estadoAnt) {
        flag = !flag; 
        Serial.println(flag?"BOTON ON": "BOTON OFF");
        tiempo = millis();
    }
    estadoAnt = estado;

    // Allow wake up pin to trigger interrupt on low.
    if (flag) {  // si flag = HIGH entonces entro en sleep
      
        attachInterrupt(0, wakeUp, LOW); 
    
        // Enter power down state with ADC and BOD module disabled.
        // Wake up when wake up pin is low.
        Serial.println("Durmiendo...");
        Serial.flush();
        LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); 
        Serial.println("Despertando.");
        Serial.flush();
    }
    else {    
        // Disable external pin interrupt on wake up pin.
        if (millis() - tiempo > 1000) {
          detachInterrupt(0); 
        }
    }
    // haz lo que tengas que hacer acá
    // Ejemplo : Leer sensores, guardar datos en SD, transmitirlos
}

¡Muchas gracias, surbyte!

Jerónimo

1 Like