0
我一直在一个项目工作了很长一段时间,但在它的最后阶段,我坚持让我的代码循环。它运行一次,并从那里不会使电机移动。我已经尝试了while
和if
陈述,但每次我提问时它都不会移动。如何让我的代码循环(与arduino)
代码应该做的是从Websockets画布上接收信息并使用该信息来查看直流电机是前进还是后退。
希望能找到解决方案! :)
#include <AFMotor.h>
int x = -10;
int y = -10;
int b = 0;
AF_DCMotor motor_shoulderx(1);
AF_DCMotor motor_shouldery(2);
AF_DCMotor motor_elbow(3);
AF_DCMotor motor_wrist(4);
void setup() {
motor_shoulderx.run(RELEASE);
motor_shouldery.run(RELEASE);
motor_elbow.run(RELEASE);
motor_wrist.run(RELEASE);
Serial.begin(9600);
}
void loop() {
uint8_t i;
while(Serial.available()) {
if (b == 0) {
x = Serial.read();
b =1;
}
else {
y = Serial.read();
b = 0;
}
if (x != -10) {
Serial.println("x is:");
Serial.println(x);
if(x > 200) {
motor_shoulderx.run(FORWARD);
for (i=0; i<255; i++) {
motor_shoulderx.setSpeed(i);
}
}
else {
motor_shoulderx.run(BACKWARD);
for (i=255; i!=0; i--) {
motor_shoulderx.setSpeed(i);
}
}
}
if (y != -10) {
Serial.println ("y is:");
Serial.println (y);
if (y > 200) {
motor_shouldery.run(FORWARD);
for (i=0; i<255; i++) {
motor_shouldery.setSpeed(i);
}
motor_elbow.run(FORWARD);
for (i=0; i<255; i++) {
motor_elbow.setSpeed(i);
}
motor_wrist.run(FORWARD);
for (i=0; i<255; i++) {
motor_wrist.setSpeed(i);
}
}
else {
motor_shouldery.run(BACKWARD);
for (i=255; i!=0; i--) {
motor_shouldery.setSpeed(i);
}
motor_elbow.run(BACKWARD);
for (i=255; i!=0; i--) {
motor_elbow.setSpeed(i);
}
motor_wrist.run(BACKWARD);
for (i=255; i!=0; i--) {
motor_wrist.setSpeed(i);
}
}
}
}
}
我看不到你的问题的答案,但你能解释为什么你用(i = 0; i <255; i ++){motor_shoulderx.setSpeed(i);}?它将速度设置为0,然后设置为1,然后设置为2,...,然后设置为255.这一切发生的时间都小于1毫秒,所以它没有任何意义。 –