1

My Code:

*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() {

}*

So this is the problem. the simple program waiting for Serial and the while loop continue forever. how to fix this. is it a known problem?

4

3 回答 3

0

Your code is fine, it looks like your serial port is not connecting. If I modify your code to watch a counter up to 5, and when the counter is 5 then myconnection is true, then !myconnection is false and the loop stops (watch it in Tools>Serial Monitor):

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() {

}
于 2014-05-19T12:05:52.623 回答
0

你的循环条件是错误的。如果您想等待串行端口上出现某些内容,则需要将其更改为:

while (Serial.available() == 0) {
于 2014-05-19T14:51:28.673 回答
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() {

}

我认为这是缺少括号关闭 setup()

于 2014-05-18T14:46:13.827 回答