#include <FastLED.h>
#include <RTClib.h>
#include <Wire.h>
// RF CONTROL #include <WirelessRemoteController.h>
RTC_DS3231 rtc;
char t[32];
char tBaseline[32];
int baseHour;
int currentHour;
#define RECEIVER_PIN 2 // either 2 or 3
/* RF CONTROL
// Create wireless remote controller instance with D0, D1, D2, D3 pins as data pins
WirelessRemoteController remote_controller(11, 10, 9, 8);
*/
boolean current_rx[4] = {1, 0, 0, 0}; //DELETE THIS IN ACTUAL PROGRAM
boolean recieverInput[4] = {0};
byte userInput = 0b0000;
String cond;
#define LED_TYPE WS2812B
#define LED_PIN 4
#define NUM_LEDS 56 // 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;
//byte pos[10][10] = {0}
boolean segValues[10][7] =
{
{1, 1, 1, 0, 1, 1, 1}, //0
{0, 0, 1, 0, 0, 0, 1}, //1
{0, 1, 1, 1, 1, 1, 0}, //2
{0, 1, 1, 1, 0, 1, 1}, //3
{1, 0, 1, 1, 0, 0, 1}, //4
{1, 1, 0, 1, 0, 1, 1}, //5
{1, 1, 0, 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;
//ROUGH Testing all functions
int c = 0;
void setup()
{
Serial.begin(9600);
Wire.begin();
rtc.begin();
//rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));
//rtc.adjust(DateTime(2023, 9, 18, 3, 0, 0));
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.print(F("Date/Time: "));
Serial.println("Following is complete base time");
Serial.println(tBaseline);
baseHour = now.second();
//pinMode(RECEIVER_PIN, INPUT_PULLUP);
/* RF CONTROL
// Trigger interrupt by putting D2 pin as DT remote controller pin (trigger)
remote_controller.addTrigger(2, receiveFromRemoteController);
Serial.print("Data sent from remote controller will be printed when received.\n");
*/
LED_Initialisation();
//DELETE IN ACTUAL PROGRAM JUST CHECKING AS RF MODULE
Serial.print("User Input: ");
for (int i = 0; i < 4; i++)
{
Serial.print(current_rx[i]);
recieverInput[i] = current_rx[i];
userInput = (userInput << 1 | recieverInput[i]);
}
Serial.println(" ");
Serial.println("SHOWING ALL THE STORED DIGITS (0-9) FOR SEGMENT LEDS STORED");
//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(" ");
}
}
void loop()
{
Serial.print("Testing Condition: ");
Serial.println(c);
//Testing 00-99 boundary and other functions
c++;
switch (c) {
// increment
case 1:
userInput = 0b1000;
break;
case 2:
userInput = 0b1000;
break;
case 3:
userInput = 0b1000;
break;
case 4:
userInput = 0b1000;
break;
case 5:
userInput = 0b1000;
break;
case 6:
userInput = 0b1000;
break;
case 7:
userInput = 0b1000;
break;
case 8:
userInput = 0b1000;
break;
case 9:
userInput = 0b1000;
break;
case 10:
userInput = 0b1000;
break;
case 11:
userInput = 0b1000;
break;
// decrement
case 12:
userInput = 0b0100;
break;
case 13:
userInput = 0b0100;
break;
case 14:
userInput = 0b0100;
break;
case 15:
userInput = 0b0100;
break;
case 16:
userInput = 0b0100;
break;
case 17:
userInput = 0b0100;
break;
case 18:
userInput = 0b0100;
break;
case 19:
userInput = 0b0100;
break;
case 20:
userInput = 0b0100;
break;
case 21:
userInput = 0b0100;
break;
case 22:
userInput = 0b0100;
break;
case 23:
userInput = 0b0100;
break;
case 24:
userInput = 0b0100;
break;
case 25:
userInput = 0b0100;
break;
case 26:
userInput = 0b0100;
break;
case 27:
userInput = 0b0100;
break;
case 28:
userInput = 0b0100;
break;
case 29:
userInput = 0b0100;
break;
case 30:
userInput = 0b0100;
break;
case 31:
userInput = 0b0100;
break;
case 32:
userInput = 0b0100;
break;
case 33:
userInput = 0b0100;
break;
case 34:
userInput = 0b0100;
break;
case 35:
userInput = 0b0100;
break;
//color
case 36:
userInput = 0b0010;
break;
case 37:
userInput = 0b0010;
break;
case 38:
userInput = 0b0010;
break;
case 39:
userInput = 0b0010;
break;
case 40:
userInput = 0b0010;
break;
case 41:
userInput = 0b0010;
break;
//brightness
case 42:
userInput = 0b0001;
break;
case 43:
userInput = 0b0001;
break;
case 44:
userInput = 0b0001;
break;
case 45:
userInput = 0b0001;
break;
case 46:
userInput = 0b0001;
break;
case 47:
userInput = 0b0001;
break;
case 48:
userInput = 0b0001;
break;
case 49:
userInput = 0b0000;
c = 48;
break;
}
//userInput = 0b0000; //for timer testing fixed it and let timer do change
//performIncrement();
checkTime();
Serial.print("User Input: ");
Serial.println(userInput);
checkCondition(userInput);
//numberAssign(1);
//memcpy(Display1_Segment, segValue, sizeof(segValue));
/*
for (int i = 0; i < NUM_LEDS; i++)
{
Serial.println(Array_LED[i]);
}
*/
}
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(1000);
currentHour = now.second();
Serial.println("Following is time difference");
Serial.println(abs(currentHour - baseHour));
if (abs(currentHour - baseHour) >= 24)
{
userInput = 0b0100;
baseHour = now.second();
}
}
void checkCondition(int recieverValue)
{
switch (recieverValue) {
case 8: //'1000' Button A
cond = "Increment";
performIncrement();
break;
case 4: //'0100' Button B
cond = "Decrement";
performDecrement();
break;
case 2: //'0010' Button C
cond = "Color";
performColorChange();
break;
case 1: //'0001' Button D
cond = "Brightness";
performBrightnessChange();
break;
default:
cond = "Increment";
}
}
//Operations on different button press
void performIncrement()
{
if (currentDigitDisplay2 > 9)
{
currentDigitDisplay2 = 0;
}
if (currentDigitDisplay2 == 0)
{
//Serial.print("ALERT:");
//Serial.println(currentDigitDisplay2);
if (currentDigitDisplay1 > 8)
{
currentDigitDisplay1 = 0;
}
//delay(1000);
memcpy(Display1_Segment, segValues[currentDigitDisplay1], sizeof(segValues[currentDigitDisplay1]));
/*
for (int i = 0; i < 7; i++)
{
Serial.print(Display1_Segment[i]);
}
Serial.println(" ");
*/
currentDigitDisplay1++;
specialCaseDisplay1();
// display();
}
//Serial.println(currentDigitDisplay2);
memcpy(Display2_Segment, segValues[currentDigitDisplay2++], sizeof(segValues[currentDigitDisplay2++]));
display();
delay(1000);
}
void performDecrement()
{
if (currentDigitDisplay2 < 0)
{
//Serial.print("ALERT:");
//Serial.println(currentDigitDisplay2);
currentDigitDisplay2 = 9;
}
if (currentDigitDisplay2 == 9)
{
currentDigitDisplay1--;
if (currentDigitDisplay1 < 1)
{
currentDigitDisplay1 = 9;
}
//delay(1000);
memcpy(Display1_Segment, segValues[currentDigitDisplay1], sizeof(segValues[currentDigitDisplay1]));
/*
for (int i = 0; i < 7; i++)
{
Serial.print(Display1_Segment[i]);
}
Serial.println(" ");
*/
specialCaseDisplay1();
// display();
}
memcpy(Display2_Segment, segValues[currentDigitDisplay2--], sizeof(segValues[currentDigitDisplay2--]));
display();
delay(1000);
}
void performColorChange()
{
if (colorFlag < 4)
{
colorFlag++;
}
else
colorFlag = 0;
display();
}
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 numberAssign(boolean num)
{
switch (num) {
case 1:
segValue = {0, 0, 1, 0, 0, 0, 1};
break;
case 2:
segValue = 0111110;
break;
case 3:
segValue = 0111011;
break;
case 4:
segValue = 1011001;
break;
}
}
*/
void display ()
{
index = 0;
memcpy(result, Display1_Segment, sizeof(Display1_Segment));
memcpy(result + sizeof(Display1_Segment), Display2_Segment, sizeof(Display2_Segment));
Serial.println("Total LEDs: ");
Serial.println(sizeof(LEDs_Output));
// For Testing Display
/*
memcpy(result, segValues[2], sizeof(segValues[2]));
memcpy(result + sizeof(segValues[2]), segValues[4], sizeof(segValues[4]));
*/
Serial.print("2 x 7 Display Bits: ");
for (int i = 0; i < 14; i++)
{
Serial.print(result[i]);
}
Serial.println("");
Serial.print("56 LEDs Bits: ");
for (int i = 0; i < NUM_LEDS; i++)
{
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();
}
void specialCaseDisplay1()
{
if (currentDigitDisplay1 == 1)
{
for (int i = 0; i < 7; i++)
{
Display1_Segment[i] = 0;
//Serial.print(Display1_Segment[i]);
}
/*
for (int i = 0; i < 7; i++)
{
Serial.print(Display1_Segment[i]);
}
Serial.println(" ");
*/
}
}
/* RF CONTROL
void receiveFromRemoteController()
{
bool current_rx[4];
if(remote_controller.getCurrentValue(current_rx))
{
Serial.print("Data received: ");
for (int i = 0; i < 4; i++)
{
Serial.print(current_rx[i]);
Serial.print(" ");
recieverInput[i] = current_rx[i];
userInput = (userInput << 1 | recieverInput[i]);
}
Serial.print("\n");
}
else
{
Serial.print("No data received (!!should not happen!!)\n");
}
}
*/