Running part of code once only each time the case is switched

Hi Guys, Whats the best way to have an LCD Display, display a message for say 2 secs then display something else whilst it remains inside that case. When the case is changed, then returns have it run the message 1 again. Example if (buttonPressed == HIGH) { mode++ }

if (mode == 1) { lcd.print(" auto mode ") // I want this to display for about 2 seconds then go away until the loop is selected next time

I want the display to then change to show battery voltage for example for the rest of the time. At the moment i have this

lcd.print( " blah blah "); delay 500; lcd.setCursor(0,0); lcd.print("battery volts'); lcd.setCursor(12,0); lcd.print(volt);

obviously this does not work. Could someone please point me in the right direction ? Should i use an elapsed timer or is there an easier way ? Thanks BlackSnake

Without seeing how the rest of your code is structured, it is hard to say, but I’d probably implement the whole thing as one or more state machines.

I have attached a small stand alone program to get this side of it working, hopefully this will show you what i want better.

// Multiple Modes with One button and controlled repitition

#include <LiquidCrystal.h> // Library for LCD Display


LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Intialise LCD and set display pins
      
    
// Constants  
  const int button = 16;
  const int Relay = 13;
  
// Variables  
  int mode = 1;
  int buttonPressed = 0;
  
  void setup (){
  pinMode(button, INPUT); // set button to input
  pinMode(Relay, OUTPUT); // set relay as output - currently LED
  lcd.begin(16,2); // set display characters & lines
  }
  void loop (){
    
    
  buttonPressed = digitalRead(button);
  if (buttonPressed == HIGH)
  {
    mode++; // Increment Mode by 1.
  }

  if (mode == 1) // Automatic Mode, Software Controlled.
  {
    lcd.setCursor(0,0);
    lcd.print(" AUTOMATIC MODE ");
    delay(500);
    lcd.noDisplay();
    lcd.display();
    // So i want it to display the above only once for 2 secs then keep displaying the following
    lcd.setCursor(0,0);
    lcd.print("MAINBATT =");
    lcd.setCursor(11,0);
    lcd.print(mainVolt); // this is in my full program (analog input)
    lcd.setCursor(0,1);
    lcd.print("AUX BATT =");
    lcd.setCursor(11,1);
    lcd.print(auxVolt);
// there are also a few timers and if statments controlling the
// automatic mode, other modes are just relay on & relay off
  }
  
  if (mode == 2)
  {
    lcd.setCursor(0,0);
    lcd.print("  MANUAL MODE   ");
    lcd.setCursor(1,0);
    lcd.print("  DISCONNECTED  ");
    digitalWrite(Relay, LOW);
    delay(500);
    lcd.noDisplay();
    lcd.display();
  }
  
  if (mode == 3)
  {
    lcd.setCursor(0,0);
    lcd.print("  MANUAL MODE   ");
    lcd.setCursor(0,0);
    lcd.print("LINKED BATTERIES");
    delay(500);
    lcd.noDisplay();
    lcd.display();
    digitalWrite(Relay, HIGH);
  }
  
 if (mode > 3)
  {
    mode = 0;
  } 
  }

You could factor the code that you want to run in two places into a single function, but it still isn't clear (to me) what the big picture is. A state-machine would allow you to get rid of the delays, and potentially make your sketch more responsive.

I recommend you start looking at and using switch/case statements.

Just to let everyone know i have worked this problem out so no need to reply
cheers