Pages: [1]   Go Down
Author Topic: Help with servo control  (Read 316 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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 {

  }

}


Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26632
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
the servo moves randoml

Have you connected the grounds together?
« Last Edit: September 05, 2010, 03:00:39 pm by AWOL » Logged

"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.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: