Arduino pin interrupts

    #include "PinChangeInterrupt.h" // https://github.com/NicoHood/PinChangeInterrupt this one

    const int drvPin = A2;
    const int DONEPin = A1;


    void setup() {
      // set pin to input with a pullup, DONE to output
      pinMode(drvPin, INPUT_PULLUP);
      pinMode(DONEPin, OUTPUT);

      Serial.begin(9600);

      attachPCINT(digitalPinToPCINT(drvPin), do_smth, FALLING); // DRV goes LOW, when TPL time has elapsed

      //ENABLE SLEEP - this enables the sleep mode
      SMCR |= (1 << 2); //power down mode
      SMCR |= 1;//enable sleep

    }


    bool sendData = true; // tracks if you need to send data
    void do_smth(void) {

      //say tahat you need to send data
      //nothing else goes here!
      sendData = true;
    }

    void loop() {
      if (sendData)
      {
        //here you read data, process it and send!
        
        Serial.println("Send data now");

        /*

            LORA SEND HERE

        */

        sendData = false; //so do_smth() can alter state;
        
        digitalWrite(DONEPin, HIGH); // reset TPL timer;
        delay(50); // have to test this on real Hardware
        digitalWrite(DONEPin, LOW); //so that TPL doesn't go to sleep at time it wakes.
        
        goToSleep();
      }
    }

    void goToSleep()
    {
      Serial.println("GOING TO SLEEP");
      Serial.flush();
      
      //BOD DISABLE - this must be called right before the __asm__ sleep instruction
      MCUCR |= (3 << 5); //set both BODS and BODSE at the same time
      MCUCR = (MCUCR & ~(1 << 5)) | (1 << 6); //then set the BODS bit and clear the BODSE bit at the same time
      __asm__  __volatile__("sleep");//in line assembler to go to sleep
    }

I don’t have HW, but try this out.

1 Like