c++ - 如何在 Arduino 延迟期间在 void loop() 中使用 serial.available()?

标签 c++ arduino serial-port delay arduino-uno

每当我通过串行监视器发送一个值时,我想执行一个函数,但是当 void loop() 执行延迟函数串行时。 available() 函数不起作用,所以如果我在延迟函数 serial.available 期间在串行监视器中发送任何值将不起作用

#define red 9
#define yellow 8
void setup() {
    // put your setup code here, to run once:
    Serial.begin(9600);
    pinMode(red, OUTPUT);
    pinMode(yellow, OUTPUT);
}
void LightON() {
    digitalWrite(red, HIGH);
    delay(1000);
    digitalWrite(yellow, HIGH);
    delay(1000);
    digitalWrite(red, LOW);
    delay(1000);
}
void LightOff() {
    digitalWrite(red, LOW);
    digitalWrite(yellow, LOW);
}
void loop() {
    // put your main code here, to run repeatedly:
    LightON();
    if (Serial.available())
    {
        LightOff();
    }
}

当我在函数 Lighton() 的延迟期间在串行监视器中输入一个值以便执行 Lightoff() 时,我该如何解决这个问题?

最佳答案

LightON 始终需要 3 秒。为了避免这种情况,事情变得更加复杂。您可以使用状态机来跟踪闪烁序列在任何给定时间的位置。这还有一个好处是允许您的代码执行其他操作,而不是忙于循环或等待 delay()

// This class represents the led state machine
class blinker {
    public:
    // Possible states
    enum {START, RED_ON, YELLOW_ON, RED_OFF, INACTIVE};

    // Constructor
    blinker(byte red_pin, byte yellow_pin, long timeout=1000) :
            red(red_pin), yellow(yellow_pin), timeout(timeout), state(INACTIVE) {
        pinMode(red, OUTPUT);
        pinMode(yellow, OUTPUT);
    }

    // Start the loop from the beginning
    void start() {
        stop();
        state = START;
    }
    // Stop the loop
    void stop() {
        digitalWrite(red, LOW);
        digitalWrite(yellow, LOW);
        state = INACTIVE;
    }

    // Update
    void loop() {
        // Only change if started and time is up
        if (state != INACTIVE && timer_expired()) {
            switch (state) {
                // Starting over?
                case START:
                    state = RED_ON;
                    digitalWrite(red,HIGH);
                    break;
                // Red is on, turn yellow on
                case RED_ON:
                    state = YELLOW_ON;
                    digitalWrite(yellow,HIGH);
                    break;
                // Yellow is on, turn red off
                case YELLOW_ON:
                    state = RED_OFF;
                    digitalWrite(red,LOW);
                    break;
                // Red is off, start over
                case RED_OFF:
                    state = START;
            }
        }
    }    

    protected:
    byte red, yellow;
    long timeout;

    // Returns true when time is up. 
    // Also resets the timer    
    bool timer_expired() {
        if ((millis() - last_time) >= timeout) {
            last_time = millis();
            return true;
        }
        return false;
    }
};

// Create the state machine
blinker blinky(9, 8);
void setup() {
    Serial.begin(9600);
    // Start loop
    blinky.start();
}

void loop() {
    // Call this everytime
    blinky.loop();

    // Stop?
    if (Serial.available()) {  
        blinky.stop();
    }

    // Can do other stuff here
}

我只是把它放在一起。您可以改进它。

关于c++ - 如何在 Arduino 延迟期间在 void loop() 中使用 serial.available()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50703901/

相关文章:

c++ - 如何将 `void(*fn)(const char *, ...)` 转换为 `std::function`,反之亦然

c++ - 使用模板和静态变量时出现链接器错误

ssh - 如何在 LAN 上检测 Arduino Yún/Yún shield IP 地址

c - Arduino编译器在C库中找不到已实现的方法

c++ - 如果我提前将参数声明为变量而不是将它们写入函数调用的行中,这(在内存方面)有什么区别?

c++ - VS2010 C++ MFC 隐藏工具栏

Java 从 Ubuntu 中的串口读取

boost - 使用 boost asio 读取串口中的所有可用数据

使用 termios.h 配置阻止 Linux 串行端口

c++ - 带 get 的数据结构,返回一个 constexpr (C++)