#include <RTClib.h>
#include <Wire.h>
#include <FastLED.h>
#include <avr/pgmspace.h>
#include <OneButton.h>
#define patten A0
OneButton btn = OneButton(
patten, // Input pin for the button
true, // Button is active LOW
true // Enable internal pull-up resistor
);
RTC_DS1307 rtc;
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB // Define color order for your strip
#define BRIGHTNESS 90
#define tens_PIN 5 // Data pin for led comunication
#define pixelcount 15
#define digitcount 8
#define columns 29
#define rows 5
#define NUM_LEDS columns*rows//
int m = 0;
int startunits = 15;
int dotpixels [] = {1, 1, 1, 1, 0, 0, 1, 1, 1, 1};
const int digits [10] [15] =
{ {1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1}, //each line draws out a different number
{0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1},
{1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1},
{1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1},
{1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1},
{1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1},
{1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1},
{1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1}
};
const int digitpixels [8][15] =
{ {0, 1, 2, 29, 30, 31, 58, 59, 60, 87, 88, 89, 116, 117, 118},
{4, 5, 6, 33, 34, 35, 62, 63, 64, 91, 92, 93, 120, 121, 122},
{8, 9, 37, 38, 66, 67, 95, 96, 124, 125},
{11, 12, 13, 40, 41, 42, 69, 70, 71, 98, 99, 100, 127, 128, 129},
{15, 16, 17, 44, 45, 46, 73, 74, 75, 102, 103, 104, 131, 132, 133},
{19, 20, 48, 49, 77, 78, 106, 107, 135, 136},
{22, 23, 24, 51, 52, 53, 80, 81, 82, 109, 110, 111, 138, 139, 140},
{26, 27, 28, 55, 56, 57, 84, 85, 86, 113, 114, 115, 142, 143, 144},
};
const int digitpixelsrows [5][24] PROGMEM = {
{0, 1, 2, 4, 5, 6, 8, 9, 9, 11, 12, 13, 15, 16, 17, 19, 20, 20, 22, 23, 24, 26, 27, 28},
{29, 30, 31, 33, 34, 35, 35, 37, 38, 40, 41, 42, 44, 45, 46, 48, 49, 49, 51, 52, 53, 55, 56, 57},
{58, 59, 60, 62, 63, 64, 66, 67, 67, 69, 70, 71, 73, 74, 75, 77, 78, 78, 80, 81, 82, 84, 85, 86},
{87, 88, 89, 91, 92, 93, 95, 96, 96, 98, 99, 100, 102, 103, 104, 106, 107, 107, 109, 110, 111, 113, 114, 115},
{116, 117, 118, 120, 121, 122, 124, 125, 125, 127, 128, 129, 131, 132, 133, 135, 136, 136, 138, 139, 140, 142, 143, 144}
};
int gappixels [] = {3, 7, 10, 14, 18, 21, 25,
32, 36, 39, 43, 47, 50, 54,
61, 65, 68, 72, 76, 79, 83,
90, 94, 97, 101, 105, 108, 112,
119, 123, 126, 130, 134, 137, 141
};
CRGB tensled[NUM_LEDS]; // Define tensLEDs strip
CRGB unitsled[NUM_LEDS]; //Define unitsLEDs strip
int currenttime [8] = {0, 0, 0, 0, 0, 0, 0, 0};
int p = 0;
int a = 0;
int aold = 0;
int b = 0;
int c = 0;
uint8_t ghue1 = 0;
int state = 0;
int o = 0;
bool DST = false; //DST state
bool changecolour = true;
bool firsttime = true;
boolean randompatten = false ;
void setup() {
randomSeed(analogRead(1));
btn.attachClick(changepatten);
btn.attachDoubleClick(different);
Wire.begin();
rtc.begin();
FastLED.addLeds<WS2812B, tens_PIN, COLOR_ORDER>(tensled, NUM_LEDS);
// FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness( BRIGHTNESS );
FastLED.clear();
//Serial.begin(115200);
// 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(2015, 2, 4, 21, 3, 0))
// rtc.adjust(DateTime(2021,8,12,15,17,0));
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
for (int i = 0; i < NUM_LEDS; i++) {
tensled[i] = CRGB::Red;
FastLED.show();
}
for (int i = 0; i < 35; i++) {
tensled[gappixels[i]] = CRGB::Black;
FastLED.show();
}
}
void loop()
{ calctime();
btn.tick();
EVERY_N_SECONDS(5) {
ghue1 = 10 + ghue1;
}
EVERY_N_SECONDS(25) {
FastLED.clear();
if (randompatten == true) {
state = random(0, 9);
}
}
switch (state) {
case 0:
drawtime();
break;
case 1:
firetime();
break;
case 2:
invfiretime();
break;
case 3:
invdrawtime();
break;
case 4:
candystripe();
break;
case 5:
eachdigitdifferntcolour();
break;
case 6:
onelineatatime();
break;
case 7:
onedigitatatime();
break;
case 8:
rainbowtime();
}
while (a % 60 == 0) {
firsttime = true;
lineacross();
}
}
void lineacross() {
EVERY_N_MILLISECONDS (5) {
o++;
if (o > columns) {
o = 0;
}
}
for (int j = 0; j < rows; j++) {
tensled[XY(o, j)] = CHSV(o * 5, 255, 255);
tensled[XY(o - 1, j)] = CRGB::Black;
}
FastLED.show();
calctime();
}
void calctime() {
btn.tick();
DateTime now = rtc.now();
a = now.second(), DEC;
if (aold != a) {
aold = a;
b = now.minute(), DEC;
c = now.hour(), DEC;
currenttime[0] = (c / 10) % 10;
currenttime[1] = c % 10;
currenttime[2] = a % 2;
currenttime[3] = (b / 10) % 10;
currenttime[4] = b % 10;
currenttime[5] = (a + 1) % 2;
currenttime[6] = (a / 10) % 10;
currenttime[7] = a % 10;
}
}
void drawtime() {
for (int i = 0; i < 8; i++) {
if (i == 2 || i == 5) {
if (currenttime[i] == 1) {
for (int j = 0; j < 10; j++) {
if (j == 4) {
j = 6;
}
tensled[digitpixels[i][j]] = CHSV(ghue(10), 255, 255);
}
}
else {
for (int j = 0; j < 10; j++) {
tensled[digitpixels[i][j]] = CRGB::Black;
}
}
}
else {
for (int j = 0; j < 15; j++) {
if (digits[currenttime[i]][j] == 1) {
tensled[digitpixels[i][j]] = CHSV(ghue(10), 255, 255);
}
else {
tensled[digitpixels[i][j]] = CRGB::Black;
}
}
}
}
FastLED.show();
}
void firetime() {
#define scalenoise 80
int d = millis();
for (int i = 0; i < 8; i++) {
if (i == 2 || i == 5) {
for (byte j = 0; j < 10; j++) {
if (dotpixels [j] == 1) {
tensled[digitpixels[i][j]] = ColorFromPalette (HeatColors_p, qsub8 (inoise8 (j % 5 * scalenoise, j / 2 * scalenoise + d, d / 2),
abs8(j / 2 - (5 - 1)) * 255 / (5 + 4)), BRIGHTNESS);
}
else {
tensled[digitpixels[i][j]] = CHSV(0, 0, 0);
}
}
}
else {
for (byte j = 0; j < pixelcount; j++) {
if (digits[currenttime[i]] [j] == 1) {
tensled[digitpixels[i][j]] = ColorFromPalette (HeatColors_p, qsub8 (inoise8 (j % 5 * scalenoise, j / 3 * scalenoise + d, d / 3),
abs8(j / 3 - (5 - 1)) * 255 / (5 + 4)), BRIGHTNESS);
}
else {
tensled[digitpixels[i][j]] = CHSV(0, 0, 0);
}
}
}
FastLED.show();
}
}
void invfiretime() {
#define scalenoise 80
int d = millis();
for (int i = 0; i < 35; i++) {
tensled[gappixels[i]] = ColorFromPalette (HeatColors_p, qsub8 (inoise8 (i % 5 * scalenoise, i / 2 * scalenoise + d, d / 2),
abs8(i / 2 - (5 - 1)) * 255 / (5 + 4)), BRIGHTNESS);
}
for (int i = 0; i < 8; i++) {
if (i == 2 || i == 5) {
for (byte j = 0; j < 10; j++) {
if (dotpixels [j] == 0) {
tensled[digitpixels[i][j]] = ColorFromPalette (HeatColors_p, qsub8 (inoise8 (j % 5 * scalenoise, j / 2 * scalenoise + d, d / 2),
abs8(j / 2 - (5 - 1)) * 255 / (5 + 4)), BRIGHTNESS);
}
else {
tensled[digitpixels[i][j]] = CHSV(0, 0, 0);
}
}
}
else {
for (byte j = 0; j < pixelcount; j++) {
if (digits[currenttime[i]] [j] == 0) {
tensled[digitpixels[i][j]] = ColorFromPalette (HeatColors_p, qsub8 (inoise8 (j % 5 * scalenoise, j / 3 * scalenoise + d, d / 3),
abs8(j / 3 - (5 - 1)) * 255 / (5 + 4)), BRIGHTNESS);
}
else {
tensled[digitpixels[i][j]] = CHSV(0, 0, 0);
}
}
}
FastLED.show();
}
}
void invdrawtime() {
for (int i = 0; i < 35; i++) {
tensled[gappixels[i]] = CHSV(ghue(10), 255, 255);
}
for (int i = 0; i < 8; i++) {
if (i == 2 || i == 5) {
if (currenttime[i] == 1) {
for (int j = 4; j < 6; j++) {
tensled[digitpixels[i][j]] = CHSV(ghue(10), 255, 255);
}
}
else {
for (int j = 0; j < 10; j++) {
tensled[digitpixels[i][j]] = CRGB::Black;
}
}
}
else {
for (int j = 0; j < 15; j++) {
if (digits[currenttime[i]][j] == 0) {
tensled[digitpixels[i][j]] = CHSV(ghue(10), 255, 255);
}
else {
tensled[digitpixels[i][j]] = CRGB::Black;
}
}
}
}
FastLED.show();
}
void onelineatatime() {
EVERY_N_MILLISECONDS (1500) {
calctime();
FastLED.clear();
for (int w = 0; w < 5; w++) { //w i the number of rows!
for (int u = 0; u < 24; u++) {
p = u / 3;
if (p == 2 || p == 5) {
for (int i = 2; i < 6; i += 3) {
for (int u = 0; u < 10; u++) {
if (dotpixels[u] == 1) {
tensled[digitpixels[i][u]] = CHSV(u / 2 * 50 + ghue1, 255, 255);
}
}
}
}
else {
if (digits[currenttime[p]][w * 3 + u % 3] == 1) {
tensled[pgm_read_dword(&(digitpixelsrows[w][u]))] = CHSV(w * 50 + ghue1, 255, 255);
}
else {
tensled[pgm_read_dword(&(digitpixelsrows[w][u]))] = CRGB::Black;
}
}
FastLED.show();
}
}
}
}
void eachdigitdifferntcolour() {
for (int i = 0; i < 8; i++) {
if (i == 2 || i == 5) {
if (currenttime[i] == 1) {
for (int j = 0; j < 10; j++) {
if (j == 4) {
j = 6;
}
tensled[digitpixels[i][j]] = CHSV((i * 30 + 30 * ghue1), 255, 255);
}
}
else {
for (int j = 0; j < 10; j++) {
tensled[digitpixels[i][j]] = CRGB::Black;
}
}
}
else {
for (int j = 0; j < 15; j++) {
if (digits[currenttime[i]][j] == 1) {
tensled[digitpixels[i][j]] = CHSV((i * 30 + 30 * ghue1), 255, 255);
}
else {
tensled[digitpixels[i][j]] = CRGB::Black;
}
}
}
}
FastLED.show();
}
void candystripe() {
for (int i = 0; i < 8; i++) {
if (i == 2 || i == 5) {
for (int j = 0; j < 10; j++) {
if (j == 4) {
j = 6;
}
if ((digitpixels[i][j] + currenttime[7]) % 2 == 0) {
tensled[digitpixels[i][j]] = CRGB::White;
}
else {
tensled[digitpixels[i][j]] = CRGB::Red;
}
}
}
else {
for (int j = 0; j < 15; j++) {
if (digits[currenttime[i]][j] == 1) {
if ((digitpixels[i][j] + currenttime[7]) % 2 == 0) {
tensled[digitpixels[i][j]] = CRGB::White;
}
else {
tensled[digitpixels[i][j]] = CRGB::Red;
}
}
else {
tensled[digitpixels[i][j]] = CRGB::Black;
}
}
}
}
FastLED.show();
}
void onedigitatatime() {
EVERY_N_MILLISECONDS (250) {
m++;
if (m > 7) {
m = 0;
FastLED.clear();
}
}
if (m == 2 || m == 5) {
for (int j = 0; j < 10; j++) {
if (j == 4) {
j = 6;
}
tensled[digitpixels[m][j]] = CHSV(ghue(10), 255, 255);
}
}
else {
for (int j = 0; j < 15; j++) {
if (digits[currenttime[m]][j] == 1) {
tensled[digitpixels[m][j]] = CHSV(ghue(10), 255, 255);
}
else {
tensled[digitpixels[m][j]] = CRGB::Black;
}
}
}
FastLED.show();
}
void rainbowtime() {
fill_rainbow( tensled, NUM_LEDS, ghue1,29);
for (int i = 0; i < 8; i++) {
if (i == 2 || i == 5) {
if (currenttime[i] == 1) {
for (int j = 0; j < 10; j++) {
if (j == 4) {
j = 6;
}
tensled[digitpixels[i][j]] = CRGB::White;
}
}
}
else {
for (int j = 0; j < 15; j++) {
if (digits[currenttime[i]][j] == 1) {
tensled[digitpixels[i][j]] = CRGB::White;
}
}
}
}
FastLED.show();
}
uint8_t ghue(int q) {
return ghue1 + q * 10;
}
uint16_t XY (uint8_t x, uint8_t y) {
return (y * columns + x);
}
uint16_t XYdot (uint8_t x, uint8_t y) {
return (y * 2 + x);
}
void changepatten() {
FastLED.clear();
state++;
if (state > 8) {
state = 0;
}
}
void different() {
FastLED.clear();
randompatten = !randompatten;
}