1
votes

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?

3

3 Answers

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

}

I think that is a missing bracket closing setup()

0
votes

Your loop condition is wrong. If you want to wait for something to appear on the serial port then you need to change it to this:

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

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

}