Switching 'modes' with an IF but keep looping

Finding coding incredibly difficult but i'm having fun learning.

I have managed to cobble together code to do what I want (understanding most of it) but i'm struggling trying to push things further.

Code A works to display temperature, time passed (I know I am not using day() correctly but it does what I need), and works basically as a thermostat. Stangely, some of the static text printed to the LCD blinks when I turn the pot but I can live with that.

I would like a push button to switch between this 'mode' and a second 'mode' displaying data from a different sensor.

My basic implementation, (Code B) switches as I want but the code no longer loops - so the temperature data or the pot value isn't updated.

In short, (sorry about my length) how would I switch between two 'modes' whilst still being able to see sensor data?

codeA.ino (8.41 KB)

codeB.ino (9.78 KB)

Struggling with this. Used code from: https://www.arduino.cc/en/tutorial/switch

Don't really know where to go from here.

I think you are suggesting that each button press should set a mode variable rather than execute direct code.

I just don't know how! Story of my life :frowning:

I get:

if mode == 0 then display one thing
    if mode == 1 then display something else
    if mode == 2 then display something else.

Just not sure how to set that variable on button press

Thank you - have it working now.

Still have some of the static text printed to the LCD blinks when I turn the pot but at least everything is functioning.

Thanks again.

The pieces of code that should run when the different modes are selected should be put into functions. Then you can have code in loop() something like this

void loop() {
   checkButtonsAndUpdateMode();
   if (mode == 1) {
      functionForMode1();
   }
   else if (mode == 2) {
     functionForMode2();
   }
   else {
     functionForMode3();
   }
}

See how functions are used in Planning and Implementing a Program

...R