Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #14505
    timotheus m
    Participant

    Hi,
    I can’t get running this code. Isn’t it possible to detect and use two different pins on a 328P with your library?
    My goal is to execute two different sleep mods as you can see in the following code.
    Thanks!

    // **** INCLUDES *****
    #include "LowPower.h"
    
    const int int_trigger_A = 2; // pd2 int0
    const int wakeUpPin = 3;     // pd3 int1
    
    boolean HANDLER_wakeUp = false;
    boolean HANDLER_int_trigger = false;
    
    void wakeUp()
    {
      HANDLER_wakeUp = true;
    }
    
    void int_trigger()
    {
      HANDLER_int_trigger = true;
    }
    
    void setup()
    {
      pinMode(wakeUpPin, INPUT);
      pinMode(int_trigger_A, INPUT);
      Serial.begin(9600);
      Serial.println("Start");
      delay(100);
    }
    
    void loop()
    {
      Serial.println("Loop");
      delay(500);
    
      attachInterrupt(0, int_trigger_A, FALLING);
      attachInterrupt(1, wakeUp, FALLING);// Wake up when wake up pin is low.
      if (HANDLER_int_trigger) {    LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); }
      if (HANDLER_wakeUp) {    LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF); }
      detachInterrupt(1); // Disable external pin interrupt on wake up pin.
      detachInterrupt(0);
      HANDLER_wakeUp = false;
      HANDLER_int_trigger = false;
    
      // Do something here
      // Example: Read sensor, data logging, data transmission.
    
      if (HANDLER_int_trigger) {
        Serial.println("int_trigger");
        delay(500);
      }
    
      if (HANDLER_wakeUp) {
        Serial.println("wakeUp");
        delay(500);
      }
    
    }
    #14510
    LIM PHANG MOH
    Keymaster

    Hi,
    Your code has only a fraction of time for the interrupt to happen as you enable the interrupt, check interrupt flag (which won’t happen), and detach interrupt. That won’t work.

    #14518
    timotheus m
    Participant

    HI,
    appreciate your reply. What do you mean there’s only a fraction of time?
    I used the example where it’s done as far as I can see the exact same way besides that I’m using two interrupts.
    What would you change to get it running?

    #14520
    LIM PHANG MOH
    Keymaster

    Your checking of the interrupt happens in between the enabling and disabling the interrupt regardless whether both of the sleep takes place. That doesn’t make sense. That is only us of time.

    #14522
    timotheus m
    Participant

    Ok, I see what you mean.
    I changed it to a different handler now..but still it doesnt work. When triggering interrupt 1 it even restarts and prints:
    Start
    Loop

    // **** INCLUDES *****
    #include "LowPower.h"
    
    const int int_trigger_A = 2; // pd2 int0
    const int wakeUpPin = 3;     // pd3 int1
    
    volatile boolean HANDLER_wakeUp = false;
    volatile boolean HANDLER_int_trigger = false;
    volatile int sleepmode = 1;
    long count;
    
    void wakeUp()
    {
      HANDLER_wakeUp = true;
    }
    
    void int_trigger()
    {
      HANDLER_int_trigger = true;
    }
    
    void setup()
    {
      pinMode(wakeUpPin, INPUT_PULLUP);
      pinMode(int_trigger_A, INPUT_PULLUP);
      Serial.begin(9600);
      Serial.println("Start");
      delay(100);
    }
    
    void loop()
    {
      Serial.println("Loop");
      delay(500);
    
      attachInterrupt(0, int_trigger_A, FALLING);
      attachInterrupt(1, wakeUp, FALLING);// Wake up when wake up pin is low.
      if (sleepmode == 1) {  delay(100); LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); }
      if (sleepmode == 2) {  delay(100);  LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF); }
      detachInterrupt(1); // Disable external pin interrupt on wake up pin.
      detachInterrupt(0);
      
      // Do something here
      // Example: Read sensor, data logging, data transmission.
      
      delay(100);
    
      if (HANDLER_int_trigger) {
        HANDLER_int_trigger = false;
        sleepmode = 1;
        Serial.println("int_trigger");
        delay(500);
      }
    
      if (HANDLER_wakeUp) {
        HANDLER_wakeUp = false;
        sleepmode = 1;
        Serial.println("wakeUp");
        delay(500);
      }
    
      if (digitalRead(int_trigger_A==LOW)) {
        sleepmode = 2;
        count++;
        Serial.println("count++");
      }
      else {
      Serial.println(count);
      delay(500);
      sleepmode = 1;
      }
      
      
    
    }
    #14524
    LIM PHANG MOH
    Keymaster

    If you look at the datasheet in the sleep mode section, during power down only level interrupt works for INT0 and INT1. So, if you change the FALLING to LOW, it should work. I did a simple experiment here:

    // **** INCLUDES *****
    #include "LowPower.h"
    
    #define PIN_WAKEUP_A 2
    #define PIN_WAKEUP_B 3
    
    volatile boolean wakeUpA = false;
    volatile boolean wakeUpB = false;
    
    void setup()
    {
      pinMode(LED_BUILTIN, OUTPUT);
      pinMode(PIN_WAKEUP_A, INPUT_PULLUP);
      pinMode(PIN_WAKEUP_B, INPUT_PULLUP);
      digitalWrite(LED_BUILTIN, LOW);
      Serial.begin(115200);
      Serial.println("Start");
      Serial.flush();
      attachInterrupt(digitalPinToInterrupt(PIN_WAKEUP_A), triggerA, LOW);
      attachInterrupt(digitalPinToInterrupt(PIN_WAKEUP_B), triggerB, LOW);  
    }
    void loop()
    {
      if (wakeUpA) 
      {
        detachInterrupt(digitalPinToInterrupt(PIN_WAKEUP_A)); 
        wakeUpA = false;  
        Serial.println("A");
        Serial.flush();
        // LED should ON for 8s
        digitalWrite(LED_BUILTIN, HIGH);
        LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); 
        digitalWrite(LED_BUILTIN, LOW);
        attachInterrupt(digitalPinToInterrupt(PIN_WAKEUP_A), triggerA, LOW);
      }
      if (wakeUpB) 
      {
        detachInterrupt(digitalPinToInterrupt(PIN_WAKEUP_B)); 
        wakeUpB = false;   
        Serial.println("B");
        Serial.flush();  
        // LED should ON for 2s
        digitalWrite(LED_BUILTIN, HIGH);  
        LowPower.powerDown(SLEEP_2S, ADC_OFF, BOD_OFF); 
        digitalWrite(LED_BUILTIN, LOW);
        attachInterrupt(digitalPinToInterrupt(PIN_WAKEUP_B), triggerB, LOW);
      }
    }
    void triggerA()
    {
      wakeUpA = true;
    }
    
    void triggerB()
    {
      wakeUpB = true;
    }
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.