-
AuthorPosts
-
October 12, 2020 at 4:05 AM #15750timster311Participant
Ive been struggling with this for a couple days now, and i’m not sure if i am even going about this the right way.
The project is a 328p with a Motion sensor (hc-sr501) connected to pin-3(INT1) that sleeps during the day. (I have hardware inverted it so the Motion triggers LOW on the sensor output using a transistor connected to ground)It has two basic functions that I am struggling to accomplish:
1. Wake up every 4 hours by timer, transmit battery level, go back to sleep.
~is there an easy way to determine when we have woken up via timer vs INT?2. Wake up from interrupt-1, trigger alarm, go back to sleep.
I have been reading some answers here in the forum, and modified it to try and get it going on a much less complex sketch for troubleshooting.
`// **** INCLUDES *****
#include “LowPower.h”#define TIME_WAKEUP 2
#define PIN_WAKEUP_B 3volatile boolean wakeUpA = false;
volatile boolean wakeUpB = false;
volatile int sleepCounter;
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
pinMode(TIME_WAKEUP, INPUT_PULLUP);
pinMode(PIN_WAKEUP_B, INPUT_PULLUP);
digitalWrite(LED_BUILTIN, LOW);
Serial.begin(9600);
Serial.println(“Start”);
Serial.flush();
attachInterrupt(digitalPinToInterrupt(TIME_WAKEUP), timer, LOW);
attachInterrupt(digitalPinToInterrupt(PIN_WAKEUP_B), triggerB, LOW);
}
void loop()
{
Serial.print(“pin-2(INT0) = “);
Serial.println(wakeUpA);
Serial.print(“pin-3(INT1) = “);
Serial.println(wakeUpB);
Serial.println(“going to sleep!”);
Serial.flush();
attachInterrupt(digitalPinToInterrupt(TIME_WAKEUP), timer, LOW);
attachInterrupt(digitalPinToInterrupt(PIN_WAKEUP_B), triggerB, LOW);
for (sleepCounter = 2; sleepCounter > 0; sleepCounter–) // Count down until “sleepCounter” reaches zero (8 x 1800 = 14400 / 60 / 60 = 4-hours).
{
if(wakeUpB = false); // Continue Sleep Loop Until pirWake is true, or for-loop counts to zero
{
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); // Sleep for 8-Seconds, With Analog to digital converter off, & Brown out detection off. (7-micro Amps in sleep)
}
}
if(wakeUpA)
{
detachInterrupt(digitalPinToInterrupt(TIME_WAKEUP));
wakeUpA = false;Serial.println(“Timer wakup!”);
Serial.flush();
// LED should ON for 8s
digitalWrite(LED_BUILTIN, HIGH);
delay(4000);
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(PIN_WAKEUP_B, LOW);
}
if(sleepCounter > 0 & wakeUpB == true)
{
detachInterrupt(digitalPinToInterrupt(PIN_WAKEUP_B));
wakeUpB = false;
Serial.println(“Pin Interrupt!”);
Serial.flush();
// LED should ON for 2s
digitalWrite(LED_BUILTIN, HIGH);
delay(2500);
digitalWrite(LED_BUILTIN, LOW);}
}
void timer()
{
wakeUpA = true;
}void triggerB()
{
wakeUpB = true;
}October 14, 2020 at 11:57 PM #15754LIM PHANG MOHKeymasterI think you would need to set an extra global variable/semaphore in the WDT ISR to check whether it is woken up by the WDT. This has to be added in the LowPower library file. On top of that, I would advise putting into SLEEP_FOREVER first and test the other 2 wake up source. Why? There’s a minimum pulse width that they need to meet in order for the ISR to take place. Else it would wake up but not knowing what woken it up as the pulse might have gone high before it check them.
-
AuthorPosts
- You must be logged in to reply to this topic.