How i can reduce this code

HOW I CAN REDUCE THIS CODE BECAUSE I WANT TO USE SAME CODE FOR DAYS IN MY CLOCK.

void Week(){
 
int myWeek = weekday();
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;
}

FastLED.show();
FastLED.clear();

}

No need to SHOUT.

Try it this way - you have a number and want to turn on a certain LED:

leds[myWeek - 1] = CRGB::Red;

HTH

a7

1 Like

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.

That is why "arrays" were invented!

Tip: It should be very similar to what @alto777 suggested above

1 Like

I have already tried the suggestion but not working only one led ON and then OFF.

C'est impossible!

Please post a complete sketch that we can compile and run for ourselves, that applies the suggestion and the suggestion fails to work.

a7

 #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();

}

correct it int myweek =weekday();

why not weekday()

https://playground.arduino.cc/Code/Time/

yes i know i change it second for test my code

Here is the optimized version of the code that you can use for days in your clock:

void Week() {
  int myWeek = weekday();
  Serial.println(myWeek);
  
  if (myWeek >= 1 && myWeek <= 7) {
    leds[myWeek - 1] = CRGB::Red;
  }
  
  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.

Bundle of Thanks for help me. Your optimized version work fine . can i use this statement for dates? it will work or no.

Probably, but if you can't let me know and I will try to help you out.

ok Thanks i will try this code on showing Dates.

I guess we'll never know how you bungled applying the suggestion. I'm glad someone was able to spell it out for you in a way you could understand.

You don't need, however, the if testing. In the context of the DS1307 weekday is already a number between including 1 and 7.

a7

Hi
I can simulate this sketch on wokwi simulator

Yeah as long as its got everything you need to simulate it.

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.

2 Likes

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.