This is simple code controlling a five servo robot arm with the serial monitor. This code works but I'm always looking for ways to improve.
#include <VarSpeedServo.h>
VarSpeedServo servo1;
VarSpeedServo servo2;
VarSpeedServo servo3;
VarSpeedServo servo4;
VarSpeedServo servo5;
int oneValue = 85;
int twoValue = 160;
int threeValue = 180;
int fourValue = 100;
int fiveValue = 150;
int goData = 0;
char receivedChar = 0;
void setup() {
servo1.attach(4);
servo2.attach(5);
servo3.attach(6);
servo4.attach(7);
servo5.attach(8);
servo1.write(85, 60);
servo3.write(110, 60, true);
servo2.write(160, 60, true);
servo3.write(180, 60, true);
servo4.write(100, 60);
servo5.write(150, 60);
Serial.begin(9600);
Serial.println("<Arduino is ready>");
Serial.print("oneValue");
Serial.println(oneValue);
Serial.print("twoValue");
Serial.println(twoValue);
Serial.print("threeValue");
Serial.println(threeValue);
Serial.print("fourValue");
Serial.println(fourValue);
Serial.print("fiveValue");
Serial.println(fiveValue);
}
void loop() {
if (Serial.available()) {
while (Serial.available()) {
receivedChar = Serial.read();
Serial.println(receivedChar);
goData = receivedChar;
Serial.print("goData");
Serial.println(goData);
}
if (goData == 122) { //z
oneValue += 5;
servo1.write(oneValue);
goData = 0;
Serial.print("goData");
Serial.println(goData);
Serial.print("oneValue");
Serial.println(oneValue);
}
if (goData == 120) { //x
oneValue -= 5;
servo1.write(oneValue);
goData = 0;
Serial.print("goData");
Serial.println(goData);
Serial.print("oneValue");
Serial.println(oneValue);
}
if (goData == 97) { //a
twoValue += 5;
servo2.write(twoValue);
goData = 0;
Serial.print("goData");
Serial.println(goData);
Serial.print("twoValue");
Serial.println(twoValue);
}
if (goData == 115) { //s
twoValue -= 5;
servo2.write(twoValue);
goData = 0;
Serial.print("goData");
Serial.println(goData);
Serial.print("twoValue");
Serial.println(twoValue);
}
if (goData == 113) { //q
threeValue += 5;
servo3.write(threeValue);
goData = 0;
Serial.print("goData");
Serial.println(goData);
Serial.print("threeValue");
Serial.println(threeValue);
}
if (goData == 119) { //w
threeValue -= 5;
servo3.write(threeValue);
goData = 0;
Serial.print("goData");
Serial.println(goData);
Serial.print("threeValue");
Serial.println(threeValue);
}
if (goData == 101) { //e
fourValue += 5;
servo4.write(fourValue);
goData = 0;
Serial.print("goData");
Serial.println(goData);
Serial.print("fourValue");
Serial.println(fourValue);
}
if (goData == 114) { //r
fourValue -= 5;
servo4.write(fourValue);
goData = 0;
Serial.print("goData");
Serial.println(goData);
Serial.print("fourValue");
Serial.println(fourValue);
}
if (goData == 100) { //d
fiveValue += 5;
servo5.write(fiveValue);
goData = 0;
Serial.print("goData");
Serial.println(goData);
Serial.print("fiveValue");
Serial.println(fiveValue);
}
if (goData == 102) { //f
fiveValue -= 5;
servo5.write(fiveValue);
goData = 0;
Serial.print("goData");
Serial.println(goData);
Serial.print("fiveValue");
Serial.println(fiveValue);
}
if (goData == 99) { //c
servo1.write(85, 60);
servo3.write(110, 60, true);
servo2.write(160, 60, true);
servo3.write(180, 60, true);
servo4.write(100, 60);
servo5.write(150, 60);
Serial.print("goData");
Serial.println(goData);
Serial.print("oneValue");
Serial.println(85);
Serial.print("twoValue");
Serial.println(160);
Serial.print("threeValue");
Serial.println(180);
Serial.print("fourValue");
Serial.println(100);
Serial.print("fiveValue");
Serial.println(150);
}
}
}