'C++ code for meArm arduino robot doesn't work

I'm having some issues with this code for meArm arduino robot. It should ask the user to input the values for the angles of the servo motors and rotate them, however it only does so the first 2 times and then stops working.
Is there any error in the code?

#include <Servo.h>

int angle1;
int angle2;
int angle3;

Servo s1;
Servo s2;
Servo s3;
Servo s4;

const int BUFFER_SIZE = 100;
char buf[BUFFER_SIZE];

void setup() {
  Serial.begin(9600);
  
  s1.attach(10);
  s2.attach(9);
  s3.attach(8);

  s1.write(90);
  s2.write(0);
  s3.write(0);
}

void loop() {
  Serial.println("Set the angle for s1 servo");
  while (Serial.available() == 0) {};
  angle1 = Serial.readBytesUntil('\n', buf, BUFFER_SIZE);

  Serial.println("Set the angle for s2 servo");
  while (Serial.available() == 0) {};
  angle2 = Serial.readBytesUntil('\n', buf, BUFFER_SIZE);

  Serial.println("Set the angle for s3 servo");
  while (Serial.available() == 0) {};
  angle3 = Serial.readBytesUntil('\n', buf, BUFFER_SIZE);

  ForwardKinematics(angle1, angle2, angle3);
}

/**
 * Rotates the servo motors by the given angles.
 * 
 * @param angle1 The angle by which the servo s1 is rotated.
 * @param angle2 The angle by which the servo s2 is rotated.
 * @param angle3 The angle by which the servo s3 is rotated.
 */
void ForwardKinematics(int angle1, int angle2, int angle3) {
  s1.write(angle1);
  s2.write(angle2);
  s3.write(angle3);
  delay(500);
}


Solution 1:[1]

Your biggest problem is,

Serial.readBytesUntil('\n', buf, BUFFER_SIZE);

will only return the number of characters read into the buffer, not the data. Not sure what you mean by it works the first two times. Do you mean it loops through 2 times, or does it just get data for the first two servos and not the third one. Anyways, below is a working code tested on a UNO.

#include <Servo.h>

int angle1;
int angle2;
int angle3;

Servo s1;
Servo s2;
Servo s3;
Servo s4;

String data = "";

const int BUFFER_SIZE = 100;
char buf[BUFFER_SIZE];

void setup() {
  Serial.begin(9600);
  
  s1.attach(10);
  s2.attach(9);
  s3.attach(8);

  s1.write(90);
  s2.write(0);
  s3.write(0);
}

void loop() {
  angle1 = 0;
  angle2 = 0;
  angle3 = 0;
  
  Serial.println("Set the angle for s1 servo");
  while (Serial.available() == 0) {};
  int rlen = Serial.readBytesUntil('\n', buf, BUFFER_SIZE);
  for(int i = 0; i < rlen; i++){
    data += buf[i];
  }
  angle1 = data.toInt();
  data = "";
  
  Serial.println("Set the angle for s2 servo");
  while (Serial.available() == 0) {};
  rlen = Serial.readBytesUntil('\n', buf, BUFFER_SIZE);
  for(int i = 0; i < rlen; i++){
    data += buf[i];
  }
  angle2 = data.toInt();
  data = "";
  
  Serial.println("Set the angle for s3 servo");
  while (Serial.available() == 0) {};
  rlen = Serial.readBytesUntil('\n', buf, BUFFER_SIZE);
  for(int i = 0; i < rlen; i++){
    data += buf[i];
  }
  angle3 = data.toInt();
  data = "";
  

  ForwardKinematics(angle1, angle2, angle3);
}

/**
 * Rotates the servo motors by the given angles.
 * 
 * @param angle1 The angle by which the servo s1 is rotated.
 * @param angle2 The angle by which the servo s2 is rotated.
 * @param angle3 The angle by which the servo s3 is rotated.
 */
void ForwardKinematics(int angle1, int angle2, int angle3) {
  s1.write(angle1);
  s2.write(angle2);
  s3.write(angle3);
  delay(500);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 edgar_wideman