Stopping and continuing


Today i made a 7 segment cathode display controller with the arduino.After i had some experience with it, i made an ascending order counting(on the display),and i added a simple switch(not push button)with a resistor as an analog input, to switch the counting on and off.I made a sketch,but i found a problem:the counting is continued while its not visible(not displayed because the switch is turned off).

The question is how to stop or delay the loop and the stop/delay will be skipped by changing the switch state?

I thought the do-while controll is good in this situation,because do:delay x-time and while:while the switch is turned off,and when i turn the switch on,the delay will stop and the loop will continue until i stop it again,but it doesnt work,or i made it wrong...

Link the counting to a state of button not being pushed.

What happened? “It doesn’t work” isn’t descriptive enough for us to assess the problem. You should also post your code. Copy it and put it in the code tags: </> icon.

The do while command will finish your loop after it is switched to off. If this is not what you want, then just have while.

As to how to do it, ‘do while’ is probably best if you need to finish the loop after it is switched off. If you want it to stop the loop when it is switched off, the while command will work fine.

If I had to guess what the problem is, it is probably that you put the delay in if it is off. If you have it switched to off, it will delay and then do the rest of the code. It looks like you did it upside down. This would be what you need, no extra delay.

do {entire command of counting;}
while(switched to on);

This would do the code when you have it switched on, and when you turn it off, it would complete the loop then stop.

Thanks a lot!I dont want to post the code,because its so long... Anyway thanks again!

You shouldn't need a while loop, loop function already loops. Use an if. If switch is on count and display else do nothing. Now your code can do more than count and display. With the while you get limited as to what else you can do.