#include "Button.h"
#include "TM1637Display.h"
#include "virtuabotixRTC.h"
#define RTC_RST 12
#define RTC_DAT 11
#define RTC_CLK 10
// Module connection pins (Digital Pins)
#define SEG_DIO 9
#define SEG_CLK 8
#define HORAS_BTN A0
#define MIN_BTN A1
// The amount of time (in milliseconds) between tests
#define TEST_DELAY 2000
TM1637Display display(SEG_CLK, SEG_DIO);
// Creation of the Real Time Clock Object
virtuabotixRTC myRTC(RTC_CLK, RTC_DAT, RTC_RST);
Button hourButton(HORAS_BTN);
Button minuteButton(MIN_BTN);
long lastStateChange = 0;
// macro version
#define DEC2BCD(dec) (((dec / 10) << 4) + (dec % 10))
#define BCD2DEC(bcd) ((bcd / 16 * 10) + (bcd % 16))
void setup()
{
Serial.begin(9600);
// Set the current date, and time in the following format:
// seconds, minutes, hours, day of the week, day of the month, month, year
myRTC.setDS1302Time(00, 59, 23, 6, 10, 1, 2014);
hourButton.begin();
hourButton.set_repeat(500, 200);
minuteButton.begin();
minuteButton.set_repeat(500, 200);
display.setBrightness(0x04);
}
void loop()
{
int k;
uint8_t data[] = { 0xff, 0xff, 0xff, 0xff };
uint8_t hour_before;
uint8_t minute_before;
display.setBrightness(0x04);
// All segments on
display.setSegments(data);
delay(TEST_DELAY);
// Selectively set different digits
data[0] = display.encodeDigit(10);
data[1] = display.encodeDigit(7);
data[2] = display.encodeDigit(6);
data[3] = display.encodeDigit(5);
display.setSegments(data);
delay(TEST_DELAY);
// This allows for the update of variables for time or accessing the individual elements. //|
myRTC.updateTime(); //|
//|
// Start printing elements as individuals
if((hour_before != myRTC.hours) || (minute_before != myRTC.minutes)){
hour_before = myRTC.hours;
minute_before = myRTC.minutes;
Serial.print(myRTC.hours); //|
Serial.print(":"); //|
Serial.println(myRTC.minutes); //|
//|
}
if (hourButton.pressed()) {
Serial.println("press hour");
lastStateChange = millis();
}
if (minuteButton.pressed()) {
Serial.println("press minute");
lastStateChange = millis();
}
Serial.println(DEC2BCD(18));
Serial.println(BCD2DEC(myRTC.hours));
}