Good Day everyone!!! Seems I have a problem controlling five servos in my project, everytime i control one servo the other servo also moves, I have already assigned each servo to each PWM pins and the five potentiometers that controls them on different analog inputs, i will post my code and i am open for any suggestions if there is any mistakes or code to change, also i am using a 5V DC-DC buck boost connected in a 9V Fly Back power supply. Thank you!!!
#include <Servo.h>
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
const int numReadings = 10;
const int numReadings2 = 10;
const int numReadings3 = 10;
const int numReadings4 = 10;
const int numReadings5 = 10;
int readings[numReadings];
int readings2[numReadings2];
int readings3[numReadings3];
int readings4[numReadings4];
int readings5[numReadings5];
int readIndex = 0;
int readIndex2 = 0;
int readIndex3 = 0;
int readIndex4 = 0;
int readIndex5 = 0;
int total = 0;
int total2 = 0;
int total3 = 0;
int total4 = 0;
int total5 = 0;
const int inputPin = A0;
const int inputPin2 = A1;
const int inputPin3 = A2;
const int inputPin4 = A3;
const int inputPin5 = A4;
int sensorValue = 0;
int sensorValue2 = 0;
int sensorValue3 = 0;
int sensorValue4 = 0;
int sensorValue5 = 0;
void setup() {
servo1.attach(3);
servo2.attach(5);
servo3.attach(7);
servo4.attach(9);
servo5.attach(11);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0; }
for (int thisReading2 = 0; thisReading2 < numReadings2; thisReading2++) {
readings2[thisReading2] = 0; }
for (int thisReading3 = 0; thisReading3 < numReadings3; thisReading3++) {
readings3[thisReading3] = 0; }
for (int thisReading4 = 0; thisReading4 < numReadings4; thisReading4++) {
readings4[thisReading4] = 0; }
for (int thisReading5 = 0; thisReading5 < numReadings5; thisReading5++) {
readings5[thisReading5] = 0; }
}
void loop() {
total = total - readings[readIndex];
total2 = total2 - readings2[readIndex2];
total3 = total3 - readings3[readIndex3];
total4 = total4 - readings4[readIndex4];
total5 = total5 - readings5[readIndex5];
readings[readIndex] = analogRead(inputPin);
readings2[readIndex2] = analogRead(inputPin2);
readings3[readIndex3] = analogRead(inputPin3);
readings4[readIndex4] = analogRead(inputPin4);
readings5[readIndex5] = analogRead(inputPin5);
total = total + readings[readIndex];
total2 = total2 + readings2[readIndex2];
total3 = total3 + readings3[readIndex3];
total4 = total4 + readings4[readIndex4];
total5 = total5 + readings5[readIndex5];
readIndex = readIndex + 1;
readIndex2 = readIndex2 + 1;
readIndex3 = readIndex3 + 1;
readIndex4 = readIndex4 + 1;
readIndex5 = readIndex5 + 1;
if (readIndex >= numReadings) {
readIndex = 0;
}
if (readIndex2 >= numReadings2) {
readIndex2 = 0;
}
if (readIndex3 >= numReadings3) {
readIndex3 = 0;
}
if (readIndex4 >= numReadings4) {
readIndex4 = 0;
}
if (readIndex5 >= numReadings5) {
readIndex5 = 0;
}
sensorValue = total / numReadings;
sensorValue2 = total2 / numReadings2;
sensorValue3 = total3 / numReadings3;
sensorValue4 = total4 / numReadings4;
sensorValue5 = total5 / numReadings5;
sensorValue = map(sensorValue, 0, 1023, 0, 170);
sensorValue2 = map(sensorValue2, 0, 1023, 0, 170);
sensorValue3 = map(sensorValue3, 0, 1023, 0, 170);
sensorValue4 = map(sensorValue4, 0, 1023, 0, 170);
sensorValue5 = map(sensorValue5, 0, 1023, 0, 170);
servo1.write(sensorValue);
servo2.write(sensorValue2);
servo3.write(sensorValue3);
servo4.write(sensorValue4);
servo5.write(sensorValue5);
}