I an trying to find a code for this sensor as it senses motion to turn on LED for a delay of 5-6 seconds.
The sensor seems to have a slow response as I need it to be almost instantaneous to turn on the LED.
I will be using multiple sensors with there own LED. Can this be done?
I am using Arduino Mega 2560.
Here is what I tried...
int led = 42; // the pin that the LED is atteched to
int sensor = 52; // the pin that the sensor is atteched to
int state = HIGH; // by default, no motion detected
int val = 1; // 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 OFF
delay(100); // turn LED OFF
if (state == LOW) {
Serial.println("Motion detected!");
state = LOW; // update variable state to LOW
}
}
else {
digitalWrite(led, LOW); // turn LED On
delay(7000);
if (state == HIGH){
Serial.println("Motion stopped!");
state = HIGH; // update variable state to HIGH
}
}
Thanks for any help on this!