Servo controlled by Button

Hi all,

I’m writing because I don’t get further here. I thought this task would be easier but it doesn’t works like expected.
Basically I try to tell a servo motor to turn 90degrees after the button was pushed.

This is my wiring setup:

http://picload.org/image/cgglwda/servo.jpg

And here’s my Arduino code (which I basically merged together from the servo and Button example):

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 

const int buttonPin = 2;     // the number of the pushbutton pin
//int buttonState = 0;         // variable for reading the pushbutton status
const int outputPin = 9; // servo signal
int pos = 90;    // variable to store the servo position 

void setup() 
{ 
  // Serial.begin(9600); // for sending data to serial monitor
  pinMode(ledPin, OUTPUT);  
  myservo.attach(outputPin);  // attaches the servo on pin 9 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 (buttonState == HIGH) {     
    myservo.write(pos);         // set the servo position
  } 
}

Any clue what I’m doing wrong?
Thank for your help!

danny_123:
Basically I try to tell a servo motor to turn 90degrees after the button was pushed.

Sounds like you want it to turn by 90 degrees, but a servo.write(90) tells it to go to 90 degrees. So if that's where it is already, it's not going to move.

Trouble is, a servo has no way of telling you where it is: you have to keep track of that. So if "pos" is where it is at the moment, then you need to do a write(pos+90) say, or newpos=pos+90 and write(newpos)

You have a pull-up on the button so it's always HIGH
change to:
if (buttonState == LOW) {
myservo.write(pos); // set the servo position
There is only one position, so nothing happens unless the servo is not at 90.
add this after above:
if (buttonState == HIGH) {
myservo.write(10); // set the servo position
That way it will move.

assuming you are using the internal pull-up resistor on your button you can use the state change as a way to signal the servo to change positions:

untested…

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 

const int buttonPin = 2;     // the number of the pushbutton pin
//int buttonState = 0;         // variable for reading the pushbutton status
const int outputPin = 9; // servo signal
int pos = 90;    // variable to store the servo position 
int ledPin = 13;
int oldButtonState;
//
void setup() 
{ 
  // Serial.begin(9600); // for sending data to serial monitor
  pinMode(ledPin, OUTPUT);  
  myservo.attach(outputPin);  // attaches the servo on pin 9 to the servo object 
  pinMode(buttonPin, INPUT_PULLUP); // initialize the pushbutton pin as an input:
} 
//
void loop() 
{ 
  // read the state of the pushbutton value:
  int buttonState = digitalRead(buttonPin);
  if (buttonState == LOW) 
  {  
    if (oldButtonState == HIGH)
    {
      myservo.write(pos);         // set the servo position
      digitalWrite(ledPin, pos=90? HIGH:LOW);
      delay(50);
      pos = (90-pos);  // toggle between 90 and 0 degrees each button push
    }
  }
  oldButtonState = buttonState;
}

Thanks for all your help so far — I'm testing it out right now.

Basic servo/button code.

//zoomkat servo button test 7-30-2011
//Powering a servo from the arduino usually *DOES NOT WORK*.

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
  }
  else {
    servo1.write(20);
  }
}

zoomkat:
Basic servo/button code.

//zoomkat servo button test 7-30-2011

//Powering a servo from the arduino usually DOES NOT WORK.

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
  }
  else {
    servo1.write(20);
  }
}

I imagine it would be kind of flakey due to the bounciness of the typical button switch, unless you de=bounced this with hardware.

interesting that we went through the trouble of declaring button1 and then threw that to the wind:

  pinMode(button1, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high

another approach would be:

  pinMode(button1, INPUT_PULLUP);
  servo1.attach(7);

zoomkat:
Basic servo/button code.

//zoomkat servo button test 7-30-2011

//Powering a servo from the arduino usually DOES NOT WORK.

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
 pinMode(button1, INPUT);
 servo1.attach(7);
 digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
 press1 = digitalRead(button1);
 if (press1 == LOW)
 {
   servo1.write(160);
 }
 else {
   servo1.write(20);
 }
}

This code makes sense to me but if i press the button there’s only a weak twitch inside the servo.
Of course I noticed your comment //Powering a servo from the arduino usually DOES NOT WORK.
but with other sketches there was a movement - even though not really manageable…

besides, i really don’t know why this isn’t working. Theoretically it should be more or less easy.

EDIT*

Okay, my Mac crashed now. I think i need an external power supply and better wiring cables. I have the constant feeling that mine is causing a defective contact…

his code makes sense to me but if i press the button there's only a weak twitch inside the servo.

Well, you really need to define what type of button pushes you will be using, there are push/hold, and push/release. My simple code is designed for push/hold such that the moves to the 160 position when the button is held down, and returns to the 20 position when the button is released. If you want the servo to change positions on each push/release, then you need some toggle code.

Okay. Actually I think it should be doable like this. In the end, even if there are better ways to do this, it's just for prototyping purposes.

I have 4 buttons and each button should have a defined position at 90, 180, 270 and 360 Deg.

You have a problem. A standard servo will only move about 180 degrees.

steinie44:
You have a problem. A standard servo will only move about 180 degrees.

I modified my standard servo with the help of this video: How to: Mod servo for continous rotation - YouTube
I don't know yet if it really works but the wheel is turning continuously.

That’s the same way I do it.
Problem is after you modify it, the only thing you can do is run forward or backward and control speed. You can no longer control the angle movement.

Yeah, I have read this some minutes ago. I was not aware of this. Well, in this case I will probably use 2 servos..