Arduino cookbook code explaination

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.
Solution

Use 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
}

To the next instruction after the while{}

You can always write some debug code to prove a hypothesis.

If reading switchPin is what gets you out of the loop, then use that to set a flag and call break.
Outside of the whole while, test the flag, if set then perform the flashLED function and clear the flag.

while(analogRead(sensorPin) > 100)
{
if(digitalRead(switchPin) == HIGH)
  {
   ledFlashFlag = 1;
   break; //exit the loop if the switch is pressed
   }
}
if (ledFlashFlag == 1){
ledFlashFlag = 0;
flashLED(); // call a function to turn an LED on and off
}
while(analogRead(sensorPin) > 100)
{
if(digitalRead(switchPin) == HIGH)
  {//wait for button to be released
  while (digitalRead(switchPin) == HIGH);
  //THEN you can break and the button won't be on anymore
  break; 
  }
flashLED(); // call a function to turn an LED on and off
}

@CrossRoads , @Kenf

Thank u guys i do really appreciate , God bless U