So im working on this little project with my arduino eleven and I require to move the servo 90 degrees from its starting position. I have about 4 servos connected in parallel but i took them all out. Now only 1 is connected. It is grounded to the rail on a breadboard and signaled to the breadboard which goes to the arduino, also the servo is running on its own power. For some reason when I upload the sketch, the servo just jerks back and fourth to 90 degrees. Originally its suppose go straight to its position and stay there for a couple seconds and go back to its starting position until the button is pressed again. Can anyone suggest what I can do or should do to fix this problem? Also the sketch that i upload seems to be correct as i verify it before i upload it.
#include <Servo.h>
// Set digital pin numbers:
const int servoPin = 9; // The number of the Servo pin
const int buttonPin = 8; // The number of the Pushbutton pin
int buttonState = 0; // Variable for reading the pushbutton status
int directionState = 0; // Variable for reading direction of the servo
Servo myservo; // Create servo object to control a servo
int pos = 0; // Variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 8 to the servo object
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
if (directionState == 0){
//The button is pushed
if (buttonState == HIGH) {
directionState = 1;// The direction for the servo is clockwise
// goes from 0 degrees to 180 degrees in steps of 1 degree
for(pos= 0; pos <=90; pos =pos+90)
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
} else if (directionState == 1) {
// The button is pushed
if (buttonState == HIGH) {
directionState = 0; // The direction for the servo is anti-clockwise
// goes from 180 degrees to 0 degrees in steps of 1 degree
for(pos= 0; pos <=90; pos =pos+90)
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
}
}