How can I do that?

Heey :slight_smile:

So, I am new to arduino and I recently have some problem figuring how to code the sequence I want to. I would like that my servo, which is a HS-311 (180degree), make a rotation of 60degree when I press on an interuptor and than go back to his initial point when I press that interuptor again. Obviously, that means the servo wouldn’t move after his 60degree rotation until I press the interuptor again. By the way I’m using an Arduino Uno. Soo, yeah my question is like is that possible? If so, how?


Yes, it's possible.

There are example sketches which show how to detect a button press. You can use a state variable to record which position the servo is in and change it to the other position each time a button press occurs.

First master the SWEEP tutorial.

Then modify the sketch to do what you want.

Code that may do what you want.

//zoomkat servo button toggle test 4-28-2012

#include <Servo.h>
int button = 5; //button pin, connect to ground to move servo
int press = 0;
Servo servo;
boolean toggle = true;

void setup()
  pinMode(button, INPUT); //arduino monitor pin state
  servo.attach(7); //pin for servo control signal
  digitalWrite(5, HIGH); //enable pullups to make pin high

void loop()
  press = digitalRead(button);
  if (press == LOW)
      toggle = !toggle;
      toggle = !toggle;
  delay(500);  //delay for debounce

Thank you :slight_smile:
The code is perfect…
So as I can understand, servo information cable is on digital pin 7, there is a wire connecting the button to Analog Pin 5, I can plug my voltage on the board-included 5V power output, and there is a wire completing the circuit from the other side of the button to ground (board-included just beside the 5V)…well it must be a bit confusing to read I’ll try to post a picture actually. So, yea it doesn’t work for the moment I must have done something wrong…If one of you could check my circuit and tell me what’s wrong with it that would be awesome :wink:

You don’t need to connect anything to an analog pin.

One side of the button should be connected to digital pin 5 and the other side to ground. The software causes digital pin 5 to be HIGH and when the button is pressed it should connect the pin to ground so that it reads LOW.

As it is you seem to have the analog pin connected to +5v which makes no sense even if it does no physical damage.

You really should have a separate power supply for the servo as the Arduino 5v can’t provide enough current. It may work for testing if there is no load on the servo. But the problem is that drawing too much current from the 5v pin causes other aspects of the Arduino to misbehave in ways that look like there may be software errors.


thanks, it's working perfectly :smiley:
you are a genius man...