/*
My first Arduino program/ This controls two servos by using a pot.
An LED lights when the pot's value changes, and each servo rotates
in opposite directions.
*/
#include <Servo.h>
Servo myservo;
Servo myservo2;
int potpin = 7; // pin # (analog) that potentiometer is on
int s1pin = 2; // pin # (pwm) that servo 1 is on
int s2pin = 3; // pin # (pwm) that servo 2 is on
int val = 0; // variable to store last value from potentiometer
int pval = 0;
void setup() {
myservo.attach(s1pin); // wake up servo 1
myservo2.attach(s2pin); // wake up servo 2
}
void loop() {
/*Read pot value, scale to range 0-180.*/
val = map(analogRead(potpin),0,1023,0,180);
myservo.write(val);
myservo2.write(180-val);
/*Light up LED if pot value is new.*/
if (val != pval) {
digitalWrite(7,HIGH);
delay(5);
digitalWrite(7,LOW);
pval = val;
}
/*A final pause.*/
delay(15);
}