@faisalzed (Faisalzed) posted a new topic, subject "How to End a Loop instantly". It read:

   **I Used 9 LED to blink on and off delay(100ms) in arduino nano, and i attached two Digital Input pin to my arduino nano (D11, D12)**
    
    I am using Blynk app to send the signals into Digital Input Pin to arduino , **BUT** **When i send the off signal to the Digital input pins , the Loop doesnt End Instantly** *They wait for the Whole loop and then its end *
    
    **BUT i want , when i will send the LOW signal to the output the loop will end instantly**
    
    **please help me … get me enlightened**

Then @killzone_kid (killzone_kid) said

    if it is an iteration loop such as `while` or `for` you can use
    
    `break;`
    
    if it is loop() function you can use
    
    `return;`

So @faisalzed (Faisalzed) replied

    thanks for your reply , i am new in arduino , can you help me to put “return” in correct place in the source code , here is my code …
    [code] 
        //From ESP8266 we will run 10 LED
        int digiPin = 11; 
        int digiPin2 = 12;                           
        int ledPin = 2; 
        int ledPin2 = 3;
        int ledPin3 = 4;
        int ledPin4 = 5;
        int ledPin5 = 6;
        int ledPin6 = 7;
        int ledPin7 = 8;
        int ledPin8 = 9;
        int ledPin9 = 10;
        
        
        // select the pin for the LED
        int pinVal = 0; 
        int pinVal2 = 0;
        
        void setup()
        {
         
          Serial.begin(9600);
          
          pinMode(digiPin, INPUT);
          pinMode(digiPin2,INPUT);
          pinMode(ledPin , OUTPUT);
          pinMode(ledPin2 , OUTPUT);
          pinMode(ledPin3 , OUTPUT);
          pinMode(ledPin4 , OUTPUT);
          pinMode(ledPin5 , OUTPUT);
          pinMode(ledPin6 , OUTPUT);
          pinMode(ledPin7 , OUTPUT);
          pinMode(ledPin8 , OUTPUT);
          pinMode(ledPin9 , OUTPUT);
        
          
        }
        
        void loop() {
           
             pinVal= digitalRead(digiPin);
             Serial.println(pinVal);
             delay(50); 
             pinVal2 = digitalRead(digiPin2);
             Serial.println(pinVal2);
             delay(50);
            if(pinVal ==HIGH)  {
        
          
            digitalWrite(ledPin, HIGH);
            delay(100);
            digitalWrite(ledPin, LOW);
            delay(100);
        
            digitalWrite(ledPin2, HIGH);
            delay(100);
            digitalWrite(ledPin2, LOW);
            delay(100);
        
            digitalWrite(ledPin3, HIGH);
            delay(100);
            digitalWrite(ledPin3, LOW);
            delay(100);
            
            digitalWrite(ledPin4, HIGH);
            delay(100);
            digitalWrite(ledPin4, LOW);
            delay(100);
        
            digitalWrite(ledPin5, HIGH);
            delay(100);
            digitalWrite(ledPin5, LOW);
            delay(100);
        
            digitalWrite(ledPin6, HIGH);
            delay(100);
            digitalWrite(ledPin6, LOW);
            delay(100);
        
            digitalWrite(ledPin7, HIGH);
            delay(100);
            digitalWrite(ledPin7, LOW);
            delay(100);
        
            digitalWrite(ledPin8, HIGH);
            delay(100);
            digitalWrite(ledPin8, LOW);
            delay(100);
        
        
            digitalWrite(ledPin9, HIGH);
            delay(100);
            digitalWrite(ledPin9, LOW);
            delay(100);
        
          //2nd Row
        
            digitalWrite(ledPin8, HIGH);
            delay(100);
            digitalWrite(ledPin8, LOW);
            delay(100);
        
            digitalWrite(ledPin7, HIGH);
            delay(100);
            digitalWrite(ledPin7, LOW);
            delay(100);
        
            digitalWrite(ledPin6, HIGH);
            delay(100);
            digitalWrite(ledPin6, LOW);
            delay(100);
        
            digitalWrite(ledPin5, HIGH);
            delay(100);
            digitalWrite(ledPin5, LOW);
            delay(100);
        
            digitalWrite(ledPin4, HIGH);
            delay(100);
            digitalWrite(ledPin4, LOW);
            delay(100);
        
            digitalWrite(ledPin3, HIGH);
            delay(100);
            digitalWrite(ledPin3, LOW);
            delay(100);
        
        
            digitalWrite(ledPin2, HIGH);
            delay(100);
            digitalWrite(ledPin2, LOW);
            delay(100);
        
            digitalWrite(ledPin, HIGH);
            delay(100);
            digitalWrite(ledPin, LOW);
            delay(100);
        
        
        
          //2nd part
        
              digitalWrite(ledPin, HIGH);
        
              digitalWrite(ledPin3, HIGH);
        
              digitalWrite(ledPin5, HIGH);
        
              digitalWrite(ledPin7,HIGH);
        
              digitalWrite(ledPin9, HIGH);
              delay(500);
          
              digitalWrite(ledPin ,LOW);
            
              digitalWrite(ledPin3 ,LOW);
        
              digitalWrite(ledPin5 ,LOW);
        
              digitalWrite(ledPin7 ,LOW);
        
              digitalWrite(ledPin9 ,LOW);
              delay(500);
        
        
              digitalWrite(ledPin2,HIGH);
          
              digitalWrite(ledPin4, HIGH);
        
              digitalWrite(ledPin6, HIGH);
        
              digitalWrite(ledPin8,HIGH);
        
              delay(500);
        
              digitalWrite(ledPin2 ,LOW);
            
              digitalWrite(ledPin4 , LOW);
        
              digitalWrite(ledPin6 ,LOW);
        
              digitalWrite(ledPin8 ,LOW);
        
              delay(500);
        
        
        
        
            } 
            else {
              digitalWrite(ledPin ,LOW);
              digitalWrite(ledPin2 ,LOW);
              digitalWrite(ledPin3 ,LOW);
              digitalWrite(ledPin4 , LOW);
              digitalWrite(ledPin5 ,LOW);
              digitalWrite(ledPin6 ,LOW);
              digitalWrite(ledPin7 ,LOW);
              digitalWrite(ledPin8 ,LOW);
              digitalWrite(ledPin9 ,LOW);
            }
            if(pinVal2 ==HIGH){
            digitalWrite(ledPin ,HIGH);
            delay(100);
            digitalWrite(ledPin,LOW);
            delay(100); 
        
            digitalWrite(ledPin2 ,HIGH);
            delay(100);
            digitalWrite(ledPin2,LOW);
            delay(100); 
        
            digitalWrite(ledPin ,HIGH);
            delay(100);
            digitalWrite(ledPin,LOW);
            delay(100); 
        
            digitalWrite(ledPin2 ,HIGH);
            delay(100);
            digitalWrite(ledPin2 ,LOW);
            delay(100); 
         }
        
         
         else{
          digitalWrite(ledPin,LOW);
        }
        
          
            
          }
        
    [/code]

@MicroBahner (Franz-Peter) replied

    The problem are that many delays in your sketch. Your input pin is only read once in the loop after all the blinking is done.  
    You must rewrite your sketch without delays. See the example ‘BlinkWithoutDelay'. Or you can use a library with time-functions, which maybe a little bit easier

So @UKHeliBob (UKHeliBob) replied

    Use millis() for timing instead of delay() to avoid the code blocking waiting for the delay()s during which it can do nothing else
    
    See https://forum.arduino.cc/index.php?topic=503368.0 and https://forum.arduino.cc/index.php?topic=223286.0 and the BlinkWithoutDelay example

After which, @paulpaulson (Paul-Paulson) added

    Hello  
    I have read your sketch to understand what your intention is.  
    I think you would like to have a gallery of flash lights controlled by the the input switches digiPin and digiPin2. That will not work in all cases the
    
    else {  
    digitalWrite(ledPin , LOW);  
    digitalWrite(ledPin2 , LOW);  
    digitalWrite(ledPin3 , LOW);  
    digitalWrite(ledPin4 , LOW);  
    digitalWrite(ledPin5 , LOW);  
    digitalWrite(ledPin6 , LOW);  
    digitalWrite(ledPin7 , LOW);  
    digitalWrite(ledPin8 , LOW);  
    digitalWrite(ledPin9 , LOW);  
    }
    
    will inhibit all other LED actions.  
    Well I think a collection of timers and a small FSM will be a proper start point to do this job.

To which @johnwasser (johnwasser) replied

    One quick and dirty option is to write a ‘myDelay()' function that checks the button while waiting for time to expire. If the button is pressed, have it return ‘true'. If the time expires, have it reurn ‘false'. Then, in your code, replace “delay(100);” with
    [code] 
            if (myDelay(100)) return;
        
    [/code]
    
    The ‘return;' statement will exit from loop() which will then be called again and will detect that the button is pressed.
    [code] 
        bool myDelay(unsigned long delayTime)
        {
          unsigned long startTime = millis();
          while (millis() - startTime <= delayTime)
          {
            if (digitalRead(digiPin) == LOW)
              return true;
          }
          return false;
        }
        
    [/code]

So @alto777 (alto777) replied

    johnwasser:
    
    > One quick and dirty option
    
    One man's **quick and dirty** is another's _clever_.
    
    a7

@faisalzed (Faisalzed) replied

    **Thank You So MUCH**

To which @faisalzed (Faisalzed) replied

    in which part i will put this code ???

Then @johnwasser (johnwasser) said

    faisalzed:
    
    > in which part i will put this code ???
    
    Like I said:
    
    johnwasser:
    
    > Then, in your code, replace “delay(100);” with
    [code] 
    >         if (myDelay(100)) return;
    >     
    [/code]
    
    What part of that did you not understand?

To which @system (system) replied

    This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.


{'source': 'discourse', 'url': 'https://forum.arduino.cc/t/how-to-end-a-loop-instantly/853056', 'title': 'How to End a Loop instantly', 'views': 309, 'has_accepted_answer': False, 'word_count': 952, 'like_count': 5, 'reply_count': 7, 'participant_count': 7, 'posts': 12, 'tags': '', 'author': '@faisalzed (Faisalzed)', 'participants': '@faisalzed (Faisalzed), @killzone_kid (killzone_kid), @MicroBahner (Franz-Peter), @UKHeliBob (UKHeliBob), @paulpaulson (Paul-Paulson), @johnwasser (johnwasser), @alto777 (alto777), @system (system)', 'image_url': [None], 'created_at': '2021-04-24T07:14:07.696Z', 'last_posted_at': '2021-08-27T14:56:01.092Z'}