I am making a project where a camera tracks your face. I have a program on my computer that sends the face coordinates to the Arduino through the serial port. The Arduino program drives two servos. One for the X coordinate and one for the Y coordinate. The code works but it is very slow. It takes about a second to update the servo. How would I make this code faster?
Here is the code:
include <Servo.h>
Servo x_servo;
Servo y_servo;
void setup() {
Serial.begin(9600);
x_servo.attach(9);
y_servo.attach(10);
x_servo.write(90);
y_servo.write(90);
}
void loop() {
if (Serial.available()) {
String data = Serial.readString();
int delimeter = data.indexOf(',');
int x = data.substring(0, delimeter).toInt();
int y = data.substring(delimeter + 1).toInt();
int x_pos = map(x, 0, 640, 0, 180);
int y_pos = map(y, 0, 480, 0, 180);
x_servo.write(x_pos);
y_servo.write(y_pos);
}
}