Servo problems

Hello,

i can not get my servo to work at all, (and yes the wire is correct)
when i upload my program the servo moves around. Can someone explain that??

everything else runs just fine, LED lights, my counter, but for the love god i cant get the servo to work.
i tried running the servo on it’s in another program and same thing happens, moves around when
uploading but nothing besides that.

please help!!!

#include <Servo.h>

  Servo servo;
  
  const int MaxNum = 5;      // number times beam can be tripped
  const int LEDPIN = 6;      // Red LED 
  const int LEDPIN2 = 8;     // Green LED 
  const int SENSORPIN = 4;   // Servo Pin
  const int RTime = 150;     // delay time for Red LED
  const int GTime = 150;     // delay time for Green LED
  const int Stime = 500;     // delay time for Servo
  const int Angle = 60;      // max angle Servo moves
  
  int sensorState = 0;       // variable for reading the pushbutton status
 
void setup() {
  servo.attach(2);             
  pinMode(LEDPIN2, OUTPUT);      
  pinMode(LEDPIN, OUTPUT);         
  pinMode(SENSORPIN, INPUT);      
  digitalWrite(SENSORPIN, HIGH); 
}

int count = 0; 
void loop(){   
  sensorState = digitalRead(SENSORPIN); // check if the sensor beam is broken
  if (count < MaxNum) {
    digitalWrite(LEDPIN2, HIGH);
    if (sensorState == LOW) {
      for (int x = 0; x <= 5; x ++) { // loop for LED.
        digitalWrite(LEDPIN2, HIGH);
        delay(GTime);
        digitalWrite(LEDPIN2, LOW);
        delay(GTime);
      }
      // runs Servo motor.         
      for (int pos = 0; pos < Angle; pos++) {
        servo.write(pos);
      } 
      for (int pos = Angle; pos > 0; pos--) {
        servo.write(0);
      }
      count++;
    } 
   else { // if beam is detected
      servo.detach();
    } 
  } else if (count == MaxNum) { // if the max count has been reached
   digitalWrite(LEDPIN, HIGH);
   servo.detach();
   }
}

Hi,

How have you got button wired?
How are you powering your servo?
The 5V form the Arduino will not be able to supply the current they need.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

Hi, again,

Have you written code that ONLY concerns the servo and moving it?

    for (int pos = 0; pos < Angle; pos++) {
        servo.write(pos);
      } 
      for (int pos = Angle; pos > 0; pos--) {
        servo.write(0);
      }

Look at your second for loop. servo.write(0)… servowrite(pos)
Tom… :slight_smile:

yes i am powering the servo with the 5V from the arduino, but the LEDs are powered from the 3.3V. It is that still not enough??

i dont understand your second comment.

thanks

Hi,
Your second for loop has servo.write(0); shouldn't it be servo.write(pos);

Also do a CTRL -T to auto format your code to indent the for and if command.

Tom.. :slight_smile:

Hi,
Remove the servo.detach();
because you are not attaching it again to make it sweep again.

Tom… :slight_smile:

what would you recommend putting instead of the detach?
my goal for those sections to have the servo hold its position.

thanks for ctrl T tip, i was not aware of that :slight_smile:

Hi,
The servo will hold the last servo.write command sent, the output pin that signals the servo is continuous output.
The command is used to just change the angle.

If you detach, if an external force moves the servo, it will not try to keep your position.

Tom... :slight_smile: