Go Down

Topic: Help with servo control (Read 534 times) previous topic - next topic


i have a servo and 2 push buttons.
i would like the servo to rotate clockwise with one button and anti-clockwise with the other at increments of around 10 degres i have this following code however the servo moves randomly. any help will be much appreciated

Code: [Select]
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position
int buttonPin = 2;
int buttonState = 0;         // variable for reading the pushbutton status
int buttonPin2 = 3;
int buttonState2 = 0;         // variable for reading the pushbutton status

void setup()
 myservo.attach(9);  // attaches the servo on pin 9 to the servo object
 pinMode(buttonPin, INPUT);
 pinMode(buttonPin2, INPUT);
void loop()
 buttonState = digitalRead(buttonPin);
 buttonState2 = digitalRead(buttonPin2);
 if (buttonState == LOW) {
   pos = pos + 18;

 else {

 if (buttonState2 == LOW) {
   pos = pos - 18;

 else {




Sep 05, 2010, 10:00 pm Last Edit: Sep 05, 2010, 10:00 pm by AWOL Reason: 1
the servo moves randoml

Have you connected the grounds together?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.


You also need pull-up or pull-down resistors with the switches.
The art of getting good answers lies in asking good questions.

Go Up