/* name : clock_4d_led_03.ino
purpose : Clock with 4 digit LED display (TM1637), using Arduino and RTC DS1307
author : LZ2WSG Veselin Georgiev
site : http://kn34pc.com/construct/lz2wsg_arduino_clock_4d_led.html
date : 29-06-2016
revision : v1.03
used info : http://www.manelsoft.com/projects/arduino_ds1307_clock.aspx
: http://playground.arduino.cc/Main/TM1637
: http://www.ad7c.com/projects/ad9850-dds-vfo/
---------------------------------------------------------------------------------------------------------*/
#include <Wire.h>
#include "RTClib.h" // https://github.com/adafruit/RTClib
#include "TM1637.h" // https://github.com/avishorp/TM1637
#define CLK 9 // TM1637 pin CLK
#define DIO 8 // TM1637 pin DIO
#define SW_HOUR A0 // Set HOURS button
#define SW_MIN A1 // Set MINUTES button
TM1637 tm1637(CLK, DIO);
RTC_DS1307 RTC; // RTC DS1307 use SDA(A4) and SCL(A5) pins
boolean dp = true; // digital points
uint8_t m; // minutes
uint8_t h; // hours
uint8_t tmp;
unsigned long vr = 0; // time variable
//---------------------------------------------------------------------------------------------------------
void setup()
{
pinMode(SW_HOUR, INPUT_PULLUP);
pinMode(SW_MIN, INPUT_PULLUP);
Wire.begin(); // Initiate the Wire library and join the I2C bus
RTC.begin(); // Initiate the RTC library
// RTC.adjust(DateTime(__DATE__, __TIME__));
tm1637.init(); // Initiate the TM1637 library
tm1637.set(BRIGHTEST); // BRIGHT_TYPICAL = 2, BRIGHT_DARKEST = 0, BRIGHTEST = 7
}
//---------------------------------------------------------------------------------------------------------
void loop()
{
if (millis() - vr > 500) { // time refresh (and digital points blink): every 0,5 sec
show_time();
tm1637.point(dp);
dp = !dp;
vr = millis();
}
if (read_button(SW_HOUR) == 1) {
DateTime now = RTC.now();
tmp = now.hour();
if (tmp == 23)
tmp = 0;
else
tmp++;
RTC.adjust(DateTime(now.year(), now.year(), now.day(), tmp, now.minute(), now.second()));
show_time();
delay(250);
}
if (read_button(SW_MIN) == 1) {
DateTime now = RTC.now();
tmp = now.minute();
if (tmp == 59)
tmp = 0;
else
tmp++;
RTC.adjust(DateTime(now.year(), now.year(), now.day(), now.hour(), tmp, 0));
show_time();
delay(250);
}
}
//---------------------------------------------------------------------------------------------------------
void show_time() // Time Format -> HH : MM
{
DateTime now = RTC.now();
h = now.hour();
m = now.minute();
if (h < 10) // leading zero
tm1637.display(0x00, 0x7f);
else
tm1637.display(0, h / 10);
tm1637.display(1, h % 10);
tm1637.display(2, m / 10);
tm1637.display(3, m % 10);
}
//---------------------------------------------------------------------------------------------------------
int read_button (byte pin_wbutton) // push button state, return: 0(very fast), 1(normal or hold)
{
unsigned long vr_start = 0;
unsigned long vr_button = 0;
byte wposition = 0;
vr_start = millis();
while (digitalRead(pin_wbutton) == LOW) {
vr_button = millis() - vr_start;
if (vr_button > 100)
break;
}
if (vr_button < 50) // 'nothing' for debounce
wposition = 0;
else wposition = 1; // push or hold button
return wposition;
}
//---------------------------------------------------------------------------------------------------------