Arduino Day And Night

The code in this page will change the daylight conditions in your diorama or model railroad and at the same time turn on and off your street lights.

Credit Philippe “Smitty” Desmyttere¬† https://www.facebook.com/groups/926390087391995/user/1168034489

 

Materials Required

Arduino Nano

Pixel LED Strips

LED street and building lights

 

Code:

/*
Sunrise-Day-Sunset-Night sketch for RGB led type WS2811 with ‘BRG’ color.
This is performed in a time lapse of 30 minutes or 11520 steps.
Meanwhile the street lights are gradually switched On and Off.
Modified by Philippe “Smitty” Desmyttere (March 2021). https://www.facebook.com/groups/926390087391995/user/1168034489
This code is in public domain so feel free to use/modify it.
*/

//—————————–
//———-LED DATA———–
#define FASTLED_INTERNAL
#include <FastLED.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#define NUM_LEDS 3 // NUMBER OF PIXELS IN THE USED STRIP
#define DATA_PIN 6

uint16_t currentPixel = 0; / SET TO PIXEL NUMBER 0
uint16_t led = 0;

int STREET_LED = 9; // THE PIN THAT THE STREET LEDs ARE ATTACHED TO
int brightness_STREET = 0; // BRIGHTNESS OF THJE STREET LEDs AT STARTUP
int fadeAmount = 1; KeeP THIS AMOUNT UNDER 5 FOR A NICE FADING OF THE STREET LEDs

LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 4 line display

//—————————–
//———–TIMING————

unsigned long currentMillis = 0;
unsigned long previousMillis = 0;

unsigned long speedOfNightToMorning = 1800; // DURATION IN STEPS OF nightDayCycle ROUTINE – 6steps/second (1 minute = 360 steps)
unsigned long speedOfMorningToNoon = 3600; // DURATION IN STEPS OF dayNoonCycle ROUTINE – 6steps/second (1 minute = 360 steps)
unsigned long speedOfNoonToEvening = 4320; // DURATION IN STEPS OF noonEveningCycle ROUTINE – 6steps/second (1 minute = 360 steps)
unsigned long speedOfEveningToNight = 1800; // DURATION IN STEPS OF eveningNightCycle ROUTINE – 6steps/second (1 minute = 360 steps)

//_____________________________
//———TEST TIMING———
//unsigned long speedOfNightToMorning = 100;//1800; // DURATION IN STEPS OF nightDayCycle ROUTINE – 6steps/second (1 minute = 360 steps)
//unsigned long speedOfMorningToNoon = 100;//3600; // DURATION IN STEPS OF dayNoonCycle ROUTINE – 6steps/second (1 minute = 360 steps)
//unsigned long speedOfNoonToEvening = 100;//4320; // DURATION IN STEPS OF noonEveningCycle ROUTINE – 6steps/second (1 minute = 360 steps)
//unsigned long speedOfEveningToNight = 100;//1800; // DURATION IN STEPS OF eveningNightCycle ROUTINE – 6steps/second (1 minute = 360 steps)

unsigned long timeBaseInterval = speedOfEveningToNight + speedOfNightToMorning; // TIMEBASE FOR THE EVENING + NIGHT CYCLE
unsigned long time_Now = 0;

byte maxBrightness = 150; // VALUES FROM 0 – 255 -> THE HIGHER, THE MORE Amps NEEDED – FOR THE LED STRIPS

static const float intervalNightToMorning = speedOfNightToMorning; / STEPS PER MILLISECONDS
static const float intervalMorningToNoon = speedOfMorningToNoon;
static const float intervalNoonToEvening = speedOfNoonToEvening;
static const float intervalEveningToNight = speedOfEveningToNight;

//—————————–
//———-PALETTES———–

DEFINE_GRADIENT_PALETTE (Night_To_Morning_gp) { // NIGHT TO MORNING PALETTE (nightDayCycle)
0, 0,51,102, //Dark Midnight Blue
64, 0,35,71, //Oxford Blue
102, 0, 0, 0, //Rich Black
153, 0, 0, 0, //Rich Black
191,0,35,71, //Oxford Blue
255,0,51,102, //Dark Midnight Blue
};

DEFINE_GRADIENT_PALETTE (Morning_To_Noon_gp) { / MORNING TO NOON PALETTE (dayNoonCycle)
0,0,63,125, //Dark Cerulean Blue
36,45,45,45, //Charleston Green
85,34,34,34, //Raisin Black
167,0,69,84, //Midnight Green
255,255,142,0, //Dark Orange
};

DEFINE_GRADIENT_PALETTE (Noon_To_Evening_gp) { // NIGHT TO DAY PALETTE (noonEveningCycle)
0,253,119,2, //Heat Wave Orange
90,255,80,3, //Aerospace International Orange
100, 50, 50, 200, //Daylight
155, 50, 50, 200, //Daylight
175, 255,80,3, //Aerospace International Orange
255, 253,119,2, //Heat Wave Orange
};

DEFINE_GRADIENT_PALETTE (Evening_To_Night_gp) { // DAY TO NIGHT PALETTE (eveningNightCycle)
0, 255,142,0, //Dark Orange
36, 0,69,84, //Midnight Green
85, 34,34,34, //Raisin Black
167, 45,45,45, //Charleston Green
255, 0,63,125, //Dark Cerulean Blue

};

CRGBPalette16 myPal1 = Night_To_Morning_gp; // CREATING PALETTE OBJECTS AS MANY AS THERE ARE PALETTES IN USE
CRGBPalette16 myPal2 = Morning_To_Noon_gp;
CRGBPalette16 myPal3 = Noon_To_Evening_gp;
CRGBPalette16 myPal4 = Evening_To_Night_gp;

struct CRGB leds[NUM_LEDS]; // DEFINE OBJECT OF LEDS
static uint8_t paletteIndex = 0; // CURRENT GRADIENT PALETTE COLOUR

//——————————
//————SETUP————-

void setup() {
Serial.begin(9600);
FastLED.addLeds<WS2811, DATA_PIN, BRG>(leds, NUM_LEDS); / BRG SEQUENCE IN PLACE OF RGB FOR MY STRIP

pinMode(STREET_LED, OUTPUT);
lcd.init(); // initialize the lcd
lcd.backlight(); // turn on the backlight
lcd.begin(20,4);
lcd.clear();
lcd.setCursor(0, 0);
}

//—————————–
//————LOOP————-

void loop() {

eveningNightCycle();
nightDayCycle();
dayNoonCycle();
noonEveningCycle();

FastLED.show();

currentMillis = millis(); // capture the latest value of millis()
// this is equivalent to noting the time from a clock
// use the same time for all LED flashes to keep them synchronized
if(millis() >= time_Now + timeBaseInterval)
{
time_Now += timeBaseInterval;
streetLightFading();
}
}

//——————————
//—–DAY TO NIGHT CYCLES——

void nightDayCycle()
{
CRGB colour = ColorFromPalette(myPal1, paletteIndex, maxBrightness, LINEARBLEND); / OR EVENTUALLY A BUILT-IN PALETTE
fill_solid(leds, NUM_LEDS, colour); // LIGHT THE WHOLE STRIP WITH THE COLOUR GRABBED FROM THE PALETTE

lcd.setCursor(0, 0);
lcd.print (“Night to Morning”);

EVERY_N_MILLISECONDS(intervalNightToMorning) { // TRAVERSE THE COMPLETE PALETTE
if (paletteIndex < 255) { // see https://github.com/FastLED/FastLED/issues/515#issuecomment-340627525
paletteIndex++;
}
if (paletteIndex == 255) {
paletteIndex = 0;
}
}
Serial.println(colour); // MAY BE REMOVED AFTER TESTING
}

void dayNoonCycle()
{
CRGB colour = ColorFromPalette(myPal2, paletteIndex, maxBrightness, LINEARBLEND);
fill_solid(leds, NUM_LEDS, colour);

lcd.setCursor(0, 1);
lcd.print (“Morning to Noon”);

EVERY_N_MILLISECONDS(intervalMorningToNoon) {
if (paletteIndex < 255) {
paletteIndex++;
}
if (paletteIndex == 255) {
paletteIndex = 0;
}
}
Serial.println(colour); // MAY BE REMOVED AFTER TESTING
}

void noonEveningCycle()
{
CRGB colour = ColorFromPalette(myPal3, paletteIndex, maxBrightness, LINEARBLEND);
fill_solid(leds, NUM_LEDS, colour);

lcd.setCursor(0, 2);
lcd.print (“Noon to Evening”);

EVERY_N_MILLISECONDS(intervalNoonToEvening) {
if (paletteIndex < 255) {
paletteIndex++;
}
if (paletteIndex == 255) {
paletteIndex = 0;
}
}
Serial.println(colour); / MAY BE REMOVED AFTER TESTING
}

void eveningNightCycle()
{
CRGB colour = ColorFromPalette(myPal4, paletteIndex, maxBrightness, LINEARBLEND);
fill_solid(leds, NUM_LEDS, colour);

lcd.setCursor(0, 3);
lcd.print (“Evening to Night”);

EVERY_N_MILLISECONDS(intervalEveningToNight) {
if (paletteIndex < 255) {
paletteIndex++;
}
if (paletteIndex == 255) {
paletteIndex = 0;
}
}
Serial.println(colour); // MAY BE REMOVED AFTER TESTING
}

//——————————
//——–FADING LIGHTS———

void streetLightFading()
{
if (currentMillis – previousMillis >= timeBaseInterval)
analogWrite(STREET_LED, brightness_STREET);
brightness_STREET = brightness_STREET + fadeAmount;
if (brightness_STREET <= 0 || brightness_STREET >= 255) {
fadeAmount = -fadeAmount ;
}

delayMicroseconds(timeBaseInterval); waits for “timeBaseInterval” to see the fading effect
// As of Arduino 0018, delayMicroseconds() no longer disables interrupts.

}

 

Wiring

T

Tell others about this information by using one of these services. Thank you for the support.
Scroll to top