#include <TM1637.h>
#include <TimerOne.h>
#include <ezButton.h>
#include <ArduinoTrace.h>
unsigned char hour = 7;
unsigned char minute = 7;
const int CLK = 2;
const int DIO = 3;
TM1637 tm(CLK, DIO);
ezButton timeSetBtn(5); // create ezButton object that attach to pin 7;
ezButton hourBtn(6); // create ezButton object that attach to pin 7;
ezButton minuteBtn(7); // create ezButton object that attach to pin 7;
void setup()
{
Serial.begin(115200);
timeSetBtn.setDebounceTime(50); // set debounce time to 50 milliseconds
hourBtn.setDebounceTime(50);
minuteBtn.setDebounceTime(50);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
tm.init();
tm.set(BRIGHT_TYPICAL);
}
unsigned long pm1;
void loop()
{
timeSetBtn.loop();
hourBtn.loop();
minuteBtn.loop();
tm.display(0, (hour/10) % 10);
tm.display(1, hour % 10);
tm.display(2, (minute/10) % 10);
tm.display(3, minute % 10);
increment();
incrementminute();
DUMP(hour);
delay(10);
}
void increment()
{
enum {s0,s1,s2,s3};
static int state=s0;
switch(state)
{
case s0:
if(timeSetBtn.getState()==LOW)
{
state=s1;
}
break;
case s1:
if(hourBtn.getState()==LOW && timeSetBtn.getState()==LOW)
{
pm1=millis();
state=s2;
if(hour<23)
{
hour++;
}
else
{
hour=0;
}
}
else
{
state=s0;
}
break;
case s2:
if(hourBtn.getState()==LOW && timeSetBtn.getState()==LOW)
{
if((millis()-pm1)>800)
{
pm1=millis();
state=s3;
}
}
else
{
state=s0;
}
break;
case s3:
if(hourBtn.getState()==LOW && timeSetBtn.getState()==LOW)
{
if((millis()-pm1)>250)
{
pm1=millis();
if(hour<23)
{
hour++;
}
else
{
hour=0;
}
}
}
else
{
state=s0;
}
break;
}
}
void incrementminute()
{
enum {s0,s1,s2,s3};
static int state=s0;
switch(state)
{
case s0:
if(timeSetBtn.getState()==LOW)
{
state=s1;
}
break;
case s1:
if(minuteBtn.getState()==LOW && timeSetBtn.getState()==LOW)
{
pm1=millis();
state=s2;
if(minute<59)
{
minute++;
}
else
{
minute=0;
}
}
else
{
state=s0;
}
break;
case s2:
if(minuteBtn.getState()==LOW && timeSetBtn.getState()==LOW)
{
if((millis()-pm1)>800)
{
pm1=millis();
state=s3;
}
}
else
{
state=s0;
}
break;
case s3:
if(minuteBtn.getState()==LOW && timeSetBtn.getState()==LOW)
{
if((millis()-pm1)>250)
{
pm1=millis();
if(minute<59)
{
minute++;
}
else
{
minute=0;
hour++;
}
}
}
else
{
state=s0;
}
break;
}
}