简体   繁体   English

为什么我的代码不断循环电机 function 代码?

[英]Why does my code keep looping the motor function code?

Essentially as the title says the motor moving function keeps looping.基本上正如标题所说,移动 function 的电机一直在循环。 And it stops printing the IR values to the serial, essentially it just keeps running the motor function.它停止将 IR 值打印到串行,基本上它只是继续运行电机 function。

I know this is probably stupid simple, and i should know it but the thing is that this is hacked together with code snippets online and i dont have any experience with c++ so.............. |{o_o}|我知道这可能是愚蠢的简单,我应该知道,但问题是这与在线代码片段一起被黑客攻击,我对 c++ 没有任何经验,所以...... | {o_o}|

//why? i have no idea but platformio put it here so must be important
#include <arduino.h>
//include the ir remote stuff
#include <IRremote.h> 

//Controls speed of motors
int MAX_PWM_VOLTAGE = 150;

// the pin where you connect the output pin of IR sensor
int RECV_PIN = D5;
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long key_value = 0; // variable to store the key value

void setup() 
{
    //Right Motor
    pinMode(D1, OUTPUT);
    pinMode(D2, OUTPUT);

    //Left Motor
    pinMode(D3, OUTPUT);
    pinMode(D4, OUTPUT);

    Serial.begin(9600);
    irrecv.enableIRIn();
}

void loop() 
{
    if (irrecv.decode(&results))
    {
        if (results.value == 0xFFFFFFFF) // if the value is equal to 0xFFFFFFFF
        {
            results.value = key_value; // set the value to the key value
        }

        Serial.println(" ");     
        Serial.print("Code: ");     
        Serial.println(results.value); //prints the value a a button press

    }

    key_value = results.value; // store the value as key_value
    irrecv.resume(); // reset the receiver for the next code
    
    //WHY DOES IT KEEP LOOPING & WHY ARE NO OTHER COMMANDS COMING IN??????
    if (results.value == 0xFF30CF)
    {
        Forward();
    }
    irrecv.resume();

}

void Reverse()
{
    digitalWrite(D2, LOW);
    digitalWrite(D3, LOW);
    analogWrite(D1, MAX_PWM_VOLTAGE);
    analogWrite(D4, MAX_PWM_VOLTAGE);
    delay(500);
    motorOff();
}

void Forward()
{
    digitalWrite(D1, LOW);
    digitalWrite(D4, LOW);
    analogWrite(D2, MAX_PWM_VOLTAGE);
    analogWrite(D3, MAX_PWM_VOLTAGE);
    delay(5000);
    motorOff();
}
    
void TurnLeft()
{
    digitalWrite(D1, LOW);
    digitalWrite(D3, LOW);
    
    analogWrite(D2, MAX_PWM_VOLTAGE);
    analogWrite(D4, MAX_PWM_VOLTAGE);
    motorOff();
}

void TurnRight()
{
    digitalWrite(D2, LOW);
    digitalWrite(D4, LOW);
    analogWrite(D1, MAX_PWM_VOLTAGE);
    analogWrite(D3, MAX_PWM_VOLTAGE);
    motorOff();
}

void motorOff()
{
    digitalWrite(D1, LOW);
    digitalWrite(D2, LOW);
    digitalWrite(D3,LOW);
    digitalWrite(D4,LOW);
}

Try to put a volatile qualifier in the declaration of the results:尝试在结果声明中放置一个 volatile 限定符:

volatile decode_results results;

Here is some info on why is it necessary to use volatile: https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/volatile/以下是有关为什么需要使用 volatile 的一些信息: https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/volatile/

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

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