How can i use a push button to toggle between different LCD disply data

hello everyone i have a small project with an lcd and 5 different push buttons and i want to display GPS_data() by default and when i press the first push button connected to pin 22 (pullup) i want to display Temp_data(); and when i press the same button again i want it to go back and display the GPS_data again and so on for the rest of the buttons i want each button to display a certain set of data.

#include <TinyGPS.h> //Gps library 
TinyGPS gps;
#include<Wire.h> //Gyro library and also used for the temperature sensors 
const int MPU_addr=0x68; //constant value used for the setup of the gyroscope 
#define Voltage_sensor_PIN A7 //analog pin for voltage sensor
#include <math.h> //mostly used for the psychrometric calculations 
float E = 2.71828182845904; // exponential constant 
float N = 0.6687451584;     // equation constant
float Relative_Humidity;
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 2, d5 = 3, d6 = 4, d7 = 5;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

#include <SoftwareSerial.h>
//SoftwareSerial Bluetooth(8, 7);
#include <DallasTemperature.h> //Temp. sensor library
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 9
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
int currentButton1State ;
int state=LOW;
bool flag=0;

bool showingGPSData = true;
          // Variable to hold the current LED state
bool tempMode = false; 

void setup() {
  Serial1.begin(9600); //form gps to arduino mega  
  Serial.begin(9600);
  //Gyro_setup(); //setup for the gyroscope function
  lcd.begin(20,4);
//  Bluetooth.begin(9600);
   sensors.begin();// first temperature sensor setup 
   pinMode(22, INPUT_PULLUP);
}
void loop() 
{
   GPS_data();
   
currentButton1State=digitalRead(22);
  Temp_data();
       
    
   

}

note:this code is not complete but assue the functions display the data on the LCD
i tried a lot of ways but nothing is working for me.
also if necessery i can allocate one push button to go back to the defaulte and display GPS_data
any help is much appreciated.

Have a look at the state change detection example that comes with the IDE.

Basically you detect the change from HIGH to LOW and switch the mode. In pseudo code

void loop()
{
  static uint8_t mode;
  if (buttonState has changed && button is ISPRESSED)
  {
    if(mode == showGps)
    {
        mode = showTemp;
    }
   else
    {
        mode = showGPS;
    }
  }
  if (mode == showGps) displayGps();
  else displayTemp();
}

i already tried it but it is not working as it should

currentButton1State=digitalRead(22);

You need to detect when the button becomes pressed rather than when it is pressed
See the StateChangeDetection example in the IDE

Use the fact that a button has become pressed to toggle the state of one or more boolean variables. Then, in loop(), display the required data depending on the state of the booleans

Show your attempt at that.

Here it is:

#include <TinyGPS.h> //Gps library 
TinyGPS gps;
#include<Wire.h> //Gyro library and also used for the temperature sensors 
const int MPU_addr=0x68; //constant value used for the setup of the gyroscope 
#define Voltage_sensor_PIN A7 //analog pin for voltage sensor
#include <math.h> //mostly used for the psychrometric calculations 
float E = 2.71828182845904; // exponential constant 
float N = 0.6687451584;     // equation constant
float Relative_Humidity;
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 2, d5 = 3, d6 = 4, d7 = 5;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

#include <SoftwareSerial.h>
//SoftwareSerial Bluetooth(8, 7);
#include <DallasTemperature.h> //Temp. sensor library
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 9
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
int currentButton1State ;
int laststate=0;


bool showingGPSData = true;
          // Variable to hold the current LED state
bool tempMode = false; 

void setup() {
  Serial1.begin(9600); //form gps to arduino mega  
  Serial.begin(9600);
  //Gyro_setup(); //setup for the gyroscope function
  lcd.begin(20,4);
//  Bluetooth.begin(9600);
   sensors.begin();// first temperature sensor setup 
   pinMode(22, INPUT_PULLUP);
}
void loop() 
{
   // read the pushbutton input pin:
  currentButton1State = digitalRead(22);

  
  if (currentButton1State != laststate) {
    /
    if (currentButton1State == 0) {
     Temp_data();
      
    } else {
    
      GPS_data();
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  laststate = currentButton1State;
    /*  
   
     */
   

}

Assuming that you have pin 22 connected to GND then I would expect that when the button is currently pressed then the Temp_data() function will be called and the GPS_data() function will be called when the button is not currently pressed

Is that what happens ?

by defult the GPS_data() is called, when the button is pressed the Temp_data() is called but when the button is not pressed again the GPS_data() is called again.

Look at what UKHeliBob and I described; use a variable that changes when the state changes from HIGH to LOW.

Use that variable to display the desired data.

i dont quite understand do you mind explaining more, how can i do what you said and still keep Temp_data() called even if the state high and when it goes back to low(when i press it the second time)

Look at the pseudo code that I presented. Your are nearly there with your previous code in reply #7, just don't call the functions when you detect the change and only react on the change from High to LOW, only modify the variable and later use that variable.

thank you so much for the guidance.
it finally worked

currentButtonState = digitalRead(22);
  
  // Detect change from HIGH to LOW
  if (lastButtonState == HIGH && currentButtonState == LOW) {
    stateChanged = true;
  } else {
    stateChanged = false;
  }
  
  lastButtonState = currentButtonState;
  
  // If the state changed from HIGH to LOW, toggle modes
  if (stateChanged) {
    showingGPSData = !showingGPSData;
  }
  
  // Use the showingGPSData variable later in your code
  if (showingGPSData) {
    GPS_data();
  } else {
    Temp_data();
  }
  
  delay(20); 

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.