Hi ,
i searched alot for a way to terminate a function in a closed loop code
and ths code below worked with me .
BUT the problem is
i'm using a push button
(there is noway to change it as i need it in other function to count it's presses ) which means that it execute the ( break; ) during my press to the button
and when i release it go back again to the while function .
and i need it work in a different behavior .
i want it do that
- press the button break out of while loop
- execute the next function ( the other function after while() as an example )
and i want to know what after ( break; ) .
i mean where the code flow go after breaking a function
well it go back to the first line of void loop ? or it will execute the function after it i the sequence ?
You want to terminate a loop early based on some condition you are testing.
SolutionUse the following code:
while(analogRead(sensorPin) > 100)
{
if(digitalRead(switchPin) == HIGH)
{
break; //exit the loop if the switch is pressed
}
flashLED(); // call a function to turn an LED on and off
}