Hello @blh64 , @qubits-us , @kolaha and the Arduino forum,
blh64 has a good point. The motion detector trigger was not in the code I posted because I was trying to bypass the PIR (passive infrared sensor) so as to simplify the code and just tell the code to turn the LED on and off.
The code called 'sketch to turn on light w PIR and Arduino 230617'
is copied herewith below and includes the PIR trigger.
Also attached is a schematic that was breadboarded, tested and operates as designed.
PIR to Arduino to LED 230617
The 'sketch to turn on light w PIR and Arduino 230617' relies on the motion detector (HC-SR501) to determine how long the LED will stay on.
The PIR has two pots on it. One to adjust the proximity sensitivity and
the other to adjust how long the PIR sends out it 3.3 v signal ( millisecods to about five minutes).
The context is a series of lights that would react as a walker descends
or ascends a staircase
Staircase system
The design was to use the timer pots on the PIRs to turn on a light
for about twenty seconds. But the PIR timer pots are pretty crude and
the variance from PIR to PIR is a large range so it is difficult to get
the lights to stay on for uniform amount of time.
The code posted by qubits-us was very helpful but in the end the code posted kolaha was installed. kolaha's excellent sketch is posted after
'sketch to turn on light w PIR and Arduino 230617' and is called
'Sketch to turn on light w/o delay using PIR and Arduino 230618'.
The ....'w/o delay'.... code, kolaha's code, from post #4, had a couple of 'not defined in this scope' variable name typos and I think I figured out what was meant. But if I may, kolaha (or anybody else who is willing to dive into it.) please accept a request to look at
'Sketch to turn on light w/o delay using PIR and Arduino 230618' ,
posted below, to see if I made the right adjustments.
I think I did make the right adjustments because the breadboard is operating pretty much as designed. The only deviation is,
I think, the time that the LED should stay on is determined by
const int intervalPIR = 5000;
That is the LED should stay on for five seconds. But the time that the
LED stays on has been timed w a stopwatch a half dozen times and the
LED stays on for closer to eight seconds which maybe the sum
of the intervalPIR and the time that the PIR sends the signal.
Working on eliminating the timer at the PIR and only using the
time set in the sketch to determine how long the light stays on.
I think just using the sketch to time how long the LED stays on will provide a much more precise operation.
Finally, the '...w/o delay...' sketch operates one light by one PIR. The next step is to expand the number of lights to six total and create two use cases, one for ascending and one for descending where the use case fired off would be determined by whether the walker approaches from above or below.
Thanks.
Allen Pitts
/**************************************************************
sketch to turn on light w PIR and Arduino 230617
*******************************************************/
// ++++ LED pin numbers ledPinX Output 2-10 ++++
int led1 = 12;
int inputPinA1 = 11;
// ++++ PIR State pirStateXX = LOW ++++
int pirStateA1= LOW; // 220504 acp
// ++++ Input status variable valXX A5 - 12, 13
int valA1= 0; // 220504 acp
// the setup function runs once when you press reset or power the board
void setup() {
// +++++ declare ledPinXX numbers as OUTPUT ++++
pinMode(led1, OUTPUT); // declare LED1 as output
// ++++ declare inputPinXX numbers as INPUT ++++
pinMode(inputPinA1, INPUT); // declare sensor1 as input 220504 acp
}
// the loop function runs continuously
void loop(){
//Connect PIR inputPinA1 (pin 11) to '+LED1-' (Pin12)
valA1 = digitalRead(inputPinA1); // read input value
if (valA1 == HIGH) { // check if the input is HIGH
digitalWrite(led1, HIGH); // turn LED ON
} else {
digitalWrite(led1, LOW); // turn LED OFF
if (pirStateA1 == HIGH){
pirStateA1 = LOW;
}
}
}
/**************************************************************end of sketch to to turn on light w PIR and Arduino 230617
*******************************************************/
/********************************************************
Sketch to turn on light w/o delay using PIR and Arduino 230618
*************************************************************/
const int ledPin = 12; // the number of the LED pin
const int inputPin = 11; // the number of the PIR input
const int intervalPIR = 5000; // interval to blink further after PIR no more active
const int intervalMillis = 1000; // interval between change light
unsigned long previousPIR_Millis = 0; // will store last time PIR was active HIGH
unsigned long previousBlinkMillis = 0; // will store last time LED was updated
bool BlinkTime = false;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(inputPin, INPUT_PULLUP);
}
void loop() {
if (digitalRead(inputPin)) {
BlinkTime = true;
previousPIR_Millis = millis();
}
if (BlinkTime) {
if (millis() - previousBlinkMillis >= intervalMillis) {
previousPIR_Millis += intervalPIR;
digitalWrite(ledPin, !digitalRead(ledPin));
}
if (!digitalRead(inputPin) && millis() - previousPIR_Millis >= intervalPIR){
BlinkTime = false;
digitalWrite(ledPin,LOW);
}
}
}
/********************************************************
end of Sketch to turn on light w/o delay using PIR and Arduino 230618
*************************************************************/