0

我有一个循环,旨在在 Arduino 的 LCD 显示器上写出文本。问题是循环可能会中断,具体取决于我何时通过串行端口发送文本。

例如,它可能会将我希望它写的内容读取为我希望文本对齐的方式。有没有办法来解决这个问题?

以下是循环的示例:

void loop() {
    if (Serial.available()) {
        do {
            ch = Serial.read();
        } while (ch = 'y');
    }

    if (Serial.available()) {
        do {
            ch = Serial.read();
        } while (ch = 'x');
    }
}
4

1 回答 1

0

You probably wanted:

void loop() {
    if (Serial.available()) {
        do {
            ch = Serial.read();
        } while (ch == 'y');
    }

    if (Serial.available()) {
        do {
            ch = Serial.read();
        } while (ch == 'x');
    }
}

here's a tip: Write your if statements backwards, like

if(NULL == somevariable){
    //Do this
}

Compilers will complain noisily if you forget the second = (you can't set a constant!)

It's an easy mistake to make!

于 2012-12-12T22:04:56.680 回答