Loops 这里的for loop不工作我正在使用蓝牙串行连接

Loops 这里的for loop不工作我正在使用蓝牙串行连接,loops,if-statement,for-loop,arduino,Loops,If Statement,For Loop,Arduino,此处forloop未循环请对此提供帮助我建议使用字符串并附加单个字符,并使用Serial.available>0(具有短延迟)作为循环条件。我希望这会对你有所帮助 void loop() // run over and over { while (!mySerial.available()); // stay here so long as COM port is empty receivedChar = mySerial.read(); if (receivedCh

此处forloop未循环请对此提供帮助

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

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

使用全新答案编辑:

好的,我希望我能更好地理解你的问题。如果您想让LED一直闪烁直到收到相应的其他命令,您可以执行以下未经明确测试的操作:

另一个编辑:从你对这个问题的评论中,我似乎明白你想在发送1时打开和关闭闪烁?如果是这种情况,您可以在发送1时切换的检查中添加另一个布尔值

将receivedChar声明为static应该在循环的下一次迭代中保持其值相同。这意味着循环将运行,如果您发送1,它将始终进入if receivedChar='1'条件,并以您选择的任何延迟打开和关闭LED,然后简单地重复此操作,直到您发送另一个字符,例如2,此时他将进入if receivedChar='2'条件,关闭LED,然后返回if receivedChar=='2',直到再次发送其他内容

这有用吗

还有一个编辑:

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

通过串行端口发送1,LED开始闪烁。再发送1,闪烁停止。如果发送2,闪烁也会停止

如果这个草图没有显示上面描述的行为,那么您正在代码中做一些您没有透露的事情

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
编辑查看其他答案中的讨论,似乎您希望在收到1时使LED闪烁,在收到2时停止闪烁。首先,你应该编辑原始问题,因为有时你写的是digitalWriteLED,HIGH,有时是digitalWritebuz,HIGH。如果您在同一个引脚中使用相同的LED,则会产生混淆

那么我建议使用这个代码

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
希望它容易理解。如果没有,请随时询问

老帖子:你确定它没有循环吗?如果receivedChar条件始终为false,则可能是?通过串口发送一些调试信息进行检查

此外,循环函数是一个无限循环,无需再添加一个。只需检查mySerial.com是否可用

似乎您想在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);
}

谢谢你的建议。我想让loop在这里工作。除非我删除第二个条件,否则它不会连续循环。如果条件为Yes,我将其设置为delay300,但我的问题是循环在其中不起作用。if receivedChar==1{而receivedChar==1{digitalWriteLED,高;//将LED设置为delay200;//等待第二个digitalWriteLED,低;//将LED设置为delay200;}}//如果是1,则在receivedChar==2时打开LED{digitalWriteLED,低;}}//如果是2圈LED关闭,我无法使用recievedChar2@AlfredKommina我改变了我的答案,我希望你是什么样的人asking@AlfredKommina我试着把我从你的评论中理解的东西结合起来眨眼不起作用。当它没有循环时,它只是闪烁让我试着看看我是否理解你的问题:如果你收到1,那么你希望LED进入闪烁模式,当你收到2时,你想中断该模式?是的,当它收到2时,你是对的,那么闪烁应该停止
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);
}
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);
        }
    }
}