Best way to write this nested loop

So here’s what I’m aiming for:

I have a 16x2 LCD hooked up via an adafruit LCD shield (https://www.adafruit.com/products/772) and my plan is to turn it into a soft of multi-function display, using a wifi shield to supply weather, email counts, whatever.

My first step was to make a basic temperature display using an MCP9808 sensor (MCP9808 High Accuracy I2C Temperature Sensor Breakout Board : ID 1782 : $4.95 : Adafruit Industries, Unique & fun DIY electronics and kits). Each “page” of the display would be keyed to a button on the shield (there are 5 total, plus a reset). I have the simple sketch written (see below) but I’m trying to figure out the best way to work the button coding. Currently all works, but the button to display temperature only polls the sensor once, then stops.

What would the best way to work the buttons so that when each is pressed, the resultant code would loop continuously, until another button is pressed?

// include the library code:
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
#include <Adafruit_MCP9808.h>

// The shield uses the I2C SCL and SDA pins. On classic Arduinos
// this is Analog 4 and 5 so you can't use those for analogRead() anymore
// However, you can connect other I2C sensors to the I2C bus and share
// the I2C bus.
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();


void setup() {
  Serial.begin(9600);
  lcd.begin(16,2);
  tempsensor.begin(0x19);
}

void temp () {
  float c = tempsensor.readTempC();
  lcd.setCursor(0,0);
  lcd.print("Current Temp.");
  lcd.setCursor(0,1);
  lcd.print(c);
  lcd.setCursor(6,1);
  lcd.print("deg. C");
}
void loop() {
  uint8_t buttons = lcd.readButtons();

  if (buttons) {
    lcd.clear();
    lcd.setCursor(0,0);
    if (buttons & BUTTON_UP) {
      lcd.print("UP ");
    }
    if (buttons & BUTTON_DOWN) {
      lcd.print("DOWN ");
    }
    if (buttons & BUTTON_LEFT) {
      lcd.print("LEFT ");
    }
    if (buttons & BUTTON_RIGHT) {
      lcd.print("RIGHT ");
    }
    if (buttons & BUTTON_SELECT) {
      temp();}
    }
  }

I think the problem is that you only call temp() when the SELECT button is pushed. Instead of calling temp() there you should set a variable to note that you are in the "temperature mode". Then use BlinkWithNoDelay techniques to update the display periodically, perhaps every few seconds. You will get a screen flicker every time you do a full screen erase so you should probably only erase the screen when the mode changes and on the periodic updates just change the parts that need changing. The temp() function (and functions for the other modes) should get a flag to let them know that the mode has just changed or not. If the mode just changed they should draw the whole screen. If not, just draw the updates.

void temp (boolean newMode) {
  float c = tempsensor.readTempC();
  if (newMode) {
      lcd.setCursor(0,0);
      lcd.print("Current Temp.");
      lcd.setCursor(6,1);
      lcd.print("deg. C");
  }
  lcd.setCursor(0,1);
  lcd.print("      "); // Clear the field
  lcd.setCursor(0,1);
  lcd.print(c);
}

Thanks for the quick reply, I see what I'm doing wrong and I have a vague idea, thanks to your suggestion, on how to fix.

I've been playing around, and added a flag to indicate what mode I'm in:

if (buttons & BUTTON_UP) {
      mode = 1;
    }
    if (buttons & BUTTON_DOWN) {
      mode = 2;
    }
    if (buttons & BUTTON_LEFT) {
      mode = 3;
    }
    if (buttons & BUTTON_RIGHT) {
      mode = 4;
    }
    if (buttons & BUTTON_SELECT) {
      mode = 5;
  }

I'm still struggling with the update function, should the BlinkWithoutDelay technique be used inside the if statement for the button or within the temp() function?

(I'm really rusty, the last programming course I took was PASCAL in high school, I know I've got a lot to learn

Using a while would work? Probably not the best way, but doable.

if button_up {
while NOTbutton_down/left/etc{
//loop
}