I added some code for a servo motor. So the led's work properly, the servo motor works properly but the relay just goes click click when activated
/*
Arduino with PIR motion sensor
For complete project details, visit: http://RandomNerdTutorials.com/pirsensor
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
// Include the Servo library
#include <Servo.h>
// Declare the Servo pin
int servoPin = 6;
// Create a servo object
Servo Servo1;
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
// We need to attach the servo to the used pin number
Servo1.attach(servoPin);
}
void loop(){
val = digitalRead(sensor); // read sensor value
if (val == HIGH) { // check if the sensor is HIGH
digitalWrite(led, HIGH); // turn LED ON
// Make servo go to 0 degrees
Servo1.write(0);
delay(1000);
digitalWrite(spray, HIGH); // turn spray ON
digitalWrite(spray, LOW); // turn spray ON
delay(100); // delay 100 milliseconds
if (state == LOW) {
Serial.println("Motion detected!");
state = HIGH; // update variable state to HIGH
// Make servo go to 90 degrees
Servo1.write(90);
delay(1000);
}
}
else {
digitalWrite(led, LOW); // turn LED OFF
delay(200); // delay 200 milliseconds
digitalWrite(spray, HIGH); // turn spray ON
digitalWrite(spray, LOW); // turn spray ON
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
// Make servo go to 180 degrees
Servo1.write(180);
delay(1000);
digitalWrite(ledblue, HIGH); // turn LED ON
digitalWrite(spray, HIGH); // turn spray ON
delay(10);
digitalWrite(spray, LOW); // turn spray ON
delay(10);
delay(100);
digitalWrite(spray, HIGH); // turn spray ON
delay(10);
digitalWrite(spray, LOW); // turn spray ON
delay(10);
delay(100); // delay 100 milliseconds
if (stateblue == LOW) {
Serial.println("Motion detected!");
stateblue = HIGH; // update variable state to HIGH
// Make servo go to 90 degrees
Servo1.write(90);
delay(1000);
}
}
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
}
}
{
}
}


