If i push button then renew result from sensor

I want if i push the button that it only renew the value on the lcd then
somebody that know how i have to do it

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
int trigPin = 9;    // TRIG pin
int echoPin = 8;    // ECHO pin
int buttonpin = 2;

float duration_us, distance_cm;

void setup()
{
  Serial.begin(9600); //Serial baud rate 
lcd.begin (16,2);
  lcd.init(); // initialize the lcd
  lcd.backlight();

lcd.home ();
lcd.print(distance_cm);
delay(2000);
lcd.clear();
  // configure the trigger pin to output mode
  pinMode(trigPin, OUTPUT);
  // configure the echo pin to input mode
  pinMode(echoPin, INPUT);
  pinMode(buttonpin, INPUT);
;
}

void loop()
{ 
// generate 10-microsecond pulse to TRIG pin
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10000);
  digitalWrite(trigPin, LOW);

  // measure duration of pulse from ECHO pin
  duration_us = pulseIn(echoPin, HIGH);

  // calculate the distance
  distance_cm = 0.017 * duration_us;
delay(5000);
  // print the value to Serial Monitor
 lcd.print(distance_cm);
 delay(5000);
//if (digitalRead(buttonpin)==HIGH){
  lcd.clear();
//}

      }

Check switches for a change in state from not pushed to pushed.

When you detect a push, update the LCD.

Hi,
try this code and button connection.

thanks it works!

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