Correct sketch but sketch fails to operate on arduino

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
       }
     }
   }
 }

Edited to remove the red text (that is, converted it to black). Don't bother doing that, world war 3 hasn't broken out.

in steps of 1 degree

??

pos =pos+90

         delay(15);  // waits 15ms for the servo to reach the position

So, you are giving it quite a long time, eh?

essentially whatever is coded there, is what should be done to the servo, disregarding the annotations on the right.

       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
       }

Or, more simply myservo.write(90); plus whatever delay you calculate from the spec for the servo.

nggtristan:
essentially whatever is coded there, is what should be done to the servo, disregarding the annotations on the right.

In this case it looks like it is the code that should be disregarded.

We all occasionally make changes to code and forget to update the comments (which is a good idea for writing code that does not need comments). However when the code and the comments are seriously at variance it is always a good idea to ask yourself which is more correct in case you made a silly change to the code (which we all do occasionally - or more frequently).

...R

Robin2:

nggtristan:
essentially whatever is coded there, is what should be done to the servo, disregarding the annotations on the right.

In this case it looks like it is the code that should be disregarded.

We all occasionally make changes to code and forget to update the comments (which is a good idea for writing code that does not need comments). However when the code and the comments are seriously at variance it is always a good idea to ask yourself which is more correct in case you made a silly change to the code (which we all do occasionally - or more frequently).

...R

the only thing that i have changed in that whole code is the timing that i have messed around with and the position of the servo. nothing else

Hi, nggtristan, did you note post #2 and have you fixed your problem?

Tom........ :slight_smile:

yes i haven noticed post #2, the sketch had worked that way in the past. Its just acting up for some reason now and i havent changed the sketch since.

Hi, so you now have

pos =pos+1

Tom...... :slight_smile: