Servo stuck in two positions

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.

Thank you!

#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]);
          }
        }
      }
     }
    
  }
  }
  }

Welcome to the forum

The majority of your code, especially the if/else cascade. Note that you set the servo positions to the angle indexed by the value of c

You could just do

eye1.write(pos[c]);
eye2.write(pos[c]);

and eliminate the if/else

1 Like

@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.

a7

1 Like

The diagram in your post does not show how you are powering the servos. Jitter is almost always due to an inadequate servo power supply.

The Arduino 5V output won't work, and trying to use it for motors and servos can damage the Arduino.

Serial() communication relies pins 0 and 1. Avoid using them for anything else if your program utilizes Serial().

Try other pin instead of pin 1.

1 Like

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!

Okay, 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?

Basically your current logic is
“Scan through all PIR’s in sequence. If a PIR is active, move servo to preset angle. Repeat this as fast as you can”

What will happen if more than one PIR is active? Remember that PIR’s will stay on for several seconds (even minutes).

You can easily add LEDs or prints that tell you when a PIR is active

https://youtu.be/UYFCcSADodg here's a video of the problem!

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.

a7

1 Like

ohhhh good point, I will do that!

THX.

If you wait long enough between simulated motion on one PIR until the next, the servos move gently to the commanded position.

So you need to react to the change in the PIR sensor, not the level.

So you react only when a sensor goes from no motion sensed to motion sensed.

For this you will use

   arduino state change detection

It's a hole thing, so I left it as search terms you might google to learn all about it.

There's an example in the IDE that kinda obscures it. I will try to find a simpler version that came out of another thread here.

a7

1 Like

@tanilani - you could do worse than start with this post and read the entire thread:

Just treat the PIRs as if they were push buttons and watch for them getting "pressed".

Maybe start with the low bar of getting just one PIR sensor to behave because you are looking for transitions, not levels.

HTH

a7

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.