#include <LiquidCrystal.h>
LiquidCrystal lcd(7,6,5,4,3,2);
const byte HOURGLASS_TOP[8] = {
0b11111,
0b11111,
0b01110,
0b00100,
0b01010,
0b10001,
0b11111,
0b00000
};
const byte HOURGLASS_MIDDLE[8] = {
0b11111,
0b10001,
0b01110,
0b00100,
0b01110,
0b10001,
0b11111,
0b00000
};
const byte HOURGLASS_BOTTOM[8] = {
0b11111,
0b10001,
0b01010,
0b00100,
0b01110,
0b11111,
0b11111,
0b00000
};
volatile bool enableTimer = true;
volatile bool resetFlag = false;
byte currentImage = 0;
unsigned long currentTime = 0;
unsigned long lastTick = 0;
void setup() {
lcd.begin(16,2); // must come before createChar
lcd.createChar(0, HOURGLASS_TOP);
lcd.createChar(1, HOURGLASS_MIDDLE);
lcd.createChar(2, HOURGLASS_BOTTOM);
pinMode(20, INPUT_PULLUP);
pinMode(21, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(20), startStopTimer, FALLING);
attachInterrupt(digitalPinToInterrupt(21), resetTimer, FALLING);
display();
}
void loop() {
static bool wasEnabled = false;
bool enabled = enableTimer;
if (enabled && !wasEnabled) lastTick = millis(); // restart the 1 s count
wasEnabled = enabled;
if (resetFlag) {
resetFlag = false;
currentTime = 0;
currentImage = 0;
lastTick = millis();
lcd.clear();
display();
}
if (enabled && millis() - lastTick >= 1000) {
lastTick += 1000;
currentTime++;
currentImage = (currentImage + 1) % 3;
display();
}
}
void display() {
lcd.setCursor(0,0);
lcd.print(currentTime);
lcd.setCursor(0,1);
lcd.write(currentImage);
}
void startStopTimer() { enableTimer = !enableTimer; }
void resetTimer() { resetFlag = true; }