#include <DS3231.h>
#include <ESP8266WiFi.h>
#include <TM1637Display.h>
#include <Wire.h>
#define CLK 16
#define DIO 12
// The amount of time (in milliseconds) between tests
#define TEST_DELAY 1000
const byte HourBtn = 13;
const byte MinBtn = 2;
volatile byte state = 0;
byte displaySec = 0b01000000;
TM1637Display display(CLK, DIO);
DS3231 myRTC;
uint8_t data[] = {0xff, 0xff, 0xff, 0xff};
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
display.setBrightness(0x0f);
display.setSegments(data);
Wire.begin();
pinMode(HourBtn, INPUT_PULLUP);
pinMode(MinBtn, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(HourBtn), pushHour, RISING);
attachInterrupt(digitalPinToInterrupt(MinBtn), pushMin, RISING);
delay(500);
}
void loop() {
bool h12, pm;
byte hour = myRTC.getHour(h12, pm);
byte min = myRTC.getMinute();
switch (state) {
case 1:
hour = (hour + 1) % 24;
myRTC.setHour(hour);
state = 0;
break;
case 2:
min = (min + 1) % 60;
myRTC.setMinute(min);
state = 0;
break;
}
Serial.print(hour, DEC);
Serial.print(':');
Serial.println(min, DEC);
Serial.print("DisplaySec ");
Serial.print(displaySec);
Serial.print(", ");
Serial.println(displaySec & 0b01000000);
int time = hour * 100 + min;
display.showNumberDecEx(time, displaySec & 0b01000000, true);
delay(TEST_DELAY);
displaySec = ~displaySec;
}
IRAM_ATTR void pushHour() { state = 1; }
IRAM_ATTR void pushMin() { state = 2; }