/*
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 potpin = 0; // analog pin used to connect the potentiometer
//int val; // variable to read the value from the analog pin
//void setup() {
//myservo.attach(9); // attaches the servo on pin 9 to the servo object
//}
//void loop() {
//val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
//val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
//myservo.write(val); // sets the servo position according to the scaled value
//delay(15); // waits for the servo to get there
//}
#include <Servo.h>
Servo servo1;
Servo servo2;
int x_key = A1;
int y_key = A0;
int x_pos;
int y_pos;
int servo1_pin = 8;
int servo2_pin = 9;
int initial_position = 90;
int initial_position1 = 90;
void setup ( ) {
Serial.begin (9600) ;
servo1.attach (servo1_pin ) ;
servo2.attach (servo2_pin ) ;
servo1.write (initial_position);
servo2.write (initial_position1);
pinMode (x_key, INPUT) ;
pinMode (y_key, INPUT) ;
}
void loop ( ) {
x_pos = analogRead (x_key) ;
y_pos = analogRead (y_key) ;
if (x_pos < 300){
if (initial_position < 10)
{
}
else{
initial_position = initial_position - 20;
servo1.write ( initial_position ) ;
delay (100) ;
}
}
if (x_pos > 700){
if (initial_position > 180)
{
}
else{
initial_position = initial_position + 20;
servo1.write ( initial_position ) ;
delay (100) ;
}
}
if (y_pos < 300){
if (initial_position1 < 10) { } else{ initial_position1 = initial_position1 - 20; servo2.write ( initial_position1 ) ; delay (100) ; } } if (y_pos > 700){
if (initial_position1 > 180)
{
}
else{
initial_position1 = initial_position1 + 20;
servo2.write ( initial_position1 ) ;
delay (100) ;
}
}
}