Arduino Leonardo Endless Loop

时间:2014-05-18 14:01:07

标签: arduino arduino-ide arduino-uno

我的代码:

*int led = 13;
void setup(){
  Serial.begin(9600);
  pinMode(led, OUTPUT);

  while (!Serial) {
//My code get stack here!
//it stay here looping on endlessly!
    digitalWrite(led, HIGH);   
    delay(500);               
    digitalWrite(led, LOW);   
    delay(500);  
  }
void loop() {

}*

所以这就是问题所在。等待Serial和while循环的简单程序将永远继续。 如何解决这个问题。这是一个众所周知的问题吗?

3 个答案:

答案 0 :(得分:0)

int led = 13;
void setup(){
  Serial.begin(9600);
  pinMode(led, OUTPUT);

  while (!Serial1) {
//My code get stack here!
//it stay here looping on endlessly!
    digitalWrite(led, HIGH);   
    delay(500);               
    digitalWrite(led, LOW);   
    delay(500);  
  }
}

void loop() {

}

我认为这是一个缺少的括号关闭设置()

答案 1 :(得分:0)

您的代码很好,看起来您的串口没有连接。如果我修改你的代码来观察一个计数器最多5,当计数器是5然后myconnection为真,那么!myconnection是假的并且循环停止(在工具>串行监视器中观察):

int led = 13;
int counter = 0;
boolean myconnection = false;

void setup(){
  Serial.begin(9600);
  pinMode(led, OUTPUT);
  while (!myconnection) {
    //My code get stack here!
    //it stay here looping on endlessly!
    digitalWrite(led, HIGH);   
    delay(500);               
    digitalWrite(led, LOW);   
    delay(500); 

//here's my debug stuff
    counter++;
    if (counter ==5 ){
      myconnection = true;
    }
    Serial.println("counter: " + String(counter) + ", myconnection: " + String(myconnection)); 
  }
}

void loop() {

}

答案 2 :(得分:0)

您的循环条件错误。如果您想等待串口上显示的内容,则需要将其更改为:

while (Serial.available() == 0) {