This code is working fine certain LED turn on and showing day of the week. but if i use
this code for date i need 31 if statements. so i want to simplify this code.
#include <FastLED.h>
#include <TimeLib.h>
#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
#define DATA_PIN 3
#define NUM_LEDS 7
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
Serial.begin(9600);
while (!Serial) ; // wait until Arduino Serial Monitor opens
setSyncProvider(RTC.get); // the function to get the time from the RTC
if(timeStatus()!= timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
}
void loop()
{
Week();
if (timeStatus() == timeSet) {
digitalClockDisplay();
} else {
Serial.println("The time has not been set. Please run the Time");
Serial.println("TimeRTCSet example, or DS1307RTC SetTime example.");
Serial.println();
delay(4000);
}
delay(1000);
}
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(weekday());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
void Week(){
int myWeek = second();
Serial.println(myWeek);
//for (int i=1;i<=7;i+1);
//leds[NUM_LEDS]= CRGB::Red;
if (myWeek == 1){
leds[0] = CRGB::Red;
}
if (myWeek == 2){
leds[1] = CRGB::Red;
}
if (myWeek == 3){
leds[2] = CRGB::Red;
}
if (myWeek == 4){
leds[3] = CRGB::Red;
}
if (myWeek == 5){
leds[4] = CRGB::Red;
}
if (myWeek == 6){
leds[5] = CRGB::Red;
}
if (myWeek == 7){
leds[6] = CRGB::Red;
}
//leds [NUM_LEDS] = CRGB::Black;
FastLED.show();
FastLED.clear();
}
This code uses a conditional statement to check if the current day is between 1 and 7 (inclusive), and if it is, it sets the corresponding LED to red based on the index. This way, you don't need to write separate if statements for each day.
It would be better if you tried to solve problems yourself rather than passively waiting for help.
The forum is not a solution factory for newbies, it is a place for learning.
Please encourage newcomers. Don't let them discourage. You know what I learned from him. Do not be proud of your intelligence. Pride always has its head down. If you can be of service to someone, consider it an honor for yourself. I am sorry. You have discouraged me.