Dormir y despertar Arduino con la misma interrupción externa

Lo que ocurre es que son practicamente dos cosas distintas.
Cuando lo pones a dormir lo usas como pulsador.
Cuando lo despiertas usas una función de sleep que permite despertarlo por interrupción de modo que no son la misma cosa cuando pudieran estar en el mismo pulsador.

Se puede controlar la interrupción para que no lo despierte cuando lo estas durmiendo?

Este es el ejemplo de la Librería LowPower

Se llama asi: powerDownWakeExternalInterrupt.ino o sea lo despierta por interrupción externa

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

// Use pin 2 as wake up pin
const int wakeUpPin = 2;

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

void setup()
{
    // Configure wake up pin as input.
    // This will consumes few uA of current.
    pinMode(wakeUpPin, INPUT);   
}

void loop() 
{
    // Allow wake up pin to trigger interrupt on low.
    attachInterrupt(0, wakeUp, LOW);
    
    // Enter power down state with ADC and BOD module disabled.
    // Wake up when wake up pin is low.
    LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); 
    
    // Disable external pin interrupt on wake up pin.
    detachInterrupt(0); 
    
    // Do something here
    // Example: Read sensor, data logging, data transmission.
}

Tal vez podriamos contener el estado de sleep asi

// **** 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 = false;

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

void setup()
{
    // Configure wake up pin as input.
    // This will consumes few uA of current.
    pinMode(wakeUpPin, INPUT_PULLUP);   // el ejemplo venia con INPUT
}

void loop() 
{
    estado = digitalRead(wakeUpPin);
    if (estado != estadoAnt)
        flag = !flag; 
    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.
        LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); 
    }
    else {    
        // Disable external pin interrupt on wake up pin.
        detachInterrupt(0); 
    }
    // haz lo que tengas que hacer acá
    // Ejemplo : Leer sensores, guardar datos en SD, transmitirlos
}
1 Like