// Using LCD1602 to Arduino Uno connection example and sprintf
// function to clean up code, and test the function's usability
// potential in some of my other projects.
#include <LiquidCrystal.h>
int button = 3;
bool pressed = false;
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
char time[7];
int minutes = 0;
int seconds = 0;
void reset()
{
pressed = false;
minutes = 0;
seconds = 0;
}
void setup()
{
lcd.begin(16, 2);
// you can now interact with the LCD, e.g.:
//lcd.print("Hello World!");
pinMode(button, INPUT_PULLUP);
//sprintf(time, "%02dm%02ds", minutes, seconds); // requires multiple calls in order to display properly
}
void loop()
{
lcd.clear();
lcd.print(" Waiting...");
while (!pressed)
{
delay(1000);
seconds++;
if (seconds == 60)
{
seconds = 0;
minutes++;
}
if (digitalRead(button) == LOW)
{
delay(100);
if (digitalRead(button) == HIGH) {pressed = true;}
}
sprintf(time, "%02dm%02ds", minutes, seconds);
lcd.setCursor(0, 1);
lcd.print(" Press button");
}
lcd.clear();
lcd.print("Button pressed:");
lcd.setCursor(0, 1);
lcd.print(time);
lcd.print(" ago");
delay(1500);
reset();
}