Hello, I am relatively new to Arduino and I have made a servo motor that is controlled by a potentiometer, I wanted to add another to make a sort of belt, however, I wanted to know how could I add another servo to my code, I already tried copy and pasting my code but just changing the variable and defining another version of "potVal" and "potPin". My second motor is connected to analog 1 "A1" and digital port 10 "~10".
#include <Servo.h>
Servo myServo;
int const potPin = A0; //analog pin A
int potVal; //stores value of pot
int angle = 0; //stores position of servo
void setup() {
myServo.attach(9); //tells board which pin the servo is on
Serial.begin(9600);
} //end of setup()
void loop() {
potVal = analogRead(potPin);
Serial.print("potVal: ");
Serial.print(potVal);
angle = map(potVal, 0, 1023, 0, 179); //angle is the pot value re-scaled to 0-179
Serial.print(", angle: ");
myServo.write(angle);
delay(15);
} //end of loop()
The Code I tried that didn't work
#include <Servo.h>
Servo myServo;
int const potPin = A0; //analog pin A
int const potPi = A1; //analog pin A
int potVal; //stores value of pot
int potVa; //stores value of pot
int angle = 0; //stores position of servo
void setup() {
myServo.attach(9); //tells board which pin the servo is on
Serial.begin(9600);
myServo.attach(10); //tells board which pin the servo is on
Serial.begin(9600);
} //end of setup()
void loop() {
potVal = analogRead(potPin);
Serial.print("potVal: ");
Serial.print(potVal);
angle = map(potVal, 0, 1023, 0, 179); //angle is the pot value re-scaled to 0-179
Serial.print(", angle: ");
myServo.write(angle);
delay(15);
potVa = analogRead(potPi);
Serial.print("potVa: ");
Serial.print(potVa);
angle = map(potVa, 0, 1023, 0, 179); //angle is the pot value re-scaled to 0-179
Serial.print(", angle: ");
myServo.write(angle);
delay(15);
} //end of loop()