2 pushbuttons controlling seperate lcd pages with uglib

Hi, I know this is meant to be simple, but really struggling with creating this code, as I'm very new to the language.
I have 2 push buttons that need to turn an LED on and the other off. I got this part working...
I also need the LCD screen to display to separate pages when buttons are pressed, one saying "RECORDING IN PROGRESS" and the other "RECORDING STOPPED". Once either button is pressed, the display stay on lcd.
Thanks a lot in advance!

test_21_plus_uglib_lcd.ino (2.24 KB)

(deleted)

Thank you spycatcher2k for the superquick reply and for fixing this code, unfortunately, it doesn't seem to work.
I'm not sure what could be wrong...
I have the LCD displaying "HELLO WORLD", I also tried it with a code that has separate screens displaying one after another on a loop and it works just fine!
The push buttons control the LED perfect.
When I upload the desired code and press the buttons, no text comes up...
I attached a pic of my circuit, but here it is, written out too:
5V LED- +, pin 13
PushButton 1- 10kresistor to Ground and Pin 10, 5V
PushButton2- 10k resistor to Ground and Pin 9, 5V
LCD:
GND-Ground
VCC-5V
VO- 10k Potentiometer
RS- 4
RW-5
E-6
PSB- Ground
RST-7
BLA-3.3V
BLK- Ground

Any idea what could be the problem? My head is melted!
Thanks a lot!

test_25.ino (2.31 KB)

Hi! Still having trouble getting this LCD working, have 2 push buttons that supposed to operate 1 led on/off and display different messages on the screen.
I've been trying to assign both functions to the push-buttons, without any luck. Now I tried to assign the led to the push-buttons and the LCD to the led.
With this code, even the LED won't work.

//lcd 
#include "U8glib.h"  
// A linha abaixo define as ligacoes e deve ser 
// ajustada conforme o display utilizado. 
U8GLIB_ST7920_128X64_1X u8g(6, 5, 4 ,7); //Enable, RW, RS, RESET
int display = 1;
 

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin1 = 9;     // the number of the pushbutton pin 2
const int buttonPin2 = 10;     // the number of the pushbutton pin 3
int ledPin =  12;      // the number of the LED pin

// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;
int ledState = 0;


void u8g_prepare()
{  
  u8g.setFont(u8g_font_6x10);  
  u8g.setFontRefHeightExtendedText();  
  u8g.setDefaultForegroundColor();  
  u8g.setFontPosTop();  
}  

void u8g_Screen1()  //Screen 1 - RECORDING IN PROGRESS  
{
  u8g.setFont(u8g_font_9x18B);

  u8g.drawStr( 5, 28, "RECORDING IN");  
  u8g.drawStr( 5, 45, "PROGRESS");  

} 
void u8g_Screen2()  //Screen 2 - RECORDING STOPPED  
{
  u8g.setFont(u8g_font_9x18B);

  u8g.drawStr( 5, 28, "RECORDING ");  
  u8g.drawStr( 5, 45, "STOPPED");  
} 
 
void draw(){
  u8g.setFont(u8g_font_9x18B);

  u8g.drawStr( 5, 28, "RECORDING IN");  
  u8g.drawStr( 5, 45, "PROGRESS");  
}

void draw2(){
  u8g.setFont(u8g_font_9x18B);

  u8g.drawStr( 5, 28, "RECORDING ");  
  u8g.drawStr( 5, 45, "STOPPED"); 
}

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT); 
  pinMode(ledPin, INPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin2, INPUT);

  //lcd
  u8g.setFont(u8g_font_unifont);
  u8g.setColorIndex(1); // Instructs the display to draw with a pixel on. 
  
  // flip screen, if required  
  //u8g.setRot180();  
   
  // assign default color value  
  if ( u8g.getMode() == U8G_MODE_R3G3B2 )   
   u8g.setColorIndex(255);   // white  
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT )  
   u8g.setColorIndex(1);     // max intensity  
  else if ( u8g.getMode() == U8G_MODE_BW )  
   u8g.setColorIndex(1);     // pixel on  
    
  //u8g.setContrast(0x30);  
}



void loop() {
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);
  // read the state of the pushbutton value:
  buttonState2 = digitalRead(buttonPin2); 
  ledState= digitalRead (ledPin);

  //lcd

 {
  // check whether a  pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState1 == HIGH) {    

    // turn LED on:
    delay(5);  
    digitalWrite(ledPin, HIGH);  
   }

    if (buttonState2 == HIGH) {    
      // turn LED on:    
      delay(5);
      digitalWrite(ledPin, LOW); }
 }
 
 {
   if (ledState == HIGH)
   {
   delay(2);
   u8g_Screen1();
    do
      {
      draw();
      }
     while (u8g_Screen2); 
     }
 


 
   if (ledState == LOW)
   {
   delay(2);
   u8g.firstPage();
    do
      {
      draw2();
      }
     while (u8g.nextPage()); 
     }
 }
}

When I have the code only for the Led and not the LCD, the Led works as it's supposed to.

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin1 = 9;     // the number of the pushbutton pin 2
const int buttonPin2 = 10;     // the number of the pushbutton pin 3
const int ledPin =  12;      // the number of the LED pin

// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;


void setup() {
 // initialize the LED pin as an output:
 pinMode(ledPin, OUTPUT);      
 // initialize the pushbutton pin as an input:
 pinMode(buttonPin1, INPUT);
 // initialize the pushbutton pin as an input:
 pinMode(buttonPin2, INPUT);    
}

void loop(){
 // read the state of the pushbutton value:
 buttonState1 = digitalRead(buttonPin1);
 // read the state of the pushbutton value:
 buttonState2 = digitalRead(buttonPin2);

 // check whether a  pushbutton is pressed.
 // if it is, the buttonState is HIGH:
 
 if (buttonState1 == HIGH) {    
   // turn LED on:   // Tweet that the washer is done 
   digitalWrite(ledPin, HIGH);  
 }
 if (buttonState2 == HIGH) {    
   // turn LED on:    //Tweet that the dryer is done
   digitalWrite(ledPin, LOW);  
 }
}

The LCD works perfectly with this code: (it has the same display, but instead of buttons controlling the display, it plays the 2 screens on a loop)

/*  
GraphicsTest.pde  
>>> Before compiling: Please remove comment from the constructor of the   
>>> connected graphics display (see below).  
  
Universal 8bit Graphics Library, http://code.google.com/p/u8glib/  
  
Copyright (c) 2012, olikraus@gmail.com  
All rights reserved.  
 
Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions 
are met:  
 
* Redistributions of source code must retain the above copyright notice, 
  this list of conditions and the following disclaimer.  
     
* Redistributions in binary form must reproduce the above copyright 
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.  
  
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND   
  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,   
  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF   
  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE   
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR   
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,   
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT   
  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;   
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER   
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,   
  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)   
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   
  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.   
*/  
   
#include "U8glib.h"  
// A linha abaixo define as ligacoes e deve ser 
// ajustada conforme o display utilizado. 
U8GLIB_ST7920_128X64_1X u8g(6, 5, 4 ,7); //Enable, RW, RS, RESET  
int display = 1;  
   
void u8g_prepare() 
{  
  u8g.setFont(u8g_font_6x10);  
  u8g.setFontRefHeightExtendedText();  
  u8g.setDefaultForegroundColor();  
  u8g.setFontPosTop();  
}  
   
void u8g_Screen1()  //Screen 1 - RECORDING IN PROGRESS  
{
  u8g.setFont(u8g_font_9x18B);
  
  u8g.drawStr( 5, 28, "RECORDING IN");  
  u8g.drawStr( 5, 45, "PROGRESS");  
    
} 
void u8g_Screen2()  //Screen 2 - RECORDING STOPPED  
{
  u8g.setFont(u8g_font_9x18B);
 
  u8g.drawStr( 5, 28, "RECORDING ");  
  u8g.drawStr( 5, 45, "STOPPED");  
    
}  
   

   
void draw() //Rotina Desenho  
{
  u8g_prepare();  
  switch(display) //Carrega a tela correspondente  
  {
   case 1:  
    u8g_Screen1();  
    break;  
   case 2:  
    u8g_Screen2();  
    break;  
   
  }  
}  
   
void setup() 
{  
  // flip screen, if required  
  //u8g.setRot180();  
   
  // assign default color value  
  if ( u8g.getMode() == U8G_MODE_R3G3B2 )   
   u8g.setColorIndex(255);   // white  
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT )  
   u8g.setColorIndex(1);     // max intensity  
  else if ( u8g.getMode() == U8G_MODE_BW )  
   u8g.setColorIndex(1);     // pixel on  
    
  //u8g.setContrast(0x30);  
}  
   
void loop() 
{  
  // picture loop   
  for (display =1; display < 3; display++) //Carrega as telas de 1 a 7  
  {  
    u8g.firstPage();   
    do 
    {  
    draw();  
    } 
    while( u8g.nextPage() );  
    delay(3000);  //Pausa de 3 segundos e reinicia o processo  
  }  
}

I also put the LCD onto a seperate power supply, because I thought the buttons eating up power might be causing disruption it getting the signal. But changed nothing either. (I attached a pic of the circuit.)

I'm very new to this whole thing and really trying, but no success.
Any pointers are welcomed!
Thank you!

Hi, I got it all working there, so thought I'd post the finished code, in case it would be helpful to anyone...

//Credits:
/*   
Universal 8bit Graphics Library, http://code.google.com/p/u8glib/  
  
Copyright (c) 2012, olikraus@gmail.com  
All rights reserved.  
 
Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions 
are met:  
 
* Redistributions of source code must retain the above copyright notice, 
  this list of conditions and the following disclaimer.  
     
* Redistributions in binary form must reproduce the above copyright 
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.  
  
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND   
  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,   
  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF   
  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE   
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR   
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,   
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT   
  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;   
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER   
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,   
  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)   
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   
  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 

   push button variables:
   created 2005
   by DojoDave <http://www.0j0.org>
   modified 30 Aug 2011
   by Tom Igoe
   modified 10 Apr 2012
   by mpilchfamily
   http://www.instructables.com/answers/Arduino-coding-to-allow-separate-push-buttons-to-t/

   graphics code modified from:
   http://www.arduinoecia.com.br/2013/09/display-grafico-lcd-128x64-st7920.html

*/  


//lcd
#include "U8glib.h"
// A linha abaixo define as ligacoes e deve ser
// ajustada conforme o display utilizado.
U8GLIB_ST7920_128X64_1X u8g(6, 5, 4 , 7); //Enable, RW, RS, RESET
int display = 1;


// constants won't change. They're used here to
// set pin numbers:
const int buttonPin1 = 9;     // the number of the pushbutton pin 2
const int buttonPin2 = 10;     // the number of the pushbutton pin 3
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;


void u8g_prepare()
{
  u8g.setFont(u8g_font_6x10);
  u8g.setFontRefHeightExtendedText();
  u8g.setDefaultForegroundColor();
  u8g.setFontPosTop();
}





void draw() {

  u8g.setFont(u8g_font_9x18B);

  u8g.drawStr( 5, 28, "RECORDING IN");
  u8g.drawStr( 5, 45, "PROGRESS");
}

void draw2 () {
  u8g.setFont(u8g_font_9x18B);

  u8g.drawStr( 5, 28, "RECORDING ");
  u8g.drawStr( 5, 45, "STOPPED");
}

  void setup() {
    // initialize the LED pin as an output:
    pinMode(ledPin, OUTPUT);
    
    // initialize the pushbutton pin as an input:
    pinMode(buttonPin1, INPUT);
    // initialize the pushbutton pin as an input:
    pinMode(buttonPin2, INPUT);

    //lcd
    u8g.setFont(u8g_font_unifont);
    u8g.setColorIndex(1); // Instructs the display to draw with a pixel on.

    // flip screen, if required
    //u8g.setRot180();

    // assign default color value
    if ( u8g.getMode() == U8G_MODE_R3G3B2 )
      u8g.setColorIndex(255);   // white
    else if ( u8g.getMode() == U8G_MODE_GRAY2BIT )
      u8g.setColorIndex(1);     // max intensity
    else if ( u8g.getMode() == U8G_MODE_BW )
      u8g.setColorIndex(1);     // pixel on

    //u8g.setContrast(0x30);
  
  
}



  void loop() {
    // read the state of the pushbutton value:
    buttonState1 = digitalRead(buttonPin1);
    // read the state of the pushbutton value:
    buttonState2 = digitalRead(buttonPin2);
   
    
      // check whether a  pushbutton is pressed.
      // if it is, the buttonState is HIGH:
      if (buttonState1 == HIGH) {

        // turn LED on:
        delay(5);
        digitalWrite(ledPin, HIGH);

         u8g.firstPage();
        do
        {
          draw();
        }
        while (u8g.nextPage());
      }

      if (buttonState2 == HIGH) {
        // turn LED on:
        delay(5);
        digitalWrite(ledPin, LOW);

        u8g.firstPage();
        do
        {
          draw2();
        }
        while (u8g.nextPage());
      }
  }