Le code C++ pour le robot meArm arduino ne fonctionne pas

J'ai quelques problèmes avec ce code pour le robot arduino meArm. Il doit demander à l'utilisateur d'entrer les valeurs des angles des servomoteurs et de les faire tourner, mais il ne le fait que les 2 premières fois, puis s'arrête de fonctionner.
Y a-t-il une erreur dans le 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 du problème

Votre plus gros problème est,

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

renverra uniquement le nombre de caractères lus dans le tampon, pas les données. Je ne sais pas ce que vous entendez par cela fonctionne les deux premières fois. Voulez-vous dire qu'il boucle 2 fois, ou obtient-il simplement des données pour les deux premiers servos et non le troisième. Quoi qu'il en soit, ci-dessous est un code de travail testé sur un 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);
}

Commentaires

Posts les plus consultés de ce blog

Erreur Symfony : "Une exception a été levée lors du rendu d'un modèle"

Détecter les appuis sur les touches fléchées en JavaScript

Une chaîne vide donne "Des erreurs ont été détectées dans les arguments de la ligne de commande, veuillez vous assurer que tous les arguments sont correctement définis"