# Help with Servo Code

So what im trying to do is get my servo to go from 0 to 90 when i press the button and stop. Then when i press it again it goes from 90 back to 0 then stop. basically every time i press the button it goes back and forth from 0 and 90.

Whats going on now is when i press the button it goes 0 to 90 but stays there and when i press the button again it jumps to 0 but then goes back to 90 again. Attached is the code im using and layout with a video.

Thank you for any help i can get with this.

``````#include <Servo.h>

// Set digital pin numbers:
const int servoPin = 8;  // The number of the Servo pin
const int buttonPin = 9;  // 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(8);  // 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:

if (directionState == 0){
//The button is pushed
if (buttonState == HIGH) {
directionState = 1;// The direction for the servo is clockwise

// goes from 0 degrees to 90 degrees in steps of 1 degree
for(pos = 0; pos < 90; pos=pos+1)
{
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 90 degrees to 0 degrees in steps of 1 degree
// for (pos = 90; pos>=1; pos=pos–1)
{
myservo.write(pos);  // tell servo to go to position in variable ‘pos’
delay(15);  // waits 15ms for the servo to reach the position
}
}
}
}
``````

Range_Finder.ino (1.59 KB)

Well, look at those for loops. It's doing exactly what you told it to do.

EDITED: I misread the code and have changed this response since rereading it.

Hang on I think I misunderstood your complaint. Let’s step through your code for a second.

Let’s say we’re at the top of loop and you’ve got the button pressed. So direction is 0 so we go into that if, and the button is pressed so it sets direction to 1 and sweeps up to 90. Then it exits and loop is over. Now loop repeats. You’ve still got the button down. direction is 1 so it goes into the second if and yes the button is pressed and that has no for loop it just says to write it to pos which is still 90. SO we’re great.

Now loop repeats again and now direction is 0 so we go into the first if. The button is pressed so we run the for loop. It says to go first to 0 and then sweep up to 90. So that’s what it does, jump to 0 and then sweep up to 90.

Im not very good with coding. What would i need to do to make it just go back and forth. Where if i hit the button it goes to 90 then stops and when i hit it again it goes to 0 and stops?

See the state change example about how to react once per push of a button.

i must be missing something. to me when i read the code it looks like it should be doing what i want it to do but its not. im not seeing the sweep part of it at all. im kind of a begginer at this.

Go over reply 2 again. I walked you through it piece by piece. I don't know how to break it down any simpler.

So i made an adjustment to the code and it seems to be working. but now when i go to the 90deg position the servo vibrates like its still trying to turn even though its not. is that normal? when it goes back to 0 it stops.

``````#include <Servo.h>

// Set digital pin numbers:
const int servoPin = 8;  // The number of the Servo pin
const int buttonPin = 9;  // 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(8);  // 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:

if (directionState == 0){
//The button is pushed
if (buttonState == HIGH) {
directionState = 1;// The direction for the servo is clockwise

// goes from 0 degrees to 90 degrees in steps of 1 degree
for(pos = 0; pos < 90; pos=pos+1)
{
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 90 degrees to 0 degrees in steps of 1 degree
for (pos = 90; pos>=1; pos=pos-1)
{
myservo.write(pos);  // tell servo to go to position in variable ‘pos’
delay(15);  // waits 15ms for the servo to reach the position
}
}
}
}
``````