简体   繁体   English

如何使我的代码循环(使用arduino)

[英]How to make my code loop (With arduino)

I've been working on a project for quite a while now, but on the last phase of it I'm stuck on making my code loop. 我已经在一个项目上工作了一段时间了,但是在项目的最后阶段,我坚持进行代码循环。 It runs once and from there on it doesn't make the motors move. 它运行一次,从此不会使电动机运动。 I've tried the while and if statements but it still doesn't move every time I ask it to. 我已经尝试了whileif语句,但是每次我问时它仍然不会移动。

What the code is supposed to do is to receive information from a websockets canvas and use that information to see if the dc motor goes forwards or backwards. 代码应该做的是从websockets画布接收信息,并使用该信息查看直流电动机是向前还是向后。

Hope to find a solution! 希望找到解决方案! :) :)

#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);  
                }
            }
        }   
    }
}

You have to use another structure. 您必须使用其他结构。 At the moment your mainloop consists of many single for-loops, which were executed sequently. 目前,您的mainloop由许多单个的for循环组成,这些循环依次执行。 In order to realize a parallel execution (I think that is what you want) you need somethink like this: 为了实现并行执行(我想这就是您想要的),您需要这样的思考:

int i;

void setup() {
    i=255;
//...
}

void loop() {
     i--;
     motor_shoulderx.setSpeed(i);
     motor_elbow.setSpeed(i);  
     if(i==0)i=255;   
}

If you need more complex logic you can easily realize that with conditions. 如果您需要更复杂的逻辑,则可以轻松地通过条件实现这一点。 If you need delays you have to use time-comparing code patterns like this: 如果需要延迟,则必须使用类似以下时间的代码模式:

unsigned long interval=1000;    // the time we need to wait
unsigned long previousMillis=0; // millis() returns an unsigned long.

void setup() {
//...
}

void loop() {
 if ((unsigned long)(millis() - previousMillis) >= interval) {
 previousMillis = millis();
 // ... 
 }
}
//...

All in all, I think the important thing is that the main loop should not be delayed by the single for-statements. 总而言之,我认为重要的是主循环不应因单个for语句而延迟。 Hope this helps you. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM