I am working on this code to turn a servo when the PIR motion sensor detects no motion, but it is not working. Some symptoms include:
- Servo is turning extremely slowly
- PIR is not turning the servo
- Servo is turning to incorrect angles
I have tried test programs, and when I type in the serial monitor, the servo turns (slowly and inaccurately) (I coded the serial monitor to do this). I have input from the PIR sensor but it is constantly high, even after waiting 2 minutes for it to initialize. My code and schematic are shown below.
My code:
#include <Servo.h>
Servo servo;
void setup() {
// put your setup code here, to run once:
servo.attach(13);
pinMode(2, INPUT);
Serial.begin(9600);
Serial.println("Please wait");
delay(60000);
}
void ifMos() {
if (digitalRead(2) == HIGH) {
int USER = 270;
}
}
void loop() {
// put your main code here, to run repeatedly:
int USER = Serial.parseInt();
servo.write(USER);
Serial.println(USER);
ifMos();
delay(10000);
}
-
1firget about the servo for now ... write the simplest code that lights an LED when motion is detectedjsotola– jsotola2022年07月11日 19:38:30 +00:00Commented Jul 11, 2022 at 19:38
1 Answer 1
firget about the servo for now ... write the simplest code that lights an LED when motion is detected – -jsotola
It worked! Thank you so much. All I had to do was add the servo library, write to the servo and upload it. Only 21 lines of code! Final code:
#include <Servo.h>
Servo servo;
void setup() {
// put your setup code here, to run once:
pinMode(2, INPUT);
pinMode(3, OUTPUT);
servo.attach(13);
}
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(2) == HIGH) {
digitalWrite(3, HIGH);
servo.write(270);
}
else {
digitalWrite(3, LOW);
servo.write(0);
}
}
Thank you to everyone. This was my first time posting on stack exchange, and it was a huge success. Thank you again!