Exit from LOOP Function

If a write a program to CONTINUOUSLY BLINK A LED, like this:

void SETUP()
{
pinMode(13,OUTPUT);
}

void LOOP()
{
digitalWrite(13,HIGH);
delay(2000);
digitalWrite(13,LOW);
delay(2000);
}

I upload this on the Board and it works fine. This code will repeat itself and the LED will blink continuously. How to stop the blink of LED? Do I need to write another code to stop it and upload that with the previous one working…because I have tried this after removing USB from Laptop and again plugging in the previous code still executes.
My question is how to stop this iteration all at once, means, all the codes needed to be stopped at one point of time. How to do that??

2 Likes

Please solve the query.

2 Likes

One possible solution is to have a while loop with the ‘true’ condition passed on to the set of instructions to be looped and the CTRL + C button pressed from the console to exit the loop.

If you want the loop to be handled with more control then you could employ a variable which stops the loop when a certain input is read.

1 Like

Hello,

To exit from a loop you can make use of conditional statements, like “while” or “if” depending on the number of times you want the LED to blink, and later add on a break or exit statements.These statements help you come out of the loop.

In aurdino the code you dump earlier is retained until you dump a new code, hence your LED blinks (executes the previous code) after replugging in.

Hope this answers your question.

2 Likes

Thanks @rishabhajay24 and @sonalsurendra2000 for replying.

4 Likes

@sonalsurendra2000 can you please share a program code where loops have been used to stop the program.

1 Like

u can find many such examples in the internet.
1 Like

use ctrl +c
i will work

1 Like

Ok. Thanks @abhishekbiswas772 @sonalsurendra2000 for the help.

use
exit(0);
after the number of iterations u need to complete