How to cotnroll multiple servos at once

so i found this code, which you can control servo with potentiometers, save the movement and play it. the only problem is that its only made for 1 servo and i need a code that i will controll multiple servos with mutiple potenciometers and save their position for robotic arm. Heres the code:

#include <Servo.h>
Servo servo1; // testing for a single potentiometer & servo combination

int AnalogOut = 0;

int NewAnalogOut = 0;

int pin_Button = 2; //attach button to PWM pin 13

int ledPin = 3; //status LED when push button is on

int pin_Button_State = 0;

int pin_Button_State_Last = 0;

int storage[200]; // # of positions for recording

int storage_loc = 0;

int recording = 0;

void setup() {

Serial.begin(9600); //turn on serial monitor

servo1.attach(7); //attach servo to PWM pin 7

pinMode(pin_Button, INPUT); //button for playback

pinMode(ledPin, OUTPUT); //LED is an output

}

void loop() {

if (recording == 0) { //not recording state

int sensorValue = analogRead(A0); //reading potentiometer at analog pin A0

NewAnalogOut = map(sensorValue, 0, 1023, 80, 0); //only has 80 degrees of rotation translated

if (abs(NewAnalogOut - AnalogOut) > 2) { //abs is for absolute, recording speed with value

AnalogOut = NewAnalogOut;

servo1.write(AnalogOut);

}

}

delay(1);

if ( recording == 1) { //recording turned on

digitalWrite(ledPin, HIGH); //turn on LED, to signify recording

recording = 1;

if (storage_loc < 200) {

int sensorValue = analogRead(A0);

NewAnalogOut = map(sensorValue, 0, 1023, 80, 0);

storage[storage_loc] = NewAnalogOut;

servo1.write(NewAnalogOut);

delay(100);

Serial.println(storage[storage_loc]);

storage_loc++;

}

} else if (recording > 1) { //recording playback, hit reset on arduino to stop playback

storage_loc = 0;

digitalWrite(ledPin, LOW);

while (1 == 1) {

if (storage_loc < 200 and storage[storage_loc] != 666) {

servo1.write(storage[storage_loc]);

storage_loc ++;

delay(100);

Serial.println(storage_loc);

Serial.println(storage[storage_loc]);

}

else{

storage_loc = 0;

}

}

}

pin_Button_State = digitalRead(pin_Button);

if (pin_Button_State != pin_Button_State_Last) {

if (pin_Button_State == HIGH) {

recording++;

if (recording == 2) {

storage[storage_loc] = 666;
}
}
delay(50); //number of “frames” or speed it is recording at, due to number of delays in the loop
}
pin_Button_State_Last = pin_Button_State;
}

so i would appreciate it if somebody could tell me how to change the code to do that. thank you

Make an array of Servo objects, and a same-sized array of analogue pin numbers.
Make “storage” two dimensional, to match the number of servos, and reduce the type to “byte” (which means you won’t be able to store the Number Of The Beast in a “storage” element, so change your end-marker to something else)

Please remember to use code tags when posting code.

int pin_Button = 2; //attach button to PWM pin 13

Please try to keep code and comments consistent

recording = 1;

“recording” already is 1, or you wouldn’t be here; this is pointless.

could you show me in code what i need to change, because i did some changes myself and code is completely wrong. im not rly good at programing but i need to make this for a school project

I can’t see the changes you made, so it’s very difficult to help you.

Don’t forget the code tags this time

well here is the edited code that i did, but i fell like i messed it up hard…and like i said thats not my code i copied it from a website so i cant do anything about tags he used sorry

#include <Servo.h>
Servo servo1; // testing for a single potentiometer & servo combination
Servo servo2;

int AnalogOut = 0;

int NewAnalogOut = 0;

int AnalogOut1 = 0;

int NewAnalogOut1 = 0;

int pin_Button = 2; //attach button to PWM pin 13

int ledPin = 3; //status LED when push button is on

int pin_Button_State = 0;

int pin_Button_State_Last = 0;

int storage[200]; // # of positions for recording

int storage_loc = 0;

int recording = 0;

void setup() {

Serial.begin(9600); //turn on serial monitor

servo1.attach(5); //attach servo to PWM pin 7
servo2.attach(6);

pinMode(pin_Button, INPUT); //button for playback

pinMode(ledPin, OUTPUT); //LED is an output

}

void loop() {

if (recording == 0) { //not recording state

int sensorValue = analogRead(A0); //reading potentiometer at analog pin A0
NewAnalogOut = map(sensorValue, 0, 1023, 80, 0); //only has 80 degrees of rotation translated
if (abs(NewAnalogOut - AnalogOut) > 2) { //abs is for absolute, recording speed with value
AnalogOut = NewAnalogOut;
servo1.write(AnalogOut);

int sensorValue1 = analogRead(A1); //reading potentiometer at analog pin A0
NewAnalogOut1 = map(sensorValue1, 0, 1023, 80, 0); //only has 80 degrees of rotation translated
if (abs(NewAnalogOut1 - AnalogOut1) > 2) { //abs is for absolute, recording speed with value
AnalogOut1 = NewAnalogOut1;
servo2.write(AnalogOut1);

}

}

delay(1);

if ( recording == 1) { //recording turned on

digitalWrite(ledPin, HIGH); //turn on LED, to signify recording

recording = 1;

if (storage_loc < 200) {

int sensorValue = analogRead(A0);
NewAnalogOut = map(sensorValue, 0, 1023, 80, 0);
storage[storage_loc] = NewAnalogOut;
servo1.write(NewAnalogOut);

int sensorValue1 = analogRead(A1);
NewAnalogOut1 = map(sensorValue1, 0, 1023, 80, 0);
storage[storage_loc] = NewAnalogOut1;
servo2.write(NewAnalogOut1);

delay(100);

Serial.println(storage[storage_loc]);

storage_loc++;

}

} else if (recording > 1) { //recording playback, hit reset on arduino to stop playback

storage_loc = 0;

digitalWrite(ledPin, LOW);

while (1 == 1) {

if (storage_loc < 200 and storage[storage_loc] != 666) {

servo1.write(storage[storage_loc]);

servo2.write(storage[storage_loc]);

storage_loc ++;

delay(100);

Serial.println(storage_loc);

Serial.println(storage[storage_loc]);

}

else{

storage_loc = 0;

}

}

}

pin_Button_State = digitalRead(pin_Button);

if (pin_Button_State != pin_Button_State_Last) {

if (pin_Button_State == HIGH) {

recording++;

if (recording == 2) {

storage[storage_loc] = 666;

}

}

delay(50); //number of “frames” or speed it is recording at, due to number of delays in the loop

}

pin_Button_State_Last = pin_Button_State;

}
}

You can’t put two positions into one storage location, so that’s not going to work.

If you’re going to want to do this for a significant number of servos and pots, you’re going to have to embrace arrays and for loops - see my notes above about the size and type of your storage array; you’ll run out of memory if you’re not careful.

Please, remember to use code tags when posting code.

The easier you make it to read and copy the code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here