I'm trying to make a program that will allow the servos to move to a specific position depending on which PIR sensor is triggered. However, for 3-5 seconds after one sensor is triggered, if I trigger another sensor, the servo will jitter back and forth between the two positions assigned to the two sensors. I've tried attaching a command to turn off the PIR sensor immediately after reading the value of pir sensor c, but the problem still occurs.
I'm not sure if the servos are meant to stick at a position for some amount of time after writing it to a certain position ? If anyone can help me figure out the problem going on that would be great.
#include <Servo.h>
Servo eye1; //declaring servo objects
Servo eye2;
int eyeR = 11; // choose the pins for the servos
int eyeL = 10;
int pos[] = {0, 45, 90, 135, 180}; // position variables
int rest = 90;
int ledF = 7; //declaring led for for loopo
int ledR = 8; //led for each repetition
int pirSensors[5] = {1,2,3,4,5}; // creates array for pir sensors
int val = 0;
void setup() {
eye1.attach(eyeR); //kinda eyeR and eyeL r kinda unnecessary
eye2.attach(eyeL);
pinMode(eyeR, OUTPUT); // declare servos as output
pinMode(eyeL, OUTPUT);
pinMode(ledF, OUTPUT); //declare leds as output
pinMode(ledR, OUTPUT);
for (int a = 0; a<5; a++) // declare sensors as input
{
pinMode(pirSensors[a], INPUT);
}
Serial.begin(9600);
}
void loop(){
for (int c = 0; c<5; c++)
{
val = digitalRead(pirSensors[c]); //read input
digitalWrite(pirSensors[c],LOW); //reset sensor
if (val == HIGH) { // check if the input is HIGH
if (c == 0){ //then moves
eye1.write(pos[0]);
eye2.write(pos[0]);
}
else {
if (c == 1){
eye1.write(pos[1]);
eye2.write(pos[1]);
}
else {
if (c == 2){
eye1.write(pos[2]);
eye2.write(pos[2]);
}
else {
if (c == 3){
eye1.write(pos[3]);
eye2.write(pos[3]);
}
else {
eye1.write(pos[4]);
eye2.write(pos[4]);
}
}
}
}
}
}
}
@tanilani servos will stay where you put them if no external applied force is enough to make them slip.
Add serial printing to the for statement in your loop() function. You may see that your sensors are not being handled correctly.
Alternately or even in addition you could replace the sensors with pushbuttons just to see if your logic works for the way you think the sensors will (eventualy) be informing the flow.
That is nonsense, remove it. Even if it did "turn off the PIR", that would be a Bad Idea, as PIR sensors typically work better after a brief warm up period then no interruptions in the delivery of power.
Or:
Hook one PIR up and have it just directly control one LED. Simplest possible sketch - read the sensor and write the value to the LED. Place the PIR in circumstances similar to the final deployment, and watch how (and what) it senses.
You will need some ind of filtering or conditioning, as well as a careful mechanical layout, to get five PIRs to do the kind of thing it looks like you are attemtping.
I'm only running this digitally through wokwi, so I think it's a software problem? I'll make sure to add batteries in the physical build, though. Thanks!
Thanks- I honestly haven't even implemented all this into hardware yet, I'm programming this virtually on wokwi until I can actually get my hands on materials for the project. Therefore, I don't think this is an issue with the PIR's sensing?
Good. So it will be even easier to test your logic using push buttons instead of the PIR.
And the PIR in the wokwi has some fiddly adjustments. Check the document page by clicking the question mark when you select a PIR in the diagram.
When you do get real PIRs, test them in the manner I described, as they are one thing that may operate quite different to how they behave when in the perfect world of the simulator.
If you haven't, use the Share button in wokwi and post a link to your project.