I'm trying to control 2 servos with 2 pots. I thought I could just amend the tutorial code by adding in the extra pot on a1 and extra servo on d8 (am using a Nano).
#include <Servo.h>
Servo myservo; // create servo object to control a servo
Servo myservo_2;
int potpin = 0; // analog pin used to connect the potentiometer
int potpin_2 = 1;
int val; // variable to read the value from the analog pin
int val_2;
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo_2.attach(8);
}
void loop() {
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val_2 = analogRead(potpin_2);
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
val_2 = map(val_2, 0, 1023, 0, 180);
myservo.write(val); // sets the servo position according to the scaled value
myservo_2.write(val_2);
delay(15); // waits for the servo to get there
}
...but the servos only respond to one of the pots (on a0) & both move to the same position regardless of what value is on a1.
Am I missing something really obvious? Why is the value on a1 ignored & why is the value mapped from a0 sent to both d8 & d9?
I have swapped out the pot on a1 just in case it was faulty but I get the same behavior.