/*
Controlling a servo position using a potentiometer (variable resistor)
by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
modified on 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Knob
*/
#include <Servo.h>
Servo motor; // create servo object to control a servo
int val; // variable to read the value from the analog pin
int botao = 7; //entrada do botão
int velocidade = 20;
void setup() {
motor.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(botao, INPUT_PULLUP);
}
void loop() { // reads the value of the potentiometer (value between 0 and 1023)
val = map(analogRead(A0), 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
motor.write(val); // sets the servo position according to the scaled value
delay(velocidade);
if(digitalRead(botao)==LOW){
for (int k=val; k>=0; k--){
motor.write(k);
delay(velocidade);
}
for (int k=0; k>=180; k++){
motor.write(k);
delay(velocidade);
}
for (int k=180; k>=val; k--){
motor.write(k);
delay(velocidade);
}
} // waits for the servo to get there
}