/* www.arduinopoint.com */
/* Digital 24 hour time format clock by Arduino, TM1637 4 digit 7 segment display and DS3231 RTC.*/
// Add libraries: RTClib and TM1637
#include "RTClib.h"
#include <TM1637Display.h>
#include <FastLED.h>
#define NUM_LEDS 60
#define DATA_PIN 5
// Define the connections pins for TM1637 4 digit 7 segment display
#define CLK 7
#define DIO 8
unsigned char hourS = 0;
unsigned char minuteS = 0;
unsigned char secondS = 0;
// Create rtc and display object
RTC_DS3231 rtc;
TM1637Display display = TM1637Display(CLK, DIO);
CRGB leds[NUM_LEDS];
void setup() {
// Begin serial communication at a baud rate of 9600
Serial.begin(9600);
// Wait for console opening
// delay(3000);
// Initialize the FastLED library
FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
// Check if RTC is connected correctly
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
// Check if the RTC lost power and if so, set the time:
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
// The following line sets the RTC to the date & time this sketch was compiled:
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
// Set the display brightness (0-7):
display.setBrightness(5);
// Clear the display:
display.clear();
clearLEDS();
}
void loop() {
// Set the LED to blue
unsigned long puk = millis()/1000;
leds[secondS] = CHSV(map(secondS,1,60,0,255), 255, 255);
if ( secondS == 59 ){
clearLEDS();
}
// Get current date and time
DateTime now = rtc.now();
hourS = now.hour();
minuteS = now.minute();
secondS = now.second();
// Adjust hour for 12-hour format if needed
if (hourS > 12) {
hourS = hourS - 12;
}
// Create time format to display:
int displaytime = (hourS * 100) + minuteS;
// Print displaytime and seconds to the Serial Monitor
Serial.print("Time: ");
Serial.print(displaytime);
Serial.print(" Seconds: ");
Serial.println(secondS);
// Display the current time in 24 hour format with leading zeros enabled and a center colon:
display.showNumberDecEx(displaytime, 0b11100000, true);
// Remove the following lines of code if you want a static instead of a blinking center colon:
// delay(1000);
display.showNumberDec(displaytime, true); // Prints displaytime without center colon.
FastLED.show();
// delay(1000);
}
void clearLEDS() {
for(int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Black;
}
FastLED.show();
}