Hey, I’m very new to arduino so please forgive any rookie mistakes/ code formatting. Basically I have a PIR motion sensor, a servo motor, a push button, a relay and an LED.
When motion is detected, I want the servo to move 180 degrees then stop. The LED will then blink. After 5 button pushes, the LED will stop blinking and the servo will return to its initial position. The relay is there to disable the motion sensor after it has triggered the motor, and re-enable it afterwards.
I have my relay and servo connected to 5V on the arduino, with the button, LED and PIR sensor (and one pine of the relay) connected to digital pins (listed in the code). The servo moves fine an the LED works. I’m pretty sure I’ve got the wiring (at least between components) correct.
So far I’ve just got the servo moving 180 degrees and not doing much else. It doesn’t even seem to be triggered by the sensor, it moves a few seconds after I upload the code. .
I’ve attached and posted my code below.
Any help will be appreciated, thanks in advance!
#include <Servo.h>
Servo myservo;
const int pirinputPin = 2;
const int buttonPin = 6;
const int ledpin = 13;
int pos = 0; // variable to store the servo position
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int LastButtonState = 0; // previous state of the button
int pirState = LOW; // we start, assuming no motion detected
int relaypin = 10; // PIR connected to normally closed port (activates PIR sensor when not actuated)
int relaystate = LOW;
void setup() {
myservo.attach(9);
pinMode(13, OUTPUT);
pinMode(pirinputPin, INPUT);
pinMode(relaypin, OUTPUT);
digitalWrite (relaypin, LOW );
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop() {
// read PIR input value
if (digitalRead(pirinputPin) == HIGH) {
// there was previously no motion detected
if (pirState == LOW) {
Serial.println("Motion detected");
pirState = HIGH;
// PIR sensor disconnected (normally closed)
digitalWrite (relaypin, HIGH);
relaystate = HIGH;
}
}
if (pirState = HIGH && pos <=180); {
// goes from 0 degrees to 180 degrees
pos = pos+1;
myservo.write(pos);
delay(15);}
if (pos == 180) {
pirState= LOW;
// led blinks
digitalWrite(ledpin, HIGH);
delay(200);
digitalWrite(ledpin, LOW);
delay(200);}
else digitalWrite(ledpin, LOW);
// compare the buttonState to its previous state
if (buttonState != LastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH); {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
// Delay a little bit to avoid bouncing
delay(50);
}
}
// save the current state as the last state, for next time through the loop
LastButtonState = buttonState;
if (buttonPushCounter ==5 && pos >0) {
// goes from 0 degrees to 180 degrees in steps of 1 degree
pos = pos-1;
myservo.write(pos);
delay(15);
}
if (pos == 0 && relaystate == HIGH) {
digitalWrite (relaypin, LOW);
delay (10000);
relaystate = LOW;
}
}
PIR_Servo_.ino (2.14 KB)