Go Down

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

Ginger_Conor

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);
 myservo.write(pos);
}
void loop()
{
 buttonState = digitalRead(buttonPin);
 buttonState2 = digitalRead(buttonPin2);
 if (buttonState == LOW) {
   myservo.write(pos);
   delay(15);
   pos = pos + 18;

 }
 else {

 }
 
 if (buttonState2 == LOW) {
   myservo.write(pos);
   delay(15);
   pos = pos - 18;

 }
 else {

 }

}



AWOL

#1
Sep 05, 2010, 10:00 pm Last Edit: Sep 05, 2010, 10:00 pm by AWOL Reason: 1
Quote
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.

PaulS

You also need pull-up or pull-down resistors with the switches.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy