C++ Mearm Arduino机器人的代码不工作
我对Mearm Arduino机器人的代码有一些问题。 它应该要求用户输入伺服电动机角度的值并旋转它们,但是只能在前2次这样做,然后停止工作。
代码中有错误吗?
#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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您最大的问题是
仅将读取的字符数返回到缓冲区,而不是数据。不确定您的意思是前两次起作用。您是说它循环2次,还是仅获取前两个伺服器的数据,而不是第三次伺服器。无论如何,以下是在UNO上测试的工作代码。
Your biggest problem is,
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.