Update: Hi please review my code and give some suggestions. i tried to avoid rapidly calling of fastled show command and delay function. Now i am interested to add alarm function and 12 hour format. i am using rtc lib. Any good tutorial ??
/* Digital Clock with WS2813 Strip Version 2*/
#include <FastLED.h>
#include <Wire.h>
#include "RTClib.h"
#define LED_PIN 7
#define NUM_LEDS 31
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define BRIGHTNESS 100 // MAX Brightness 0---255
int firstdigithour; // For storing first digit of hour
int seconddigithour; // For storing Second digit of hour
int firstdigitminute; // For storing first digit of minute
int seconddigitminute; // For storing Second digit of minute
int bitsecond; // For storing last bit of seconds
int storeseconds; // For storing current seconds
int previousfirstdigithour;
int previousseconddigithour;
int previousfirstdigitminute;
int previousseconddigitminute;
int previousbitsecond;
CRGB leds[NUM_LEDS];
CRGB color = CRGB::White;
RTC_DS3231 rtc;
//char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup ()
{
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness(BRIGHTNESS);
FastLED.delay(300) ; // For initializing LED strip
for(int x = 0; x < NUM_LEDS; x++)
{
leds[x] = color; // For checking all SMD chips are working fine
}
FastLED.show();
delay(1000);
for(int x = 0; x < NUM_LEDS; x++)
{
leds[x] = CRGB::Black;;
}
FastLED.show();
delay(500);
Serial.begin(9600);
while (!Serial) { /* Wait until serial is ready */ }
if (!rtc.begin())
{
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower())
{
Serial.println("RTC lost power, lets set the time!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // For synchronize time with computer
// 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));
}
gettime();
firsttime();
blinkseconds();
leds[30] = CRGB::Red;
}
void gettime()
{
DateTime now = rtc.now();
firstdigithour = now.hour() / 10;
seconddigithour = now.hour() % 10;
firstdigitminute = now.minute() / 10;
seconddigitminute = now.minute() % 10;
storeseconds =now.second();
bitsecond = bitRead(storeseconds, 0);
}
void firsttime()
{
displayTime(0, seconddigitminute);
displayTime(7, firstdigitminute);
displayTime(16, seconddigithour);
displayTime(23, firstdigithour);
previousfirstdigithour = firstdigithour;
previousseconddigithour = seconddigithour;
previousfirstdigitminute = firstdigitminute;
previousseconddigitminute = seconddigitminute;
previousbitsecond = bitsecond;
FastLED.show();
/* Serial.println();
Serial.print(firstdigithour);
Serial.print(seconddigithour);
Serial.print(":");
Serial.print(firstdigitminute);
Serial.print(seconddigitminute); */
}
void blinkseconds()
{
if(bitsecond==1)
{
leds[14] = CRGB::Red;
leds[15] = CRGB::Red;
}
else
{
leds[14] = CRGB::Black;
leds[15] = CRGB::Black;
}
}
void displayTime(int startindex, int number) {
byte numbers[] = {
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111, // 9
};
for (int i = 0; i < 7; i++) {
leds[i + startindex] = ((numbers[number] & 1 << i) == 1 << i) ? color : CRGB::Black;
}
}
void loop()
{
gettime();
if(bitsecond!=previousbitsecond)
{
previousbitsecond = bitsecond;
Serial.println("seconds command executed");
blinkseconds();
FastLED.show();
}
if(seconddigitminute!=previousseconddigitminute)
{
previousseconddigitminute = seconddigitminute;
Serial.println("second digit minute command executed");
displayTime(0, seconddigitminute);
FastLED.show();
}
if(firstdigitminute!=previousfirstdigitminute)
{
previousfirstdigitminute = firstdigitminute;
Serial.println("first digit minute command executed");
displayTime(7, firstdigitminute);
FastLED.show();
}
if(seconddigithour!=previousseconddigithour)
{
previousseconddigithour = seconddigithour;
Serial.println("second digit hour command executed");
displayTime(16, seconddigithour);
FastLED.show();
}
if(firstdigithour!=previousfirstdigithour)
{
previousfirstdigithour = firstdigithour;
Serial.println("first digit hour command executed");
displayTime(23, firstdigithour);
FastLED.show();
}
}
UPDATE: i purchased DS3231 and have ws2813 let strips 60 leds per meter. The tutorial for DS3231 was not found on net for beginners. Does anyone have reference from where i start? I am no longer using RGB diodes.
Hi everyone,
I have arduino nano and want to build simple digital clock in 7 segment display (hours, minutes, blinking seconds )with customized colours.
My first question is what components are required ? which is better option RGB leds with common Anode or Cathode? DS3231 module i am using.
Thanks in advance