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

for loop here is not working i am using bluetooth serial connection

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

这里"forloop"没有循环,请帮忙

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

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

使用全新答案进行编辑:

好的,我希望我能更好地理解你的问题。如果你想让 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

EDIT 看到其他答案中的讨论,似乎您想在收到 1 时使 LED 闪烁,并在收到 [=13= 时停止闪烁].首先,您应该编辑原始问题,因为有时您会写 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);
}

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


OLD POST:你确定不是循环播放?可能 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);
        }
    }
}