Help with Multiple Nested Loop (Strange Behavior)



Looking for some help on some code which seems to be behaving weirdly… @ScruffR, @peekay123.

I have a couple of loops running through the ButtonClick library (to drive some LCD and button controlled motors).

Would there be any reason, in the code below, that after the while loop (while timeElapsed < interval) would kick out and run again (LCD display seems to run a couple of times, despite the while loop completing).

Any ideas on how to test, or perhaps to make the code more efficient would be most appreciated… I realise I’m using the d-word / delays but this was put together quickly.

Thanks all. Comments / suggestions welcome.

  if(function == 2) {                               //double click
  if(function == 3) {                               //triple click
  if(function == -1) {                              //single long click on button 2
        while(timeElapsed < interval){              //timeout function (interval is above) after no button pressed it exits
            if (digitalRead(D3) == LOW){
                ml_dose = dose_amt[dose_amt_select];
                display.setTextSize(3);                   // from 1-9 sensible actually can be bigger but useless
                display.setTextColor(WHITE, BLACK);       // 'inverted' text
                if (ml_dose>99) {
                        else if (ml_dose<100) {
                display.setCursor(71,10);                 // 128,64 pixels
                display.setCursor(30,35);                 // 128,64 pixels
                display.setCursor(11,50);                 // 128,64 pixels
                display.println("Dose Size");
                timeElapsed = 0;
                if (dose_amt_select == 11){ dose_amt_select = 0; }


@neal_tommy, without seeing you entire code, it is difficult to advise. It isn’t clear what you are trying to achieve in the code with the while() and the digitalRead(D3). Can you explain?


I agree with @peekay123, but two things jump to mind

  • you are not setting function to anything other than -1 so that if(function == -1) will clearly hit again
  • while(timeElapsed < interval) shoul be satisfied immediately after setting timeElapsed = 0 too

so from that snippet I’m not surprised to see that part fire multiple times after it has fired once :wink:



Thanks for the quick responses, appreciate the time.

Essentially I’m using the ClickButton library for 3 separate push buttons. For certain push button actions I’m displaying various text on the LCD and then changing a variable for use later on in the program.

The while loop is a timeout function (if the user doesn’t push a button then it kicks out).

@ScruffR - I’ll go through your suggestions and work from there. I take it I should be more specific on the setting of the variables upon exiting the various loops?



Yes, but what do you mean with “various loops”?
There is only one loop there and I can’t see any nested loops either.

If you mean your if() blocks, then these are not loops but conditional blocks - don’t mix terms, otherwise people will just get confused.


@ScruffR - cheers for the advice, not wanting to add any more confusion to the situation.

Yes, I think I should be more specific on declaring the state of the variables (timeElapsed and function). I will try a few things and let you know.