/**
Arduino Digital Clock (12-hour format)
Copyright (C) 2020, Uri Shaked.
Released under the MIT License.
*/
#include <SevSeg.h>
#include "Button.h"
#include "Clock.h"
#include "config.h"
const int COLON_PIN = 13;
Button hourButton(A0);
Button minuteButton(A1);
Clock clock;
SevSeg sevseg;
enum DisplayState {
DisplayClock,
};
DisplayState displayState = DisplayClock;
long lastStateChange = 0;
void changeDisplayState(DisplayState newValue) {
displayState = newValue;
lastStateChange = millis();
}
long millisSinceStateChange() {
return millis() - lastStateChange;
}
void setColon(bool value) {
digitalWrite(COLON_PIN, value ? LOW : HIGH);
}
void displayTime() {
DateTime now = clock.now();
bool blinkState = now.second() % 2 == 0;
int hour = now.hour();
if (hour > 12) {
hour -= 12;
}
if (hour == 0) {
hour = 12;
}
sevseg.setNumber(hour * 100 + now.minute());
setColon(blinkState);
}
void clockState() {
displayTime();
if (hourButton.pressed()) {
clock.incrementHour();
}
if (minuteButton.pressed()) {
clock.incrementMinute();
}
}
void setup() {
Serial.begin(115200);
clock.begin();
hourButton.begin();
hourButton.set_repeat(500, 200);
minuteButton.begin();
minuteButton.set_repeat(500, 200);
pinMode(COLON_PIN, OUTPUT);
byte digits = 4;
byte digitPins[] = {2, 3, 4, 5};
byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12};
bool resistorsOnSegments = false;
bool updateWithDelays = false;
bool leadingZeros = true;
bool disableDecPoint = true;
sevseg.begin(DISPLAY_TYPE, digits, digitPins, segmentPins, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);
sevseg.setBrightness(90);
}
void loop() {
sevseg.refreshDisplay();
switch (displayState) {
case DisplayClock:
clockState();
break;
}
}