// Tiny RTC (DS1307)

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 RTC;

#include "TM1637.h"
#define CLK 2//pins definitions for TM1637 and can be changed to other ports
#define DIO 3
TM1637 tm(CLK, DIO);

#include "Button2.h";
/////////////////////////////////////////////////////////////////
Button2 button = Button2(4, INPUT_PULLUP, true);

int slotTimes = 0;

unsigned long start_time;
unsigned long timed_event = 5000;
unsigned long current_time; // millis() function returns unsigned long

unsigned long slot_time;
unsigned long slot_timed_event = 5000;


void setup(void) {
  Wire.begin();
  RTC.begin();

  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);

  Serial.begin(9600);
  if (! RTC.isrunning()) {
    RTC.adjust(DateTime(__DATE__, __TIME__));
    // RTC.adjust(DateTime(2017, 11, 6, 23, 35, 15));
    Serial.println("O relógio de tempo real foi iniciado e definido para a hora do sistema.");
  }
  else Serial.println("O relógio em tempo real já está funcionando.");

  tm.init();
  tm.set(BRIGHT_TYPICAL);//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
  tm.point(1);

  button.setPressedHandler(pressed);
}

void loop() {

  button.loop();
  current_time = millis(); // update the timer every cycle
  if (current_time - start_time >= timed_event) {
    //Serial.println("Timer expired, resetting"); // the event to trigger
    start_time = current_time;  // reset the timer
    show_time_and_date();  // Datum und Uhrzeit ausgeben
  }

  if (current_time - slot_time >= slot_timed_event && slotTimes > 0) {
    //Serial.println("Timer expired 2, resetting"); // the event to trigger
    slot_time = current_time;  // reset the timer
    slotTimes = 0;
    show_time_and_date();  // Datum und Uhrzeit ausgeben
  }
}

String get_day_of_week(uint8_t dow) {

  String dows = "  ";
  switch (dow) {
    case 0: dows = "Dom"; break;
    case 1: dows = "Seg"; break;
    case 2: dows = "Ter"; break;
    case 3: dows = "Qua"; break;
    case 4: dows = "Qui"; break;
    case 5: dows = "Sex"; break;
    case 6: dows = "Sab"; break;
  }

  return dows;
}


void show_time_and_date() {
  DateTime datetime = RTC.now();

  if (slotTimes == 1) {
    Serial.println("france");
    DateTime now = RTC.now() + TimeSpan(0, 1, 0, 0);
    datetime = (DateTime(now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second()));
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);
  }
  if (slotTimes == 2) {
    Serial.println("HK");
    DateTime now = RTC.now() + TimeSpan(0, 8, 0, 0);
    datetime = (DateTime(now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second()));
    digitalWrite(6, HIGH);
    digitalWrite(5, LOW);
  }
  int timeConvert = (datetime.hour() * 100) + datetime.minute();
  displayNumber(timeConvert);
  if (slotTimes == 0) {
    digitalWrite(6, LOW);
    digitalWrite(5, LOW);
  }

  // Serial.print(get_day_of_week(datetime.dayOfTheWeek()));
  // Serial.print(", ");
  // if (datetime.day() < 10)Serial.print(0);
  // Serial.print(datetime.day(), DEC);
  // Serial.print(".");
  // if (datetime.month() < 10)Serial.print(0);
  // Serial.print(datetime.month(), DEC);
  // Serial.print(".");
  // Serial.println(datetime.year(), DEC);

  // if (datetime.hour() < 10)Serial.print(0);
  // Serial.print(datetime.hour(), DEC);
  // Serial.print(":");
  // if (datetime.minute() < 10)Serial.print(0);
  // Serial.print(datetime.minute(), DEC);
  // Serial.print(":");
  // if (datetime.second() < 10)Serial.print(0);
  // Serial.println(datetime.second(), DEC);
  show_time_and_date_serial(datetime);
}

void show_time_and_date_serial( DateTime datetime){
Serial.print(get_day_of_week(datetime.dayOfTheWeek()));
  Serial.print(", ");
  if (datetime.day() < 10)Serial.print(0);
  Serial.print(datetime.day(), DEC);
  Serial.print(".");
  if (datetime.month() < 10)Serial.print(0);
  Serial.print(datetime.month(), DEC);
  Serial.print(".");
  Serial.print(datetime.year(), DEC);
  Serial.print(" ");
  if (datetime.hour() < 10)Serial.print(0);
  Serial.print(datetime.hour(), DEC);
  Serial.print(":");
  if (datetime.minute() < 10)Serial.print(0);
  Serial.print(datetime.minute(), DEC);
  Serial.print(":");
  if (datetime.second() < 10)Serial.print(0);
  Serial.println(datetime.second(), DEC);
}


void displayNumber(int num) {
  tm.display(3, num % 10);
  tm.display(2, num / 10 % 10);
  tm.display(1, num / 100 % 10);
  tm.display(0, num / 1000 % 10);
}

void pressed(Button2& btn) {
  Serial.print("pressed: ");
  //slotTimes=(slotTimes+1)%(maximum+1);
  slotTimes = (slotTimes + 1) % (3);
  //slotTimes = slotTimes == 3 ? 0 : slotTimes + 1;
  Serial.print("slotitmes: ");

  Serial.println(slotTimes);
  show_time_and_date();  // Datum und Uhrzeit ausgeben
  slot_time = millis();
}
4-Digit Display
GND5VSDASCLSQWRTCDS1307+