Arduino Control Multiple Servos thru Serial Monitor
// Multi Serial to Servo Position
// Vije Miller

/*
Type: Servo No. + Position
Example: 290 (Servo 2 Position 90)
10 to 1180 for Servo 1
20 to 2180 for Servo 2
30 to 3180 for Servo 3
40 to 4180 for Servo 4
50 to 5180 for Servo 5
*/

#include <Servo.h>

Servo one;
Servo two;
Servo three;
Servo four;
Servo five;

String input;
String also;
int also_int;

void setup() {
Serial.begin(9600);

// Assign Pins
one.attach(3);
two.attach(4);
three.attach(5);
four.attach(6);
five.attach(7);

one.write(90);
delay(500);
two.write(90);
delay(500);
three.write(90);
delay(500);
four.write(90);
delay(500);
five.write(90);
delay(500);
}

void loop() {
if (Serial.available() > 0)
{
input = Serial.readString();
also = input;

// Get Servo Number
input.remove(1);

// Get Position
also.remove(0, 1);
also_int = also.toInt();

Serial.print(input);
Serial.print(" ");
Serial.println(also_int);

if (input == "1") {
one.write(also_int);
} else if (input == "2") {
two.write(also_int);
} else if (input == "3") {
three.write(also_int);
} else if (input == "4") {
four.write(also_int);
} else if (input == "5") {
five.write(also_int);
}
delay(100);
}
}

@pkvi
"...may not meet professional standards."
12,168 miters
4 miters today
203 tenons
Subscribe