Hey, I am new to Arduino and I am relatively good with coding, however, Arduino language is new to me. I am making a 'lamp' project where a PIR sensor turns on a LED when it detects motion. Easy, got that sorted. The issue is I don't know how to keep the LED on when someone is in front of it.
I want someone to walk into a room, walk past the PIR sensor and essentially the
LED or 'lamp' turns on and stays on until it doesn't detect motion anymore. So as long as someone is in range, not necessarily moving, the LED will stay on (if you go into a room and sit in bed and lay still if the PIR can see your heat signature it keeps the LED on until you leave the room).
This is my code, any advice would be appreciated!
int led = 13; // the pin that the LED is atteched to
int sensor = 2; // the pin that the sensor is atteched to
int state = LOW; // by default, no motion detected
int val = 0; // variable to store the sensor status (value)
void setup() {
pinMode(led, OUTPUT); // initalize LED as an output
pinMode(sensor, INPUT); // initialize sensor as an input
Serial.begin(9600); // initialize serial
}
void loop(){
val = digitalRead(sensor); // read sensor value
if (val == HIGH) { // check if the sensor is HIGH
digitalWrite(led, HIGH); // turn LED ON
delay(100000); // delay 1 minute
if (state == LOW) {
Serial.println("Motion detected!");
state = HIGH; // update variable state to HIGH
}
}
else {
digitalWrite(led, LOW); // turn LED OFF
delay(10); // delay 10 milliseconds
if (state == HIGH){
Serial.println("Motion stopped!");
state = LOW; // update variable state to LOW
}
}
}