I am working on a simple code that uses 2 motion sensors. The first sensor lights a red led when it detects motion. The second sensor lights a blue led and it's supposed to trigger a relay that activates a spray switch on then off. The led lights are doing what they are supposed to do, but the relay is not. Everything is wired correctly and all components work. /*
Arduino with PIR motion sensor
For complete project details, visit: Arduino with PIR Motion Sensor | Random Nerd Tutorials
Modified by Rui Santos based on PIR sensor by Limor Fried
*/
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)
int ledblue = 10; // the pin that the LED is atteched to
int sensorblue = 5; // the pin that the sensor is atteched to
int stateblue = LOW; // by default, no motion detected
int valblue = 0; // variable to store the sensor status (value)
int spray = 8; // the pin that the spray bottle is atteched to
int valspray = 0; // by default, no motion detected
void setup() {
pinMode(led, OUTPUT); // initalize LED as an output
pinMode(sensor, INPUT); // initialize sensor as an input
pinMode(spray, OUTPUT); // initialize spray as an output
Serial.begin(9600); // initialize serial
pinMode(ledblue, OUTPUT); // initalize LED as an output
pinMode(sensorblue, 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(100); // delay 100 milliseconds
if (state == LOW) {
Serial.println("Motion detected!");
state = HIGH; // update variable state to HIGH
}
}
else {
digitalWrite(led, LOW); // turn LED OFF
delay(200); // delay 200 milliseconds
if (state == HIGH){
Serial.println("Motion stopped!");
state = LOW; // update variable state to LOW
}
}
valblue = digitalRead(sensorblue); // read sensor value
valspray = digitalRead(sensorblue);
if (valblue == HIGH) { // check if the sensor is HIGH
digitalWrite(ledblue, HIGH); // turn LED ON
digitalWrite(spray, HIGH); // turn spray ON
digitalWrite(spray, LOW); // turn spray ON
delay(100);
digitalWrite(spray, HIGH); // turn spray ON
digitalWrite(spray, LOW); // turn spray ON
delay(100); // delay 100 milliseconds
if (stateblue == LOW) {
Serial.println("Motion detected!");
stateblue = HIGH; // update variable state to HIGH
}
}
else {
digitalWrite(ledblue, LOW); // turn LED OFF
delay(200); // delay 200 milliseconds
if (state == HIGH){
Serial.println("Motion stopped!");
state = LOW; // update variable state to LOW
}
}
{
}
}``




