arduinoarduino-unoarduino-ideserial-monitor

Void loop inside a void loop doesn't work. What does?


So I have a program here with three different LED loop patterns, the codes are inside an if statement I noticed that it does not loop anymore even if the if statement is inside the void loop. I have tried putting a void loop inside the void loop but it does not work. I can't have the code loop to a certain extent because when I tried it and set the number to 50 loops, it finished all 50 loops even if the user typed another input at the 3rd loop. I am new to arduino and would really appreciate new information on how to solve this problem. Here is my code:

int previousLED;

void setup(){
    Serial.begin(9600);
    Serial.println("Enter desired Pattern Number: ");
    for(int i=2; i<=9; i++){
    pinMode (i,OUTPUT);      
    }   


}

void loop(){
    if (Serial.available()){
        char serialdata = Serial.read();
        //Serial.print("You have entered Pattern  ");
        //Serial.println(serialdata);
        //Serial.print(" in BINARY:");
        //Serial.println(serialdata,BIN);

        if (serialdata == '1'){
          
          Serial.println("LED Pattern 1 has been triggered!");
          
          //
          
            for(int ledPin =  2; ledPin<=9; ledPin++){
            
                digitalWrite(previousLED,LOW);
    
                digitalWrite(ledPin,HIGH);
    
                delay(200);
                previousLED = ledPin;    
            }
  
            for(int ledPin =  9; ledPin>=2; ledPin--){
    
                digitalWrite(previousLED,LOW);
    
                digitalWrite(ledPin,HIGH);
    
                delay(200);
                previousLED = ledPin;
    
            }

        } 
        
        if (serialdata == '2'){
          
          Serial.println("LED Pattern 2 has been triggered!");
          
          
          
            for(int ledPin =  2; ledPin<=9; ledPin++){    
    
                digitalWrite(ledPin,HIGH);    
                delay(700);
              }
          
        delay(3000);
        digitalWrite(2, LOW);
        digitalWrite(3, LOW);
        digitalWrite(4, LOW);
        digitalWrite(5, LOW);
        digitalWrite(6, LOW);
        digitalWrite(7, LOW);
        digitalWrite(8, LOW);
        digitalWrite(9, LOW);
        delay(1000);    
    
        }   
        
        if (serialdata == '3'){
          
          Serial.println("LED Pattern 3 has been triggered!");
          
          for (int k = 0; k<=5; k++){   //
          
            for(int ledPin =  2; ledPin<=9; ledPin++){
    
                digitalWrite(ledPin, HIGH);    
              }
  
            delay(200);
  
            for(int ledPin =  2; ledPin<=9; ledPin++){
    
                digitalWrite(ledPin, LOW);    
            }
  
            delay(200);
            
          }     //
    
        }   
      

    }

}

Solution

  • try something like this. this might not be a exact answer but you can get a idea.

    if (serialdata == '1'){
     while(!Serial.available()) {
          if(ledpin==2){
            //for loop
            }
          if(ledpin==9){
            //for loop
            }
        }
    }