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

1.6k Views Asked by At

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);
            
          }     //
    
        }   
      

    }

}
2

There are 2 best solutions below

1
On BEST ANSWER

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
        }
    }
} 
0
On

Let's start with some terminology.

I have tried putting a void loop inside the void loop but it does not work.

This sentence does not make much sense.

Every Arduino sketch must implement void loop() {} that is a funcition named loop which has no return value (void) and has no parameters (()).

When you start your Arduino it will call setup once and then run an infinte loop that calls loop in each iteration.

A loop on the other hand is a control structure. There are various kinds like the for loop or the while loop. But none of them have anything to do with the function loop

So you cannot put "a void loop inside the void loop".

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

You don't update user input inside your loops. So how can you expect to respond to it? If you you want to abort your loop prematurely you have to read the user input in the loop frequently.