for循环这里不起作用我正在使用蓝牙串行连接

时间:2015-04-12 16:05:11

标签: loops if-statement for-loop arduino

void loop() // run over and over 
{
    while (!mySerial.available()); // stay here so long as COM port is empty
    receivedChar = mySerial.read();

    if (receivedChar == '1')
    {
        digitalWrite(LED, HIGH);
        for (int i=0; i<500; i++) 
        {
            digitalWrite(buz, HIGH);
            delayMicroseconds(500);
            digitalWrite(buz, LOW);
            delayMicroseconds(500); 
        }
    }// if it's a 1 turn LED on

    if (receivedChar == '2')
    {
        digitalWrite(LED, LOW);
    } // if it's a 2 turn LED off
} // if it is a 3 flash the LED

这里&#34; forloop&#34;没有循环请帮助这个

3 个答案:

答案 0 :(得分:0)

我建议您使用String并附加单个字符,并使用Serial.available() > 0作为循环条件短delay。我希望这会对你有所帮助。

int led = 13; 

void setup(){
    pinMode(led, OUTPUT); 
    Serial.begin(9600); 
    Serial.flush();
}

void loop()
{
    String receivedChars = "";

    while (Serial.available() > 0){
        receivedChars += (char) Serial.read(); 
        delay(5); 
    }

    if (receivedChars == "1"){
        digitalWrite(led, HIGH); 
    }
    else if (receivedChars == "2"){
        digitalWrite(led, LOW); 
    }
}

答案 1 :(得分:0)

编辑全新答案:

好的,我希望我能更好地理解你的问题。如果你想让LED闪烁,直到你收到相应的其他命令,你可以做这样的事情(没有明确测试):

另一个编辑:从您对该问题的评论我似乎明白,您希望在发送1时切换闪烁的开关?如果是这种情况,您可以在发送1时切换到支票时添加另一个布尔值。

void loop() // run over and over 
{
    static bool active = false;
    static char receivedChar = '0';
    if (mySerial.available()) { 
        receivedChar = mySerial.read();
        // if you received a 1, change the state of the 'active' boolean
        if (receivedChar == '1')
        {
            active = !active;
        }
    }



    // only perform the action on receiving 1 
    // only when also the boolean is set to the correct value
    if (receivedChar == '1' && active)
    {
            digitalWrite(buz, HIGH);
            delay(100);
            digitalWrite(buz, LOW);
            delay(100); 
    }// if it's a 1 turn LED on

    if (receivedChar == '2')
    {
        digitalWrite(LED, LOW);
    } // if it's a 2 turn LED off
} // if it is a 3 flash the LED

receivedChar声明为静态应该在loop的下一次迭代中保持其值相同。这意味着loop将会运行,如果您发送了1,它将始终进入if (receivedChar == '1')状态,并根据您选择的任何延迟打开和关闭LED,然后重复此操作直到你发送另一个角色,例如2此时他将进入条件if (receivedChar == '2'),关闭LED并返回if (receivedChar == '2'),直到再次发送其他内容为止。

这有帮助吗?

又一个编辑:

将arduino和LED连接到引脚3,下面的草图可以满足您的要求:

通过串口发送1,LED开始闪烁。发送另一个1,闪烁停止。如果您发送2,则眨眼也会停止。

如果此草图未显示上述行为,那么您在代码中执行的某些操作并未公开。

int LED = 3;

// the setup routine runs once when you press reset:
void setup() {      
  Serial.begin(9600);

  // initialize the digital pin as an output.
  pinMode(LED, OUTPUT);     
}


void loop() // run over and over 
{
    static bool active = false;
    static char receivedChar = '0';
    if (Serial.available()) { 
        receivedChar = Serial.read();
        // if you received a 1, change the state of the 'active' boolean
        if (receivedChar == '1')
        {
            active = !active;
        }
    }



    // only perform the action on receiving 1 
    // only when also the boolean is set to the correct value
    if (receivedChar == '1' && active)
    {
            digitalWrite(LED,HIGH);  
            delay(100);
            digitalWrite(LED, LOW);
            delay(100); 
    }// if it's a 1 turn LED on

    if (receivedChar == '2')
    {
        digitalWrite(LED, LOW);
        active = false;
    } // if it's a 2 turn LED off
} // if it is a 3 flash the LED

答案 2 :(得分:0)

编辑看到其他答案中的错误,似乎您希望在收到1时使LED闪烁并在收到2时停止闪烁。首先,您应该编辑原始问题,因为有时您会写digitalWrite(LED, HIGH),有时会写digitalWrite(buz, HIGH)。如果你在同一个引脚中使用相同的LED,那就太麻烦了。

然后我会建议这段代码。

boolean blink = false;
byte state = 0;

void loop()
{
    if (mySerial.available()) // there is some data to read
    {
        receivedChar = mySerial.read();
        switch (receivedChar)
        {
            case '1':
                blink = true;
                break;
            case '2':
                blink = false;
                digitalWrite(LED, LOW); //turn off LED
                break;
        }
    }
    if (blink)
    {
        state ^= 1; //switch bit using xor operator
        digitalWrite(PED, state);
    }
    delay(500);
}

希望它很容易理解。如果没有,请随时询问。


旧帖子:你确定它没有循环吗?可能if (receivedChar)条件总是错误的?通过串口发送一些调试信息进行检查。

此外,loop()函数是一个无限循环,不需要再添加一个。只需查看if (mySerial.available())

看起来你想在LED亮起时发出声音,你可以使用Tone功能。

我会建议这样的事情:

void loop()
{
    if (mySerial.available()) // there is some data to read
    {
        receivedChar = mySerial.read();
        if (receivedChar == '1')
        {
            mySerial.println("Received a 1"); // for debugging
            digitalWrite(LED, HIGH);
            tone(buz, 1000, 500);
        }
        if (receivedChar == '2')
        {
            mySerial.println("Received a 2"); // for debugging
            digitalWrite(LED, LOW);
        }
    }
}