Lighting up an LED strip using millis()

I have 2 led strips that each have 84 leds and are divided into 4 segments of 21 leds each, creating a square. I want to make it so that the two side segments (segments 2 & 4) light up at the same time gradually in groups of 3 at certain intervals, like this:

I'm pretty new to coding so I just found out that I need to be using millis() instead of delay, but I haven't figured out the right way to do it. This is my full code so far:

#include <DS3231.h> //Include the clock library RTC_DS3231 rtc
#include <FastLED.h>

// Changable Vars
unsigned long time;
CRGB leds[84];
CRGB leds2[84];
const int switchPin = 3;
const int switchPin2 = 5;
int fadeTime = 30; // How long the light will fade to max
int interval = ((fadeTime * 60000)/8);
int i = 0;
int setHour = 8; // wake (military time)
int setMin = 30;
int bedHour = 23; // bed
int bedMin = 0;
int workHour = 10; // work
int workMin = 0;
int restHour = 18; // rest
int restMin = 0;
int endHour = 23;
int endMin = 59;
int nightHour = 0; // nightlight
int nightMin = 0;
int timeMarker = 0; // for delays


// Set up Vars
DS3231  rtc(SDA, SCL);
Time t;
void start();
 
void setup()
{
  FastLED.addLeds<WS2812, 10, GRB>(leds, 84);
  FastLED.addLeds<SK6812, 9, GRB>(leds2, 84);
  pinMode(3, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  Serial.begin(9600); // Match to serial monitor
  rtc.begin();
}

void loop()
{
  time = millis();
  Serial.print("pin3: ");
  Serial.println(digitalRead(3));
  Serial.print("pin5: ");
  Serial.println(digitalRead(5));
  t = rtc.getTime(); // Make a time class called 't'
  
  // Send Day-of-Week
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");
  
  // Send date
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");

  // Send time
  Serial.println(rtc.getTimeStr());

  

  if (digitalRead(3) == HIGH && digitalRead(5) == LOW){
    setting1();
  }else if (digitalRead(5) == HIGH && digitalRead(3) == LOW){
    setting2();
  }else {
    setting3();
  }
}

void sunrise()
{
  timeMarker = time;
  // segment 1 
  for(int i = 0; i <= 20; i++)
  {
    leds[i] = CRGB(100, 0, 0);
    leds2[i] = CRGB(200, 0, 0);
    FastLED.show();
  }
  // segments 2 & 4
 // 1
if(time - timeMarker >= interval){
  for(int i = 21; i <= 23 && i >= 81 && i <= 83; i++){
    leds[i] = CRGB(50, 5, 0);
    leds2[i] = CRGB(10, 0, 200);
    FastLED.show();
  }
}
//2
if(time - timeMarker >= (interval * 2)){
  for(int i = 24; i <= 26 && i >= 78 && i <= 80; i++){
    leds[i] = CRGB(50, 5, 0);
    leds2[i] = CRGB(10, 0, 200);
    FastLED.show();
  }
}
//3
if(time - timeMarker >= (interval * 3)){
  for(int i = 27; i <= 29 && i >= 75 && i <= 77; i++){
    leds[i] = CRGB(50, 5, 0);
    leds2[i] = CRGB(10, 0, 200);
    FastLED.show();
  }
}
//4
if(time - timeMarker >= (interval * 4)){
  for(int i = 30; i <= 32 && i >= 72 && i <= 74; i++){
    leds[i] = CRGB(50, 5, 0);
    leds2[i] = CRGB(10, 0, 200);
    FastLED.show();
  }
}
//5
if(time - timeMarker >= (interval * 5)){
  for(int i = 33; i <= 35 && i >= 69 && i <= 71; i++){
    leds[i] = CRGB(50, 5, 0);
    leds2[i] = CRGB(10, 0, 200);
    FastLED.show();
  }
}
//6
if(time - timeMarker >= (interval * 6)){
  for(int i = 36; i <= 38 && i >= 66 && i <= 68; i++){
    leds[i] = CRGB(50, 5, 0);
    leds2[i] = CRGB(10, 0, 200);
    FastLED.show();
  }
}
//7
if(time - timeMarker >= (interval * 7)){
  for(int i = 39; i <= 41 && i >= 63 && i <= 65; i++){
    leds[i] = CRGB(50, 5, 0);
    leds2[i] = CRGB(10, 0, 200);
    FastLED.show();
  }
}
  // segment 3 
  if(time - timeMarker >= (interval * 8)){
  for(int i = 42; i <= 62; i++)
  {
    leds[i] = CRGB(50, 5, 5);
    leds2[i] = CRGB(10, 0, 200);
    FastLED.show();
  }
  timeMarker = time;
  }
  if(time - timeMarker >= ((fadeTime * 60000)/2)){
    for(int i = 0; i <= 83; i++){
      leds[i] = CRGB(0, 0, 0);
      leds2[i] = CRGB(0, 0, 0);
      FastLED.show();
    }
  }
  delay (1000);
}

  
void sunset()
{
  timeMarker = time;
  // beginning
for(int i = 0; i <= 20; i++)
{
  leds[i] = CRGB(100, 20, 0);
  leds2[i] = CRGB(200, 0, 0);
  FastLED.show();
}
for(int i = 21; i <= 41; i++)
{
  leds[i] = CRGB(0, 0, 0);
  leds2[i] = CRGB(255, 0, 100);
  FastLED.show();
} 
for(int i = 42; i <= 62; i++)
{
  leds[i] = CRGB(5, 0, 100);
  leds2[i] = CRGB(0, 0, 0);
  FastLED.show();
}
for(int i = 63; i <= 83; i++)
{
  leds[i] = CRGB(0, 0, 0);
  leds2[i] = CRGB(255, 0, 100);
  FastLED.show();
}
if(time - timeMarker >= 1800000){
  // segment 1 
  for(int i = 42; i <= 62; i++)
  {
    leds[i] = CRGB(0, 0, 0);
    leds2[i] = CRGB(0, 0, 0);
    FastLED.show();
  }
  timeMarker = time;
}
  // segments 2 & 4
// 1
if(time - timeMarker >= 225000){
  for(int i = 39; i <= 41 && i >= 63 && i <= 65; i++){
    leds[i] = CRGB(0, 0, 0);
    leds2[i] = CRGB(0, 0, 0);
    FastLED.show();
  }
  timeMarker = time;
}
//2
if(time - timeMarker >= 225000){
  for(int i = 36; i <= 38 && i >= 66 && i <= 68; i++){
    leds[i] = CRGB(0, 0, 0);
    leds2[i] = CRGB(0, 0, 0);
    FastLED.show();
  }
  timeMarker = time;
}
//3
if(time - timeMarker >= 225000){
  for(int i = 33; i <= 35 && i >= 69 && i <= 70; i++){
    leds[i] = CRGB(0, 0, 0);
    leds2[i] = CRGB(0, 0, 0);
    FastLED.show();
  }
  timeMarker = time;
}
//4
if(time - timeMarker >= 225000){
  for(int i = 30; i <= 32 && i >= 72 && i <= 74; i++){
    leds[i] = CRGB(0, 0, 0);
    leds2[i] = CRGB(0, 0, 0);
    FastLED.show();
  }
  timeMarker = time;
}
//5
if(time - timeMarker >= 225000){
  for(int i = 27; i <= 29 && i >= 75 && i <= 77; i++){
    leds[i] = CRGB(0, 0, 0);
    leds2[i] = CRGB(0, 0, 0);
    FastLED.show();
  }
  timeMarker = time;
}
//6
if(time - timeMarker >= 225000){
  for(int i = 24; i <= 26 && i >= 78 && i <= 80; i++){
    leds[i] = CRGB(0, 0, 0);
    leds2[i] = CRGB(0, 0, 0);
    FastLED.show();
  }
  timeMarker = time;
}
//7
if(time - timeMarker >= 225000){
  for(int i = 21; i <= 23 && i >= 81 && i <= 83; i++){
    leds[i] = CRGB(0, 0, 0);
    leds2[i] = CRGB(0, 0, 0);
    FastLED.show();
  }
  timeMarker = time;
}
  // segment 3 
 if(time - timeMarker >= 225000){ 
  for(int i = 0; i <= 20; i++)
  {
    leds[i] = CRGB(0, 0, 0);
    leds2[i] = CRGB(0, 0, 0);
    FastLED.show();
  }
 }
 delay (1000);
}

  void work()
  {
    for(int i = 0; i <= 20; i++)
    {
      leds[i] = CRGB(0, 0, 0);
      leds2[i] = CRGB(0, 50, 200);
      FastLED.show();
    }
    for(int i = 21; i <= 83; i++)
    {
      leds[i] = CRGB(0, 50, 200);
      leds2[i] = CRGB(0, 200, 0);
      FastLED.show();
    }
    delay (1000);
  }

  void rest()
{
   for(int i = 0; i <= 20; i++)
    {
      leds[i] = CRGB(100, 20, 0);
      leds2[i] = CRGB(255, 0, 100);
      FastLED.show();
    }
    for(int i = 21; i <= 41; i++)
    {
      leds[i] = CRGB(0, 0, 0);
      leds2[i] = CRGB(255, 0, 100);
      FastLED.show();
    }
     for(int i = 42; i <= 62; i++)
    {
      leds[i] = CRGB(5, 0, 200);
      leds2[i] = CRGB(0, 0, 0);
      FastLED.show();
    }
    for(int i = 63; i <= 83; i++)
    {
      leds[i] = CRGB(0, 0, 0);
      leds2[i] = CRGB(255, 0, 100);
      FastLED.show();
    }
    delay (1000);
}

void night()
{
  for(int i = 0; i <= 20; i++)
  {
    leds[i] = CRGB(100, 0, 0);
    leds2[i] = CRGB(0, 0, 0);
    FastLED.show();
  }
  for(int i = 21; i <= 83; i++)
  {
    leds[i] = CRGB(0, 0, 0);
    leds2[i] = CRGB(0, 0, 0);
    FastLED.show();
  }
  delay (1000);
}

void setting1(){
  
    if (t.hour >= setHour && t.hour < workHour && t.min >= setMin) // Check if it's time to wake up!
  {
    sunrise();
  }
  
  else if (t.hour >= bedHour && t.min >= bedMin)
  {
    sunset();
 

  }
}

void setting2(){
   
    if (t.hour >= workHour && t.hour < restHour && t.min >= workMin)
  {
    work();
  }
  else if (t.hour >= restHour && t.hour <= endHour && t.min >= restMin && t.min <= endMin)
  {
    rest();
  }
  else if (t.hour >= nightHour && t.hour < workHour && t.min >= nightMin)
  {
    night();
  }
}

void setting3(){
    
    for(int i = 0; i <= 83; i++)
    {
    leds[i] = CRGB(0, 0, 0);
    leds2[i] = CRGB(0, 0, 0);
    FastLED.show();
  }
  // Wait one second before repeating
  delay (1000);
}

And this is the part that I'm struggling with:

void sunrise()
{
  timeMarker = time;
  // segment 1 
  for(int i = 0; i <= 20; i++)
  {
    leds[i] = CRGB(100, 0, 0);
    leds2[i] = CRGB(200, 0, 0);
    FastLED.show();
  }
  // segments 2 & 4
 // 1
if(time - timeMarker >= interval){
  for(int i = 21; i <= 23 && i >= 81 && i <= 83; i++){
    leds[i] = CRGB(50, 5, 0);
    leds2[i] = CRGB(10, 0, 200);
    FastLED.show();
  }
}
//2
if(time - timeMarker >= (interval * 2)){
  for(int i = 24; i <= 26 && i >= 78 && i <= 80; i++){
    leds[i] = CRGB(50, 5, 0);
    leds2[i] = CRGB(10, 0, 200);
    FastLED.show();
  }
}
//3
if(time - timeMarker >= (interval * 3)){
  for(int i = 27; i <= 29 && i >= 75 && i <= 77; i++){
    leds[i] = CRGB(50, 5, 0);
    leds2[i] = CRGB(10, 0, 200);
    FastLED.show();
  }
}
//4
if(time - timeMarker >= (interval * 4)){
  for(int i = 30; i <= 32 && i >= 72 && i <= 74; i++){
    leds[i] = CRGB(50, 5, 0);
    leds2[i] = CRGB(10, 0, 200);
    FastLED.show();
  }
}
//5
if(time - timeMarker >= (interval * 5)){
  for(int i = 33; i <= 35 && i >= 69 && i <= 71; i++){
    leds[i] = CRGB(50, 5, 0);
    leds2[i] = CRGB(10, 0, 200);
    FastLED.show();
  }
}
//6
if(time - timeMarker >= (interval * 6)){
  for(int i = 36; i <= 38 && i >= 66 && i <= 68; i++){
    leds[i] = CRGB(50, 5, 0);
    leds2[i] = CRGB(10, 0, 200);
    FastLED.show();
  }
}
//7
if(time - timeMarker >= (interval * 7)){
  for(int i = 39; i <= 41 && i >= 63 && i <= 65; i++){
    leds[i] = CRGB(50, 5, 0);
    leds2[i] = CRGB(10, 0, 200);
    FastLED.show();
  }
}
  // segment 3 
  if(time - timeMarker >= (interval * 8)){
  for(int i = 42; i <= 62; i++)
  {
    leds[i] = CRGB(50, 5, 5);
    leds2[i] = CRGB(10, 0, 200);
    FastLED.show();
  }
  timeMarker = time;
  }
  if(time - timeMarker >= ((fadeTime * 60000)/2)){
    for(int i = 0; i <= 83; i++){
      leds[i] = CRGB(0, 0, 0);
      leds2[i] = CRGB(0, 0, 0);
      FastLED.show();
    }
  }
  delay (1000);
}

(I know that sunset() looks pretty rough too, I'll fix it once I know how to fix sunrise()).
The way it is now, the bottom segment lights up and after a few minutes the top one does, so it seems like it's basically skipping over the side segments. Any advice?

The code you posted has a compilation error in the definition of the "time" variable:

unsigned long time;
Time t;

Tell us which "arduino" or ESPXXX you are using.

I'm using an Arduino Nano and a DS3231. I don't get a compilation error, "time" refers to millis and time "t" is the actual time from the clock module. Or should I change the name of the millis variable?

Your LEDs are mis-numbered.

Bottom row: 0 - 20
Left column: 21 - 41
Top row: 42 - 62
Right column: 63 - 83

Verify the arrows connecting the LED strips are going "clockwise." You may have soldered DOUT to DOUT on the sides... (should be DOUT to DIN).

I did use the correct numbers in the code, just used the actual numbers in the graph, and I know it's not an issue of the connections because simpler codes have worked fine, so it's definitely the code

I think this is not a legal "for" statement. If you want three LEDs on each column to light (for example, 21, 22, 23), then use the first part:

for (int i = 21; i <= 23; i++)

then use "i" plus the offset of 60... (not tested)

leds[i] = CRGB(50, 5, 0); // left column
leds[i+60] = CRGB(50, 5, 0); // right column
leds2[i] = CRGB(10, 0, 200); // left column
leds2[i+60] = CRGB(10, 0, 200); // right column

This thread had some good hints and references on how to do some non-blocking FastLED animations with different segments of strips:

Note: i is incrementing. The for will cease the first time i fails the condition, which is i=24. End of loop.
Also note, your sections are at 21 offsets, so start at 0, 21, 42, 63, 84.
To do what you're trying to do, as @xfpd indicated, you need to do something like(untested):

for(int i = 0; i<3 ; i++) {
// and then, do something like
   leds[i+20] = CRGB(50, 5, 0);
   leds2[i+84] = CRGB(10, 0, 200);
FastLED.show();
}

<edit - apologies, @xfpd , clicked on the wrong 'reply' button>

Won't it stop before it starts?

2 Likes

I tried this:

void sunrise()
{
  timeMarker = time;
  // segment 1 
  for(int i = 0; i <= 20; i++)
  {
    leds[i] = CRGB(50, 0, 0);
    leds2[i] = CRGB(100, 0, 0);
    FastLED.show();
  }
  // segments 2 & 4
  // 1
if(time - timeMarker >= interval){
  for(int i = 21; i <= 23; i++){
    leds[i] = CRGB(50, 5, 0);
    leds2[i] = CRGB(10, 0, 200);
    leds[i + 60] = CRGB(50, 5, 0);
    leds2[i + 60] = CRGB(10, 0, 200);
    FastLED.show();
  }
}
//2
if(time - timeMarker >= (interval * 2)){
  for(int i = 24; i <= 26; i++){
    leds[i] = CRGB(50, 5, 0);
    leds2[i] = CRGB(10, 0, 200);
    leds[i + 54] = CRGB(50, 5, 0);
    leds2[i + 54] = CRGB(10, 0, 200);
    FastLED.show();
  }
}
//3
if(time - timeMarker >= (interval * 3)){
  for(int i = 27; i <= 29; i++){
    leds[i] = CRGB(50, 5, 0);
    leds2[i] = CRGB(10, 0, 200);
    leds[i + 48] = CRGB(50, 5, 0);
    leds2[i + 48] = CRGB(10, 0, 200);
    FastLED.show();
  }
}
//4
if(time - timeMarker >= (interval * 4)){
  for(int i = 30; i <= 32; i++){
    leds[i] = CRGB(50, 5, 0);
    leds2[i] = CRGB(10, 0, 200);
    leds[i + 42] = CRGB(50, 5, 0);
    leds2[i + 42] = CRGB(10, 0, 200);
    FastLED.show();
  }
}
//5
if(time - timeMarker >= (interval * 5)){
  for(int i = 33; i <= 35; i++){
    leds[i] = CRGB(50, 5, 0);
    leds2[i] = CRGB(10, 0, 200);
    leds[i + 36] = CRGB(50, 5, 0);
    leds2[i + 36] = CRGB(10, 0, 200);
    FastLED.show();
  }
}
//6
if(time - timeMarker >= (interval * 6)){
  for(int i = 36; i <= 38; i++){
    leds[i] = CRGB(50, 5, 0);
    leds2[i] = CRGB(10, 0, 200);
    leds[i + 30] = CRGB(50, 5, 0);
    leds2[i + 30] = CRGB(10, 0, 200);
    FastLED.show();
  }
}
//7
if(time - timeMarker >= (interval * 7)){
  for(int i = 39; i <= 41; i++){
    leds[i] = CRGB(50, 5, 0);
    leds2[i] = CRGB(10, 0, 200);
    leds[i + 24] = CRGB(50, 5, 0);
    leds2[i + 24] = CRGB(10, 0, 200);
    FastLED.show();
  }
}
  // segment 3 
if(time - timeMarker >= (interval * 8)){
  for(int i = 42; i <= 62; i++)
  {
    leds[i] = CRGB(50, 5, 5);
    leds2[i] = CRGB(10, 0, 200);
    FastLED.show();
  }
  timeMarker = time;
  }
  if(time - timeMarker >= ((fadeTime * 60000)/2)){
    for(int i = 0; i <= 83; i++){
      leds[i] = CRGB(0, 0, 0);
      leds2[i] = CRGB(0, 0, 0);
      FastLED.show();
    }
  }
  delay (1000);
}

And it lit up the first segment, and after ~40 seconds (faster than it should’ve done anything else), it lit all 3 of the others up, but did 3 on 3 off on the sides:

Can you describe the pattern do you want? In your original post, you want "groups of 3" at an interval. I interpreted your code as wanting 3 on 3 off from this...

My bad, I meant that I want the bottom 3 leds of segments 2 & 4 to light up, and then after x amount of millis, the next 3, and so on

if it helps, this is what the code used to look like which actually did what i wanted it to:
(z = 21)

void sunrise()
{
  // segment 1 
  for(int i = 0; i <= 20; i++)
  {
    leds[i] = CRGB(100, 0, 0);
    leds2[i] = CRGB(200, 0, 0);
    FastLED.show();
  }
  delay((fadeTime * 60000)/8);
  // segments 2 & 4
  for(int i = 83; i >= 63; i -= 3)
 {
    leds[i] = CRGB(50, 5, 0);
    leds2[i] = CRGB(10, 0, 200);
    leds[i - 1] = CRGB(50, 5, 0);
    leds2[i - 1] = CRGB(10, 0, 200);
    leds[i - 2] = CRGB(50, 5, 0);
    leds2[i - 2] = CRGB(10, 0, 200);
    leds[z] = CRGB(50, 5, 0);
    leds2[z] = CRGB(10, 0, 200);
    leds[z + 1] = CRGB(50, 5, 0);
    leds2[z + 1] = CRGB(10, 0, 200);
    leds[z + 2] = CRGB(50, 5, 0);
    leds2[z + 2] = CRGB(10, 0, 200);
    FastLED.show();
    z += 3; 
    delay((fadeTime * 60000)/8);
  }
  // segment 3 
  for(int i = 42; i <= 62; i++)
  {
    leds[i] = CRGB(50, 5, 5);
    leds2[i] = CRGB(10, 0, 200);
    FastLED.show();
  }
  }

the problem is, i can't use delays because i need other functions to be able to run

Look at this:

and

The thread has good hints about delay-less multiple animations running over portions of strips.

Does "time" come from the DS3231 library? I do not understand how your sketch compiles with "time" as a re-defined variable from the c library.

I used your function called "sunrise()" and removed the "timeMaker" references (because I can not simulate a DS3231) and used Arduino's millis() function.

The result probably does not look like what you want... but I can adjust it to do anything... just let me know.

You are making the common beginner mistake of calling FastLED.show() too often. The example above is going to call FastLED.show() 20 times with no delay of any kind between the calls. It will look like all the affected LEDs switch on at almost the same instant, but not quite at the same instant, because it will take the Arduino 20 times longer to do it than is necessary. It can also cause flickering and problems performing those other functions that you want the Arduino to to while the animation is running.

To avoid calling FastLED.show() too often, only call it after all the changes you want to happen at the same instant have been done. In the above example, it should probably be:

  for(int i = 0; i <= 20; i++)
  {
    leds[i] = CRGB(100, 0, 0);
    leds2[i] = CRGB(200, 0, 0);
  }
  FastLED.show();
1 Like

Yeah, I'm getting the actual time from the DS3231 library. I changed the variable for millis to tempTime to avoid confusion. This seems to partially do what I want it to:

void sunrise()
{
  int tempTime = millis();
  // segment 1 
  for(int i = 0; i <= 20; i++)
  {
    leds[i] = CRGB(50, 0, 0);
    leds2[i] = CRGB(100, 0, 0);
  }
  FastLED.show();
  // segments 2 & 4
  // 1
  tempTime = millis();
  while(millis() - tempTime < (5000)){
    if (digitalRead(3) == LOW && digitalRead(5) == HIGH){
      setting2();
    } else if (digitalRead(3) == LOW && digitalRead(5) == LOW){
      setting3();
    }
  }
 for(int i = 83; i >= 63; i -= 3)
 {
    leds[i] = CRGB(50, 5, 0);
    leds2[i] = CRGB(10, 0, 200);
    leds[i - 1] = CRGB(50, 5, 0);
    leds2[i - 1] = CRGB(10, 0, 200);
    leds[i - 2] = CRGB(50, 5, 0);
    leds2[i - 2] = CRGB(10, 0, 200);
    leds[z] = CRGB(50, 5, 0);
    leds2[z] = CRGB(10, 0, 200);
    leds[z + 1] = CRGB(50, 5, 0);
    leds2[z + 1] = CRGB(10, 0, 200);
    leds[z + 2] = CRGB(50, 5, 0);
    leds2[z + 2] = CRGB(10, 0, 200);
    FastLED.show();
    z += 3; 
    tempTime = millis();
    while(millis() - tempTime < (5000)){
      if (digitalRead(3) == LOW && digitalRead(5) == HIGH){
      setting2();
    } else if (digitalRead(3) == LOW && digitalRead(5) == LOW){
      setting3();
    }
    }
 }
 tempTime = millis();
while(millis() - tempTime < (5000)){
  if (digitalRead(3) == LOW && digitalRead(5) == HIGH){
      setting2();
    } else if (digitalRead(3) == LOW && digitalRead(5) == LOW){
      setting3();
    }
}
  // segment 3 
  for(int i = 42; i <= 62; i++)
  {
    leds[i] = CRGB(50, 5, 5);
    leds2[i] = CRGB(10, 0, 200);
    
  }
  FastLED.show();
  delay (1000);
}

2 problems: 1) it only works for smaller timeframes, in this case, 5 seconds. When I try to use the actual intended timeframe (225k millis aka 3.75 min), it gets confused. 2) The switch I have it connected to (which should turn it off or switch it to the night() function) doesn't work while it's running, which is the same problem I had when using delays

Make tempTime an unsigned long because otherwise strange things will happen across -32768ms and 32767ms.

  unsigned long tempTime = millis();

that seems to have solved the first problem, thanks!

Is this switch (attempted to be) activated in the code as night happens?

Would you post the updated code?