0

I am unable to get a button on my arduino board to control a servo. I'd like the servo to rotate 90 degrees on button push, but it is unresponsive.

Is there a flaw in my code or is it wiring?

#include <Servo.h>  // servo library

const int button1Pin = 2;  // pushbutton 1 pin
Servo servo1; // servo control object


void setup()
{
  pinMode(button1Pin, INPUT);
  servo1.attach(9);

  // Set servo 1 to output?      
}


void loop()
{
  int button1State;
int position;

button1State = digitalRead(button1Pin);


if (button1State == LOW);
{
  servo1.write(90);  // move servo
}

}        
  • It might already start at 90 degrees. If you can, turn the motor (carefully!) and see if that helps. – Anonymous Penguin Jan 14 '15 at 3:09
1

The problem is that servo1.write(90) does not do what you think it does.

servo.write(angle) tells the servo to go to that position, not to move by that many degrees. What you need is to set one angle when the button is not pressed, and a different angle when the button is pressed.

Normal hobby servos can move about 180 degrees. Where 0 is depends on how the servo horn is positioned. Which direction of rotation is positive depends on the manufacturer.

try something like this to set one position when the button is pressed and a different one when it is not:

if (button1State == LOW);{
    servo1.write(45);  // move servo
}
else {
    servo1.write(135);
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.