I'm building a mailbox sensor and Arduino sleep code part keeps failing. This is my first attempt to put board to sleep with Reed sensor interrupt awake. I found peaces of code from net and this forum and tried to combine them without success.
I tested first Adafruit_SleepyDog.h -library but it doesn't support interrupts or does it?
My board: adafruit-feather-32u4
Board has 4 interrupts. Do I need to add pullup resistor to reed sensor?
I tested Reed sensor on all interrupts and it behaves the same(Not working right...) I want board to sleep when magnet is connected.
#0 / RX - GPIO #0, also receive (input) pin for Serial1 and Interrupt #2
#1 / TX - GPIO #1, also transmit (output) pin for Serial1 and Interrupt #3
#2 / SDA - GPIO #2, also the I2C (Wire) data pin. There's no pull up on this pin by default so when using with I2C, you may need a 2.2K-10K pullup. Also Interrupt #1
#3 / SCL - GPIO #3, also the I2C (Wire) clock pin. There's no pull up on this pin by default so when using with I2C, you may need a 2.2K-10K pullup. Can also do PWM output and act as Interrupt #0.
Code has attachInterrupt(digitalPinToInterrupt(interruptPin), ReedInterrupt, HIGH); -part. Is the HIGH-mode correct here?
// LORA
#include <LoRa.h>
//Sleep
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/interrupt.h>
// Define the pins used by the LoRa module
const int csPin = 8; // LoRa radio chip select
const int resetPin = 4; // LoRa radio reset
const int irqPin = 7; // Must be a hardware interrupt pin
// Reed switch and led
const byte ledPin = 5;
const byte interruptPin = 3;
void setup() {
Serial.begin(115200);
pinMode(interruptPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH); // Debugging LED is on while board is not sleeping
delay(2000);
// Setup LoRa module
LoRa.setPins(csPin, resetPin, irqPin);
// Start LoRa module at local frequency
// 433E6 for Asia
// 866E6 for Europe
// 915E6 for North America
if (!LoRa.begin(866E6)) {
Serial.println("Starting LoRa failed!");
while (1)
;
}
else {
Serial.println("Starting LoRa OK!");
}
}
void ReedInterrupt(void) {
// Wake up section
detachInterrupt(digitalPinToInterrupt(interruptPin));
}
void enterSleep(void){
/*
attachInterrupt(digitalPinToInterrupt(pin), ISR, mode) (recommended)
attachInterrupt(interrupt, ISR, mode) (not recommended)
attachInterrupt(pin, ISR, mode) (Not recommended. Additionally, this syntax only works on Arduino SAMD Boards, Uno WiFi Rev2, Due, and 101.)
interrupt: the number of the interrupt. Allowed data types: int.
pin: the Arduino pin number.
ISR: the ISR to call when the interrupt occurs; this function must take no parameters and return nothing. This function is sometimes referred to as an interrupt service routine.
mode: defines when the interrupt should be triggered. Four constants are predefined as valid values:
LOW to trigger the interrupt whenever the pin is low,
CHANGE to trigger the interrupt whenever the pin changes value
RISING to trigger when the pin goes from low to high,
FALLING for when the pin goes from high to low.
The Due, Zero and MKR1000 boards allow also:
HIGH to trigger the interrupt whenever the pin is high.
*/
attachInterrupt(digitalPinToInterrupt(interruptPin), ReedInterrupt, HIGH);
delay(100);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
LoRa.sleep(); // put the radio to sleep
digitalWrite(ledPin, LOW);
delay(2000);
sleep_enable();
if (digitalRead(interruptPin)){
sleep_mode();
}
// Function continues from here after sleep
sleep_disable();
digitalWrite(ledPin, HIGH);
}
void loop() {
// <Some LORA related stuff here>
delay(5000);
enterSleep();
}