#include <EEPROM.h>
#include <Wire.h>
#include <TM1637Display.h>
#define CLK 17
#define DIO 16
TM1637Display display(CLK, DIO);
#define DS3231_I2C_ADDRESS 0x68
//TIMMING
unsigned long presentTime;
unsigned long displayTime; //drawing
//IO
#define BTN1 2
#define BTN2 3
bool lastInput1; //last state of #1 input
bool lastInput2; //last state of #2 input
bool presentInput1; //actual state of input #1
bool presentInput2; //actual state of input #2
//SYSTEM STATE
byte systemState;
byte bright;
byte second, minute, hour;
const uint8_t SEG_DONE[] = {
SEG_B | SEG_C | SEG_D | SEG_E | SEG_G, // d
SEG_E | SEG_G | SEG_C | SEG_D, // o
SEG_C | SEG_E | SEG_G, // n
SEG_A | SEG_D | SEG_E | SEG_F | SEG_G // E
};
void setup(){
Wire.begin();
pinMode(BTN1, INPUT_PULLUP);
pinMode(BTN2, INPUT_PULLUP);
bright = EEPROM.read(0);
display.setBrightness(bright);
}
void loop(){
presentInput1 = digitalRead(BTN1);
presentInput2 = digitalRead(BTN2);
switch (systemState) {
////////////////////////////////////////////////////////////////////////////////////////////////////
default:
presentTime = millis();
if (presentTime - displayTime >= 500) {
displayTime = presentTime;
GetRtc();
WriteTime();
}
if (!presentInput1) {
delay(20);
systemState = 1;
}
break;
////////////////////////////////////////////////////////////////////////////////////////////////////
case 1:
if (presentInput1 && presentInput2) {
delay(20);
systemState = 2;
display.clear();
}
break;
////////////////////////////////////////////////////////////////////////////////////////////////////
case 2:
WriteTime();
if (presentInput1 != lastInput1) {
delay(20);
if (presentInput1) {
systemState = 3;
display.clear();
}
}
if (presentInput2 != lastInput2) {
delay(20);
if (presentInput2) {
hour++;
if (hour > 23) {
hour = 0;
}
}
}
break;
////////////////////////////////////////////////////////////////////////////////////////////////////
case 3:
WriteTime();
if (presentInput1 != lastInput1) {
delay(20);
if (presentInput1) {
systemState = 4;
display.clear();
display.showNumberDec(bright, false, 1, 3);
}
}
if (presentInput2 != lastInput2) {
delay(20);
if (presentInput2) {
minute++;
if (minute > 59) {
minute = 0;
}
}
}
break;
////////////////////////////////////////////////////////////////////////////////////////////////////
case 4:
if (presentInput1 != lastInput1) {
delay(20);
if (presentInput1) {
systemState = 5;
display.clear();
display.setSegments(SEG_DONE);
}
}
if (presentInput2 != lastInput2) {
delay(20);
if (presentInput2) {
bright++;
if (bright > 7) {
bright = 0;
}
display.setBrightness(bright);
display.showNumberDec(bright, false, 1, 3);
}
}
break;
////////////////////////////////////////////////////////////////////////////////////////////////////
case 5:
//menu 4
if (presentInput1 != lastInput1) {
delay(20);
if (presentInput1) {
SetRtc(0, minute, hour, 1, 1, 1, 2024); //set time and zero second
EEPROM.write(0, bright); //store actual light intensity to addr 0
systemState = 0;
}
}
break;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
lastInput1 = presentInput1; //save current state to last state
lastInput2 = presentInput2; //save current state to last state
}
void GetRtc() {
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); //write "0"
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7); //request - 7 bytes from RTC
second = bcdToDec(Wire.read() & 0x7f);
minute = bcdToDec(Wire.read());
hour = bcdToDec(Wire.read() & 0x3f);
}
void WriteTime(){
//write time to matrix display
if (systemState == 2) {
display.showNumberDec(hour, true, 2, 2);
}
if (systemState == 3) {
display.showNumberDec(minute, true, 2, 2);
}
if (systemState == 0){
if (second%2 == 1){
display.showNumberDecEx((hour * 100) + minute, 0b11100000, true);}
else{
display.showNumberDec((hour * 100) + minute, true);}
}
}
byte decToBcd(byte val){
return( (val/10*16) + (val%10) );
}
byte bcdToDec(byte val){
return( (val/16*10) + (val%16) );
}
void SetRtc(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year) {
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); //set 0 to first register
Wire.write(decToBcd(second)); //set second
Wire.write(decToBcd(minute)); //set minutes
Wire.write(decToBcd(hour)); //set hours
Wire.endTransmission();
}