Hold and display function for pressure sensor

I have a pressure sensor connected to my Nano with an OLED screen showing the output. This part works fine, but what a would like to do is be able to push a button and have the screen show the sensor data as it was when the button was pressed (not constantly updating) whist showing the live data. How would I go about doing this?

Here is my code so far:

#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <splash.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <gfxfont.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

const int sensorPin = A0;
int sensorValue = 0;
float Vout = 0;
float P = 0;
const int buttonPin = A2;     // the number of the pushbutton pin
// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }
  // Clear the buffer
  display.clearDisplay();
  display.display();
  // declare the ledPin as an OUTPUT:
  Serial.begin(115200);

  // initialize the LED pin as an output:  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  // read the input on analog pin 1:
  int sensorValue = analogRead(A0) - 512;
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.00 / 1024.00);
  // print out the value you read:
  Serial.println(voltage * 10);
  delay(2000);


  { display.clearDisplay();
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(0, 00);
    display.println(voltage * 10);
    display.display();
    delay(500);
  }
}

Use a state machine to determine if the button has been pressed, and check the state before updating the display. Second press resets the “hold” state.

Thanks for the link but the sensor reading is still live, I want to hold it still.

but what a would like to do is be able to push a button and have the screen show the sensor data as it was when the button was pressed (not constantly updating) whist showing the live data.

You can display the data at a point in time OR live data. You can't do both.

Which one do you want to do?

What I want it to do is show the live data at the top of the screen and when a button is pressed, continue to show the live data at the top of the screen but also show the data as it was when i pressed the button on the bottom of the screen. Like the hold function on a voltmeter or thermometer, or perhamps like the split function on a stop watch.

smartguyuk1:
What I want it to do is show the live data at the top of the screen and when a button is pressed, continue to show the live data at the top of the screen but also show the data as it was when i pressed the button on the bottom of the screen. Like the hold function on a voltmeter or thermometer, or perhamps like the split function on a stop watch.

So, what is the problem? Capture the value when the switch becomes pressed, and show the captured value in the appropriate place on the screen.

what command captures a value?

x = y;Saves the value of y in x.