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. :)

Thanks.

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

http://arduino.cc/en/Tutorial/DigitalReadSerial

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
}

screen0(){
//display screen 0 stuff here
}

screen1(){
//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 serial.read 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);
  server.begin();

  //enable serial data print 
  Serial.begin(9600); 
  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 = client.read();

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

          //store characters to string 
          readString += c; 
          //Serial.print(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");
               client.println();
               client.println();  
             }
             else {
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

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

          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\" >");          
          client.println("</IFRAME>");

          client.println("</BODY>");
          client.println("</HTML>");
             }

          delay(1);
          //stopping client
          client.stop();

          ///////////////////// 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
          readString="";

        }
      }
    }
  }
}

Changums,

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 -

http://rcarduino.blogspot.com/2012/02/rc-lap-timer-go-kart-lap-timer-part-1.html

Duane B

rcarduino.blogspot.com

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.

http://rcarduino.blogspot.com/2012/02/rc-lap-timer-go-kart-lap-timer-part-2.html

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

Duane B.