help with continuing temperature display

I have barometric sensor that will display temperature on lcd display and when I press button1 it will display temperature in real-time (change on display as the temp changes). As of right now it displays it but doesn’t update, I have to keep pressing the button1 in order to update the temp. I know I need to have a loop so it constatnly updating values, maybe a while loop but not sure what to use as a terminating condition.
Any hints or suggestions are very appreciated.

Here’s the full code

#include <SCP1000.h>

#include <LiquidCrystal.h>

#define SlaveSelect 10

int buttonPin1= 18;
int buttonPin2 = 19;
LiquidCrystal lcd(9, 8, 5, 4, 3, 2);
SCP1000 scp(SlaveSelect);

int prevButtonState1;
int prevButtonState2;

long startTime;
long elapsedTime;
int flag;
int flag2;
int buttonState1;
int buttonState2;

void setup()
{
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  digitalWrite(buttonPin1, LOW);  // pulldown resistors
  digitalWrite(buttonPin2,LOW);
  scp.init();
  lcd.begin(20, 4);
  lcd.setCursor(2,1);
  lcd.print("Trail Tracker Co.");}




void loop(){
  buttonState1 = digitalRead(buttonPin1);  //records current state of button1 and button2
  buttonState2 = digitalRead(buttonPin2);
  scp.readSensor();  //reads sensor 
  //button1 -> elapsed time/velocity button2 -> start/stop 
  if (buttonState1 == LOW && prevButtonState1== HIGH && flag == false){  // detects transition from High to low transition
    lcd.clear();
    startTime = (millis());
    
    lcd.setCursor(0,0);
    lcd.print("Time elapsed:");
    lcd.setCursor(0,1);
    lcd.print("Distance: ");
    lcd.setCursor(0,2);
    lcd.print("Real time:");
    lcd.setCursor(0,3);
    lcd.print("Temp: ");
    lcd.print(scp.TempC);  // prints out temperature in celcius
    flag = true;
    prevButtonState1 = buttonState1;// updates current and previous state
  }
  else if(buttonState1 == LOW && prevButtonState1== HIGH && flag == true){
    lcd.clear();
    lcd.print("Velocity:");
    double vel = velocity(50,389.5);
    lcd.print(vel);
    lcd.print("mi/hr");
    lcd.setCursor(0,1);
    lcd.print("Altitude:");
    lcd.setCursor(0,2);
    lcd.print("Coordinates:");
    flag = false;
    prevButtonState1 = buttonState1;
  }
  else if(buttonState2 == LOW && prevButtonState2== HIGH && flag2 == false) { // starts logging
    lcd.clear();
    lcd.setCursor(6,1);
    lcd.print("Start");
    flag2 = true;
    prevButtonState2 = buttonState2;
  }
  else if(buttonState2 == LOW && prevButtonState2 == HIGH && flag2 == true ){ //stops logging
    flag2 = false;
    lcd.clear();
    lcd.setCursor(2,1);
    lcd.print("Stop");
    prevButtonState2 = buttonState2;
  }

  else{
    prevButtonState1 = buttonState1;
    prevButtonState2 = buttonState2;
  }

}
double velocity(double acc, double time){

  return acc * time/3600;


}

You have a loop already. It's the loop function.

What you need to decide is what triggers the program to stop displaying the real-time temperature.

There are some things that you could do in your code to make it easier to help you.

if (buttonState1 == LOW && prevButtonState1== HIGH && flag == false)
  else if(buttonState2 == LOW && prevButtonState2== HIGH && flag2 == false)

Without studying the code, the meanings of buttonState1 and prevButtonState1 are pretty easy to guess, and to check to make sure that my guess is correct. But, flag? What's that?

The buttonState1 and buttonState2 variables follow a pattern. The prevButtonState1 and prevButtonState2 variables follow the same pattern. The flag and flag2 variables do not follow that same pattern.

  else{
    prevButtonState1 = buttonState1;
    prevButtonState2 = buttonState2;
  }

The previous button state variables should be set on EVERY pass through loop, not just sometimes. It's hard to follow (with names like flag and flag2 being used) under what circumstances, if ever, these variables are reset.

It would also be easier to understand what was happening, or supposed to happen if code like this:

  if (buttonState1 == LOW && prevButtonState1== HIGH && flag == false){  // detects transition from High to low transition
    lcd.clear();
    startTime = (millis());
    
    lcd.setCursor(0,0);
    lcd.print("Time elapsed:");
    lcd.setCursor(0,1);
    lcd.print("Distance: ");
    lcd.setCursor(0,2);
    lcd.print("Real time:");
    lcd.setCursor(0,3);
    lcd.print("Temp: ");
    lcd.print(scp.TempC);  // prints out temperature in celcius
    flag = true;
    prevButtonState1 = buttonState1;// updates current and previous state
  }

looked like this:

  if (buttonState1 == LOW && prevButtonState1== HIGH && flag == false)
  { 
    ShowTimeDistAndTemp();
    flag = true;
    prevButtonState1 = buttonState1;// updates current and previous state
  }

If you make some of these changes, it should be pretty apparent why the button needs to be pressed in order to trigger an update of the display.

I know that void loop() is a looping functions, so it loops through my if statements constantly checking for button states. But its not looping inside the if statements to read the sensor continuosly and update the temperature changes. The variable flag, I know it might not be clear, I wasn’t sure what to call it lol. So here’s its purpose:
Each button is used for 2 purposes. So there are four functions total.
Button1 pressed the first time it will display: Time & Distance & Temp.
Then it will update the flag to true. So next time button1 is pressed it will check if its true to display a different set of information by pressing the same button1. Now it will display Velocity&Altitude.
Same thing with button2 using flag2. On the first press (eventually) will start logging data to an sd card, on the second press will stop logging.

I did add a separate function for TEMPERATURE by your recomendation. It is alot better. It does display changing temp BUT now my screen is stuck and won’t change or respond to any button presses. My while condition is not working properly… PLZ help

void temperature(){
  while((!(buttonState1 == LOW && prevButtonState1 == HIGH)) || (!(buttonState2 == LOW && prevButtonState2 == HIGH))){
    scp.readSensor();
    lcd.setCursor(0,3);
    lcd.print("Temp: ");
    lcd.print(scp.TempC);
    delay(500);
  }

here’s my full code as well

#include <SCP1000.h>

#include <LiquidCrystal.h>

#define SlaveSelect 10

int buttonPin1= 18;
int buttonPin2 = 19;
LiquidCrystal lcd(9, 8, 5, 4, 3, 2);
SCP1000 scp(SlaveSelect);

int prevButtonState1;
int prevButtonState2;

long startTime;
long elapsedTime;
int flag;
int flag2;
int buttonState1;
int buttonState2;

void setup()
{
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  digitalWrite(buttonPin1, LOW);  // pulldown resistors
  digitalWrite(buttonPin2,LOW);
  scp.init();
  lcd.begin(20, 4);
  lcd.setCursor(2,1);
  lcd.print("Trail Tracker Co.");}




void loop(){
  buttonState1 = digitalRead(buttonPin1);  //records current state of button1 and button2
  buttonState2 = digitalRead(buttonPin2);
   //scp.readSensor();  //reads sensor 
  //button1 -> elapsed time/velocity button2 -> start/stop 
  if (buttonState1 == LOW && prevButtonState1== HIGH && flag == false){  // detects transition from High to low transition
    lcd.clear();
    startTime = (millis());
    prevButtonState1 = buttonState1;// updates current and previous state
    prevButtonState2 = buttonState2;
    lcd.setCursor(0,0);
    lcd.print("Time elapsed:");
    lcd.setCursor(0,1);
    lcd.print("Distance: ");
    lcd.setCursor(0,2);
    lcd.print("Real time:");
    temperature();
    //lcd.setCursor(0,3);
    
    //lcd.print("Temp: ");
    //lcd.print(scp.TempC);  // prints out temperature in celcius
    
    
    flag = true;
    
  }
  else if(buttonState1 == LOW && prevButtonState1== HIGH && flag == true){
    lcd.clear();
    lcd.print("Velocity:");
    double vel = velocity(50,389.5);
    lcd.print(vel);
    lcd.print("mi/hr");
    lcd.setCursor(0,1);
    lcd.print("Altitude:");
    lcd.setCursor(0,2);
    lcd.print("Coordinates:");
    flag = false;
    prevButtonState1 = buttonState1;
    prevButtonState2 = buttonState2;
  }
  else if(buttonState2 == LOW && prevButtonState2== HIGH && flag2 == false) { // starts logging
    lcd.clear();
    lcd.setCursor(6,1);
    lcd.print("Start");
    flag2 = true;
    prevButtonState1 = buttonState1;
    prevButtonState2 = buttonState2;
  }
  else if(buttonState2 == LOW && prevButtonState2 == HIGH && flag2 == true ){ //stops logging
    flag2 = false;
    lcd.clear();
    lcd.setCursor(2,1);
    lcd.print("Stop");
    prevButtonState1 = buttonState1;
    prevButtonState2 = buttonState2;
  }

  else{
    prevButtonState1 = buttonState1;
    prevButtonState2 = buttonState2;
  }

}
double velocity(double acc, double time){

  return acc * time/3600;


}
void temperature(){
  while((!(buttonState1 == LOW && prevButtonState1 == HIGH)) || (!(buttonState2 == LOW && prevButtonState2 == HIGH))){
    scp.readSensor();
    lcd.setCursor(0,3);
    lcd.print("Temp: ");
    lcd.print(scp.TempC);
    delay(500);
  }        
  
}

Each button is used for 2 purposes. So there are four functions total. Button1 pressed the first time it will display: Time & Distance & Temp. Then it will update the flag to true.

So, if flag is true, do one thing. If it's false, do something else. The same would be said of the variable was called TimeTempDist. Now, that's kind of long to type a lot, but it clearly indicates at least one of the functions of the variable.

Even better though is to have two booleans. Each press of the button sets both. Then, have two functions. Which one gets called depends on how many times the button was pushed. For a single button, the code might look like this:

bool TimeTempDist = true;
bool VelocityAltitude = false;

void loop()
{
   buttonState1 = digitalRead(button1Pin);
   if(buttonState1 == HIGH && prevButtonState1 == LOW)
   {
      TimeTempDist != TimeTempDist; // Set to opposite of current state
      VelocityAltitude != VelocityAltitude;
   }
   prevButtonState1 = buttonState1;

   if(TimeTempDist)
     ShowTimeTempDist();
   if(VelocityAltitude)
     ShowVelocityAltitude();
}

This is easily extended to any number of buttons. Notice also that it separates the action from the button press status handling.

Now, each time through loop, the code will see if a button was pressed,and update what else it needs to do. Then, it will do whatever else it needs to do, displaying the correct (and current) data on the LCD.

I did add a separate function for TEMPERATURE by your recomendation. It is alot better. It does display changing temp BUT now my screen is stuck and won't change or respond to any button presses. My while condition is not working properly... PLZ help

Notice that that code never modifies the values in the buttonState and prevButtonState variables.

Writing code like: buttonState = digitalRead(buttonPin); does not bind the buttonState variable to the state of the pin, so that future button presses are handle automatically. It takes a snapshot of the current state of the pin.

Your code would work, if you had an escape button that you read each time through the while loop. But that requires another switch and an unnatural user interface. Look at my example above, and see how it handles the same situation.

PaulS, thank you for trying to help me.. however my buttons are working perfectly fine. My problem is displaying sensor values. It responds to all button pushes and does display correct information and current information but ONLY after I press the button. What I mean is when I press the button1 it will display the temperature. But as the temperature changes it doesn't update the current state unless I press the button1 again to update the changed temperature. What I'm thinking is after the IF statement like if (button1 pressed) then go into loop to constantly update sensor values. My problem is the condition of the loop to terminate and respond if for example i press button2. The condition im thinking is to terminate when button1 is pressed again or button2 is pressed. But for some reason i can't get it to work. I think I'm too confused with all button states...

I'm really frastrated with it... I know its simple, but I'm stuck.

Think about what you want to have happen. You want the Arduino to go somewhere else (another function) and do something (display time, temp, and distance), but still be here (in loop) to respond to button presses. Can't happen.

On each pass through loop, you need to check the buttons, as you are doing. After checking the buttons, you (may) need to do something.

You do not need to do something as soon as you detect a button press.

That is what you are doing now, and what is making it difficult.

If you create functions to display the time, the temp, the distance, the velocity, etc. and to read the button presses and set flags, you will see the problem:

void loop() { ReadButtonOne(); ReadButtonTwo();

if(showTemp) ShowTemp(); if(showTime) ShowTime(); // More tests and function calls. }

Reading the buttons this way isolates the reading of the button from the event that is to occur if the button is pressed n times.

I really recommend that you try this approach.