/*
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 myservo; // create servo object to control a servo
int analogPin = A0;
int val = 0; // variable to read the value from the analog pin
int pos = 0;
void setup() {
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.write(0);
}
void loop() {
int servo_state = myservo.read();
val = analogRead(analogPin);
if (myservo.read() == 0 and val == 1023) {
Serial.println("sale");
Serial.println(myservo.read());
Serial.println(val);
for (int i = 0; i <= 180; i++) {
myservo.write(i); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
}
else if (myservo.read() == 180 and val < 1023) {
Serial.println("scende");
Serial.println(myservo.read());
Serial.println(val);
for (int i = 180; i >= 0; i--) {
myservo.write(i); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
}
}