#include <FastLED.h>
#include <RTClib.h>
#include <Wire.h>
//RTC Defination
RTC_DS3231 rtc;
char t[32];
char tBaseline[32];
int baseDay;
int currentDay;
int dayDifference = 1; // write the required day difference for change in number through RTC
//RF Module
//define RF-Arduino inputs
#define RF_D0 8
#define RF_D1 9
#define RF_D2 10
#define RF_D3 11
#define interruptPin 2
//define variables for RF data
bool buttonPressedFlag = false;
char userInput = 'x';
String cond;
//NEO-LED Settings & variables
#define LED_TYPE WS2812B
#define LED_PIN 4
#define NUM_LEDS 58 // Number of LEDs (change in diagram.json as well)
#define LEDS_PER_SEG 4 // Number of LEDs per segment
#define COLOR_ORDER GRB
#define BRIGHTNESS 80
#define VOLTS 5
#define MAX_AMPS_mA 500
CRGB leds[NUM_LEDS];
boolean Display1_Segment[7] = {0}; //display 1 (7 segment)
boolean Display2_Segment[7] = {0}; //display 2 (7 segment)
boolean result[14] = {0}; //display 1 & display 2 (14 segment)
boolean LEDs_Output[NUM_LEDS] = {0}; //since each segment have 4 LEDs so 14*4 = 56 LEDs
int index = 0;
int counterLED = 0;
int colorFlag = 0;
int brightnessFlag = 0;
boolean segValues[10][7] =
{
{0, 1, 1, 1, 1, 1, 1}, //0
{0, 1, 0, 0, 0, 0, 1}, //1
{1, 1, 1, 0, 1, 1, 0}, //2
{1, 1, 1, 0, 0, 1, 1}, //3
{1, 1, 0, 1, 0, 0, 1}, //4
{1, 0, 1, 1, 0, 1, 1}, //5
{1, 0, 1, 1, 1, 1, 1}, //6
{0, 1, 1, 0, 0, 0, 1}, //7
{1, 1, 1, 1, 1, 1, 1}, //8
{1, 1, 1, 1, 0, 1, 1} //9
};
int currentDigitDisplay1 = 0;
int currentDigitDisplay2 = 0;
//DEL
//int o = 0;
void setup()
{
Serial.begin(9600);
Wire.begin();
rtc.begin();
//rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));
//rtc.adjust(DateTime(2023, 9, 30, 23, 59, 50));
DateTime now = rtc.now();
sprintf(tBaseline, "%02d:%02d:%02d %02d/%02d/%02d", now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());
Serial.println("Following is complete base time");
Serial.print(F("Date/Time: "));
Serial.println(tBaseline);
baseDay = now.day();
LED_Initialisation();
Serial.println("Showing all the stored digits (0-9) for segment LEDs: ");
//Showing all the stored digits (0-9) for segment LEDs
for (int i = 0; i < 10; i++)
{
for (int k = 0; k < 7; k++)
{
Serial.print(segValues[i][k]);
}
Serial.println(" ");
}
// first time run
//currentDigitDisplay1 = 0;
currentDigitDisplay2 = 1;
//memcpy(Display1_Segment, segValues[currentDigitDisplay1], sizeof(segValues[currentDigitDisplay1]));
memcpy(Display2_Segment, segValues[currentDigitDisplay2], sizeof(segValues[currentDigitDisplay2]));
display ();
//initialise arduino input pins connected to RF module
pinMode(RF_D0, INPUT);
pinMode(RF_D1, INPUT);
pinMode(RF_D2, INPUT);
pinMode(RF_D3, INPUT);
attachInterrupt(digitalPinToInterrupt(interruptPin), interuptingVT, RISING);
}
void loop()
{
checkTime();
//Serial.print("User Input: ");
//Serial.println(userInput);
/*
if (o <= 11)
{
userInput = 'B';
Serial.println("decrementing");
}
if (o > 11 && o<=23)
{
userInput = 'A';
Serial.println("incrementing");
}
if (o > 23 && o<=33)
{
userInput = 'B';
Serial.println("incrementing");
}
o++;
*/
//userInput = 'A';
//checkCondition(userInput);
//delay(500);
if (buttonPressedFlag == true)
{
checkCondition(userInput);
buttonPressedFlag = false;
}
}
// RTC Timer check for next change in number
void checkTime()
{
DateTime now = rtc.now();
sprintf(t, "%02d:%02d:%02d %02d/%02d/%02d", now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());
Serial.print(F("Date/Time: "));
Serial.println("Following is current time");
Serial.println(t);
delay(500);
currentDay = now.day();
Serial.print("Time difference (days): ");
Serial.println(abs(currentDay - baseDay));
if (abs(currentDay - baseDay) >= dayDifference)
{
userInput = 'B';
baseDay = now.day();
checkCondition(userInput);
Serial.println("New base time");
Serial.print(F("Date/Time: "));
Serial.println(t);
}
}
void checkCondition(char recieverValue)
{
switch (recieverValue) {
case 'A':
cond = "Increment";
performIncrement();
recieverValue = 'x';
break;
case 'B':
cond = "Decrement";
performDecrement();
recieverValue = 'x';
break;
case 'C':
cond = "Color";
performColorChange();
recieverValue = 'x';
break;
case 'D':
cond = "Brightness";
performBrightnessChange();
recieverValue = 'x';
break;
default:
cond = "Increment";
recieverValue = 'x';
}
}
//Operations on different button press
//Button A
void performIncrement()
{
currentDigitDisplay2++;
if (currentDigitDisplay2 > 9)
{
currentDigitDisplay2 = 0;
currentDigitDisplay1++;
if (currentDigitDisplay1 > 9)
{
currentDigitDisplay1 = 0;
}
}
memcpy(Display1_Segment, segValues[currentDigitDisplay1], sizeof(segValues[currentDigitDisplay1]));
specialCaseDisplay1();
memcpy(Display2_Segment, segValues[currentDigitDisplay2], sizeof(segValues[currentDigitDisplay2]));
display();
//delay(100);
}
//Button B
void performDecrement()
{
currentDigitDisplay2--;
if (currentDigitDisplay2 < 0)
{
currentDigitDisplay2 = 9;
currentDigitDisplay1--;
if (currentDigitDisplay1 < 0)
{
currentDigitDisplay1 = 9;
}
}
memcpy(Display1_Segment, segValues[currentDigitDisplay1], sizeof(segValues[currentDigitDisplay1]));
specialCaseDisplay1();
memcpy(Display2_Segment, segValues[currentDigitDisplay2], sizeof(segValues[currentDigitDisplay2]));
display();
//delay(100);
}
//Button C
void performColorChange()
{
if (colorFlag < 4)
{
colorFlag++;
}
else
colorFlag = 0;
display();
}
//Button D
void performBrightnessChange()
{
if (brightnessFlag < 5)
{
brightnessFlag++;
}
else
brightnessFlag = 0;
display();
}
void LED_Initialisation()
{
//FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setMaxPowerInVoltsAndMilliamps(VOLTS, MAX_AMPS_mA);
FastLED.setBrightness(BRIGHTNESS);
FastLED.clear();
FastLED.show();
}
void display ()
{
index = 0;
memcpy(result, Display1_Segment, sizeof(Display1_Segment));
memcpy(result + sizeof(Display1_Segment), Display2_Segment, sizeof(Display2_Segment));
Serial.print("2 x 7 Display Bits: ");
for (int i = 0; i < 14; i++)
{
Serial.print(result[i]);
}
Serial.println("");
Serial.print("58 LEDs Bits: ");
for (int i = 0; i < NUM_LEDS; i++)
{
if (i == 16 || i == 45)
{
LEDs_Output[i] = 0;
}
else
{
LEDs_Output[i] = result[index];
Serial.print(LEDs_Output[i]);
counterLED++;
if (counterLED >= LEDS_PER_SEG)
{
index++;
counterLED = 0;
}
}
}
Serial.println("");
// Display Execution + Color Customization
for (int i = 0; i < NUM_LEDS; i++)
{
switch (LEDs_Output[i]) {
case 0:
leds[i] = 0;
break;
case 1:
switch (colorFlag) {
case 0:
leds[i] = CRGB::Orange;
break;
case 1:
leds[i] = CRGB::Green;
break;
case 2:
leds[i] = CRGB::Purple;
break;
case 3:
leds[i] = CRGB::Red;
break;
case 4:
leds[i] = CRGB::White;
break;
}
}
}
// Brightness Modification
switch (brightnessFlag) {
case 0:
FastLED.setBrightness(80);
break;
case 1:
FastLED.setBrightness(100);
break;
case 2:
FastLED.setBrightness(120);
break;
case 3:
FastLED.setBrightness(150);
break;
case 4:
FastLED.setBrightness(200);
break;
case 5:
FastLED.setBrightness(255);
break;
}
FastLED.show();
}
//Special case of display 1 where 0 should not be shown for single digit (without modifying currentDigitDisplay1 variable)
void specialCaseDisplay1()
{
if (currentDigitDisplay1 == 0)
{
for (int i = 0; i < 7; i++)
{
Display1_Segment[i] = 0;
}
}
}
// Interrupt routine for RF module
void interuptingVT()
{
Serial.println("Button Pressed");
if (digitalRead(RF_D0) == HIGH) {
userInput = 'A';
Serial.println("A");
delay(1000);
}
else if (digitalRead(RF_D1) == HIGH) {
userInput = 'B';
Serial.println("B");
delay(1000);
}
else if (digitalRead(RF_D2) == HIGH) {
userInput = 'C';
Serial.println("C");
delay(1000);
}
else if (digitalRead(RF_D3) == HIGH) {
userInput = 'D';
Serial.println("D");
delay(1000);
}
buttonPressedFlag = true;
}