Hello Arduino Form,
Working on turning on several lights with a motion detector (PIR HC-SR501).
Had been relying on the variable pot on the PIR to turn the lights off when
the PIR stopped sending a signal but
the pots are pretty crude and it would be good if the lights stayed on
approx. the same amount of time. Some ideas on how to time several
events were found at:
Timing without using Delay()
The sketch copied herewith below marked '/* Simple MillsDelay, One LED */' works and
is tested on the PCB.
The next step is to have the PIR trigger turn the light on and have MillisDelay function turn the light off.
The sketch copied below marked '/* Simple MillsDelay, One LED */' works and
is tested normal on the PCB.
The sketch copied below the 'Simple' sketch marked 'MIllisDelay_1_PIR_1_LED_Pin2_230624' is an attempt to add reading a value from the PIR input and then using the
'delayRunning1 && ((millis() - delayStart1' constuct to turn on the light to turn
on the light for the time spec;d in DELAYTIME1.
Have tried several versions. The one posted below seems to just ignore
the PIR because it just blinks the light on and off.
If someone could give me an idea about how to how to make the PIR operate with
the MillisDelay function it would be most appreciated.
Thanks.
Allen Pitts
/* Simple MillsDelay, One LED */
#include <millisDelay.h>
int led1 = 2;
unsigned long DELAY_TIME1 = 1000; // 1.5 sec
unsigned long delayStart1 = 0; // the time the delay started
bool delayRunning1 = false; // true if still waiting for delay to finish
bool ledOn1 = false; // keep track of the led state
void setup() {
pinMode(led1, OUTPUT); // initialize the digital pin as an output.
digitalWrite(led1, LOW); // turn led off
ledOn1 = false;
// start delay
delayStart1 = millis();
delayRunning1 = true;
}
void checkToggleLed1() { // led task
// check if delay has timed out
if (delayRunning1 && ((millis() - delayStart1) >= DELAY_TIME1)) {
delayStart1 += DELAY_TIME1; // this prevents drift in the delays
// toggle the led
ledOn1 = !ledOn1;
if (ledOn1) {
digitalWrite(led1, HIGH); // turn led on
} else {
digitalWrite(led1, LOW); // turn led off
}
}
}
void loop() {
checkToggleLed1(); // call to toggle led based on timer
}
MIllisDelay_1_PIR_1_LED_Pin2_230624*
/***************************************************
MIllisDelay_1_PIR_1_LED_Pin2_230624
code and ideas on using the millis() function instead of delay() come from
https://www.forward.com.au/pfod/ArduinoProgramming/TimingDelaysInArduino.html
https://roboticsbackend.com/how-to-do-multitasking-with-arduino/
https://www.instructables.com/Simple-Multi-tasking-in-Arduino-on-Any-Board/
*/
#include <millisDelay.h>
int led1 = 2;
int PIRA5 = A5;
int valA5 = 0;
unsigned long DELAY_TIME1 = 1000; // 1.5 sec
unsigned long delayStart1 = 0; // the time the delay started
bool delayRunning1 = false; // true if still waiting for delay to finish
bool ledOn1 = false; // keep track of the led state
void setup() {
pinMode(led1, OUTPUT); // initialize the digital pin as an output.
digitalWrite(led1, LOW); // turn led off
ledOn1 = false;
pinMode(PIRA5, INPUT);
// start delay
delayStart1 = millis();
delayRunning1 = true;
}
void checkToggleLed1() { // led task
// check if delay has timed out
valA5 = digitalRead(PIRA5); // read input value
if (valA5 == HIGH) { // check if the input is HIGH
if (delayRunning1 && ((millis() - delayStart1) >= DELAY_TIME1)) {
delayStart1 += DELAY_TIME1; // this prevents drift in the delays
// toggle the led
ledOn1 = !ledOn1;
if (ledOn1) {
digitalWrite(led1, HIGH); // turn led on
} else {
digitalWrite(led1, LOW); // turn led off
}
}
}
}
void loop() {
checkToggleLed1(); // call to toggle led based on timer
}