#include <Button.h>
Button button1(4);
const int buttonPin = 4;
int oldValue = HIGH; // default/idle value for pin 4 is high.
// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 8, d5 = 7, d6 = 6, d7 = 5;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int Time_Start = 0;
int Time_End = 0;
int Time = 0;
void setup()
{
//Setup LCD-Display
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("------Time------");
Serial.begin(115200);
Serial.println("Press the button.");
// Initialize the pin for reading the button.
pinMode(buttonPin, INPUT_PULLUP);
button1.begin();
}
void loop()
{
Button_Example();
}
void Button_Example()
{
// Read the value of pin 4.
int newValue = digitalRead(buttonPin);
// Check if the value was changed,
// by comparing it with the previous value.
if(newValue != oldValue)
{
if(newValue == LOW)
{
Serial.println("The button is pressed.");
Time_Start = millis()/1000;
}
else
{
Serial.println("The button is released.");
Time_End = millis()/1000;
Time = Time_End - Time_Start;
//lcd.setCursor(0, 1);
//lcd.print(Time_End);
//lcd.setCursor(1, 1);
//lcd.print("-");
//lcd.setCursor(2, 1);
//lcd.print(Time_Start);
//lcd.setCursor(3, 1);
//lcd.print("=");
lcd.setCursor(4, 1);
lcd.print(Time);
}
// Remember the value for the next time.
oldValue = newValue;
}
// Slow down the sketch.
// Also for debouncing the button.
delay(100);
}