Using buttons to toggle 'pages' on an LCD

Hi all,

OK so I'm slowly getting to grips with everything Arduino, but one thing I don't have a clue where start is pushing a button to make the Arduino do something else.

In this case, I want to toggle between multiple LCD "pages" by pressing a button.

So the LCD will for example be showing indoor and outdoor temperature, and I would want to change this to show humidity, the time, or whatever. I'd like to toggle this by pressing a button.

Can anyone walk me through the process with some example code?

I have no programming or C experience and Arduino is my first foray into it all, so please treat me as a noob. :)


I assume you haven't tried the Examples on the tutorial page. About the second example is:

This reads the state of a button and writes it to the serial port. Obviously, you could use it to increment your page counter instead.

Thanks, but that doesn't really help..! Sorry if that sounds churlish, but I'd like to know how exactly to 'store' different 'pages' (i.e. a selection of lcd.setCursor and lcd.print commands) and then move between them.

Many thanks

This is an easy thing to do, once you get your head round the idea.

I do this (or somthing similiar) in most of my projects, and while I dont know if its the best way, I do it by using a variable to keep an idea of what page Im on, and then displaying the specified screen.

dummy code:

int screenSelect=0;
void main(){
if (button_pressed) change menuScreen;

if (screenSelect==0)  screen0()
if (screenSelect==1) screen1()... etc

//display screen 0 stuff here

//display screen 1 stuff here

I have a different display() routine for each screen and I call them depending on which screen i need to see.

I hope this helps.

edit:added extra dummy code

Thanks, that partly makes sense but I get the feeling there should be more added? Do you have a full sketch I could have a nosey at, and digest?

Thanks :)

Post your code. Someone can add to it. That's easiest way to understand. You already understand most of the code, cause you wrote them. I suggest the helpers to just use a instead of buttons class or digital read. The OP may need more time to understand if we used buttons to start with.

I am trying to do something similar. I am trying to set up my first LCD with an ethernet shield that is hooked up to my crock pot. What I am trying to do (initially) is to have the LCD read “Crock pot is on” when I toggle a PSII via a client that turns on the crock pot, and be blank or off when I toggle the pot off. I can get it to do the first part, but am struggling to get the second part to work. Any ideas? I am a bit of a noob

#include <SPI.h>
#include <Ethernet.h>
// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xF8, 0x28 }; //physical mac address
byte ip[] = { 192, 168, 1, 177 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(12349); //server port

String readString; 


void setup(){

  pinMode(8, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);

  //enable serial data print 
  Serial.println("servertest1"); // so I can keep track of what is loaded

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c =;

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 

        //if HTTP request has ended
        if (c == '\n') {

          Serial.println(readString); //print to serial monitor for debuging 

          //now output HTML data header
             if(readString.indexOf('?') >=0) { //don't send new page
               client.println("HTTP/1.1 204 Zoomkat");
             else {
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");

          client.println("<TITLE>Chang's on off</TITLE>");

          client.println("<h1 style=(\"/text-align:center\">Chang's Switch</H1>");
          client.println("<a href=\"/?on\" target=\"inlineframe\">ON</a>"); 
          client.println("<a href=\"/?off\" target=\"inlineframe\">OFF</a>"); 

          //client.println("<IFRAME name=inlineframe src=\"res://D:/WINDOWS/dnserror.htm\" width=1 height=1\">");
          client.println("<IFRAME name=inlineframe style=\"display:none\" >");          


          //stopping client

          ///////////////////// control arduino pin
          if(readString.indexOf("on") >0)//checks for on
            digitalWrite(8, HIGH);    // set pin 4 high
            Serial.println("Led On");
            lcd.begin(20, 4);
  // Print a message to the LCD.
  lcd.print("Crock pot is on");
          if(readString.indexOf("off") >0)//checks for off
            digitalWrite(8, LOW);    // set pin 4 low
            Serial.println("Led Off");
            lcd.begin(20, 4);
  // Print a message to the LCD.
  lcd.print("Crock pot is off");
          //clearing string for next read



Probably should start a new thread since your question is not helping the OP's question.

You need to move this to setup() lcd.begin(20, 4);

The on and off codes look the same so I don't know what you mean the on code is working but off code is not. What happens if you receive on and then receive off?

Hi, I am most of the way through a project that does almost exactly what you want. In may case its a radio controlled car and go kart lap timer. The project has an LCD Display and buttons which are used to cycle the display through 'record','session summary','session detail' modes. Within each mode the button purposes can change so it could have become quite cumbersome to code. As it is I have come up with a very simple approach. If you can hold on until this evening I hope to be in a position to post my approach and enough sample code to get you going.

Here is a quick outline of the project which I posted last night -

Duane B

Hi, hcanning, with reference to another post I have just made, I am assuming that you are in the 'would like to know more and willing to try' camp. If this is the case, please have a read through my latest blog post here and comment on whether it is helpful/interesting to you as a self described noob.

If you think it might be helpful but there are some areas that are not sufficiently clear again, let me know. Even if its a bit above your current level, its still good feedback to indicate whether you think with more generic background reading you could get there.

ghoti, Can you also have a look, for no more reason than that you are from Newport, Gwent and so am I.

Duane B.