use buttns on arduino R3 uno with lcd keypad shield

hey all,
i'm new here. i never have had programming exept siemens plc.
i'm trying to make a display that reads 4 temp analog sensors.
it prints 2 data lines (this worked fine)
but for 4 data i would like that sensor 1+2 is displaying and after butten up or down sensor 3+4 is displaying.
can somebody help me on the way?

#include <LiquidCrystal.h>
int ThermistorPin1 = 1;
int ThermistorPin2 = 2;
int ThermistorPin3 = 3;
int ThermistorPin4 = 4;

int Vo1;
int Vo2;
int Vo3;
int Vo4;
float R1 = 10000;
float logR2, R2, T;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;

LiquidCrystal lcd(8,9, 4, 5, 6, 7);
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
}
//vanaf de loop de lus programmeren

//sensor1 uitlaatgastemp ketel
void loop() {
Vo1 = analogRead(ThermistorPin1);
R2 = R1 * (1023.0 / (float)Vo1 - 1.0);
logR2 = log(R2);
T = (1.0 / (c1 + c2logR2 + c3logR2logR2logR2));
T = T - 273.15;
lcd.setCursor(0,0);
lcd.print("Tgas=");
lcd.print(T);
lcd.print(" C");

//sensor 2 uitlaattemp gas schouw
Vo2 = analogRead(ThermistorPin2);
R2 = R1 * (1023.0 / (float)Vo2 - 1.0);
logR2 = log(R2);
T = (1.0 / (c1 + c2logR2 + c3logR2logR2logR2));
T = T - 273.15;
lcd.setCursor(0,1);
lcd.print("Tschw=");
lcd.print(T);
lcd.print(" C");

//sensor 3 boiler temp
Vo3 = analogRead(ThermistorPin3);
R2 = R1 * (1023.0 / (float)Vo3 - 1.0);
logR2 = log(R2);
T = (1.0 / (c1 + c2logR2 + c3logR2logR2logR2));
T = T - 273.15;
lcd.setCursor(0,0);
lcd.print("Twat=");
lcd.print(T);
lcd.print(" C");
// delay en clear altijd opt laatste van de loop anders flikkerend scherm

//sensor 4 omgevingstemperatuur
Vo4 = analogRead(ThermistorPin4);
R2 = R1 * (1023.0 / (float)Vo4 - 1.0);
logR2 = log(R2);
T = (1.0 / (c1 + c2logR2 + c3logR2logR2logR2));
T = T - 273.15;
lcd.setCursor(0,1);
lcd.print("Tomg=");
lcd.print(T);
lcd.print(" C");

delay(1000);// delay en clear altijd opt laatste van de loop anders flikkerend scherm
lcd.clear();

}

Welcome to the forum! Please read the sticky post at the top of this page to see how to use code tags to post your code. It helps up help you!

To get a button, wire up a switch between some Arduino pin and ground and this code should work:

Note: In your code, you are using pin#4 for a sensor AND the lcd. It won't work!

#include <LiquidCrystal.h>
const int ThermistorPin1 = 1;
const int ThermistorPin2 = 2;
const int ThermistorPin3 = 3;
const int ThermistorPin4 = 4; // <---- you use this pin for LCD! need to change

const int ButtonPin = 10;

int Vo1;
int Vo2;
int Vo3;
int Vo4;
float R1 = 10000;
float logR2, R2, T;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  pinMode( ButtonPin, INPUT_PULLUP );
}
//vanaf de loop  de lus programmeren


//sensor1 uitlaatgastemp ketel
void loop() {

  if ( digitalRead( ButtonPin ) == HIGH ) {
    // button not pressed - display sensor 1+2
    Vo1 = analogRead(ThermistorPin1);
    R2 = R1 * (1023.0 / (float)Vo1 - 1.0);
    logR2 = log(R2);
    T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
    T = T - 273.15;
    lcd.setCursor(0, 0);
    lcd.print("Tgas=");
    lcd.print(T);
    lcd.print(" C");
  
    //sensor 2 uitlaattemp gas schouw
    Vo2 = analogRead(ThermistorPin2);
    R2 = R1 * (1023.0 / (float)Vo2 - 1.0);
    logR2 = log(R2);
    T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
    T = T - 273.15;
    lcd.setCursor(0, 1);
    lcd.print("Tschw=");
    lcd.print(T);
    lcd.print(" C");
  } else {
    // button is pressed - display sensor 3+4
    //sensor 3 boiler temp
    Vo3 = analogRead(ThermistorPin3);
    R2 = R1 * (1023.0 / (float)Vo3 - 1.0);
    logR2 = log(R2);
    T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
    T = T - 273.15;
    lcd.setCursor(0, 0);
    lcd.print("Twat=");
    lcd.print(T);
    lcd.print(" C");
    // delay en clear altijd opt laatste van de loop anders flikkerend scherm
  
    //sensor 4 omgevingstemperatuur
    Vo4 = analogRead(ThermistorPin4);
    R2 = R1 * (1023.0 / (float)Vo4 - 1.0);
    logR2 = log(R2);
    T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
    T = T - 273.15;
    lcd.setCursor(0, 1);
    lcd.print("Tomg=");
    lcd.print(T);
    lcd.print(" C");
  }
  delay(1000);// delay en clear altijd opt laatste van de loop anders flikkerend scherm
  lcd.clear();
}

oops. I missed the title- you are using the keypad attached to the LCD. In that case, you need to use lcd.readButtons() and act accordingly.

Blh64,
Thanx for the heads up (pin4) and the quick respons. I will use youre firts post and insert your second.
I hope it works.
Thanx!

hey again,
it almost does what i want.

if i press up or down it shows the wanted sensors.
but i have to keep pushing up.
is it possible that it stays in the last state.
so if push up it keeps showing 1+2 until i push down?

#include <LiquidCrystal.h>


// define some values used by the panel and buttons
int lcd_key     = 0;
int adc_key_in  = 0;
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5

// read the buttons
int read_LCD_buttons()
{
 adc_key_in = analogRead(0);      // read the value from the sensor 
 // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
 // we add approx 50 to those values and check to see if we are close
 if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
 // For V1.1 us this threshold
 if (adc_key_in < 50)   return btnRIGHT;  
 if (adc_key_in < 250)  return btnUP; 
 if (adc_key_in < 450)  return btnDOWN; 
 if (adc_key_in < 650)  return btnLEFT; 
 if (adc_key_in < 850)  return btnSELECT;  

 // For V1.0 comment the other threshold and use the one below:
/*
 if (adc_key_in < 50)   return btnRIGHT;  
 if (adc_key_in < 195)  return btnUP; 
 if (adc_key_in < 380)  return btnDOWN; 
 if (adc_key_in < 555)  return btnLEFT; 
 if (adc_key_in < 790)  return btnSELECT;   
*/


 return btnNONE;  // when all others fail, return this...
}


const int ThermistorPin1 = 1;
const int ThermistorPin2 = 2;
const int ThermistorPin3 = 3;
 // <----pin 4 you use this pin for LCD! need to change



int Vo1;
int Vo2;
int Vo3;
int Vo4;
float R1 = 10000;
float logR2, R2, T;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
 lcd.setCursor(0,0);
}
//vanaf de loop  de lus programmeren


//sensor1 uitlaatgastemp ketel
void loop() {
 lcd_key = read_LCD_buttons();  // read the buttons

 switch (lcd_key)               // depending on which button was pushed, we perform an action
 {
  case btnNONE:
     {
    // button not pressed - display sensor 1+2
    Vo1 = analogRead(ThermistorPin1);
    R2 = R1 * (1023.0 / (float)Vo1 - 1.0);
    logR2 = log(R2);
    T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
    T = T - 273.15;
    lcd.setCursor(0, 0);
    lcd.print("Tgas=");
    lcd.print(T);
    lcd.print(" C");
  
    //sensor 2 uitlaattemp gas schouw
    Vo2 = analogRead(ThermistorPin2);
    R2 = R1 * (1023.0 / (float)Vo2 - 1.0);
    logR2 = log(R2);
    T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
    T = T - 273.15;
    lcd.setCursor(0, 1);
    lcd.print("Tschw=");
    lcd.print(T);
    lcd.print(" C");
    
  delay(1000);// delay en clear altijd opt laatste van de loop anders flikkerend scherm in miliseconde
  lcd.clear();
  break;
  
  } 
  
  case btnUP: {
    // button is pressed - display sensor 3+4
    //sensor 3 boiler temp
    Vo3 = analogRead(ThermistorPin3);
    R2 = R1 * (1023.0 / (float)Vo3 - 1.0);
    logR2 = log(R2);
    T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
    T = T - 273.15;
    lcd.setCursor(0, 0);
    lcd.print("Twat=");
    lcd.print(T);
    lcd.print(" C");
    // delay en clear altijd opt laatste van de loop anders flikkerend scherm
 

  }
  delay(1000);// delay en clear altijd opt laatste van de loop anders flikkerend scherm in miliseconde
  lcd.clear();
  break;
  
}
}

You can accomplish that using a 'lastState' variable. do a lcd.readButtons() and then check the result vs. 'lastState' and if they are the same or no button is pressed, do nothing. If the other button is now pressed, change 'lastState'. Update what is on the display based on this variable.

Actual code left as an exercise for the reader :slight_smile: