So I would like to expose a small but annoying problem that I have with a sketch.
I’m in HO model trains and I would like to combine a Sunset-Sunrise cycle with some street lights.
Therefore I’m using a Mega with a Robotdyn sensor shield, led strips of the type 2811 (for the moment only 12ea) and a single white led
The Sunset-Sunrise is performed by RGB led strips and the street lighting is for the white led.
The sketch is a mix of several items written by other authors and reassembled together by me. All the palettes are self-designed and should give a rather convincing day cycle, divided in 4 parts (and thus in 4 palettes). The reason for this is that I want to be able to lengthen or shorten each part of the cycle as needed.
While the the day cycle goes on and reaches dusk, the street lighting should remain Off. When dusk starts it should gradually go On to a maximum brightness. The latter one should occure around ‘midnight’.
So far the Sunset-Sunrise perform rather well (except some flickering probably due to a weak power feeding). Also, the street lighting performs well from nothing to maximum brightness and verso.
What I can’t achieve is to start (synchronise) the street lighting with the led strip(s)a precisely the “dusk” moment.
So is this possible?
If so, then how?
Thank you in advance.
This is the code:
[code]
/* Sunset & Sunrise Ver 2.0
The sketch is a mix of several items written by other authors and reassembled together by me.
All the palettes are self-designed and should give a rather convincing day cycle, divided in 4 parts (and thus in 4 palettes).
The reason for this is that I want to be able to lengthen or shorten each part of the cycle as needed.
While the the day cycle goes on and reaches dusk, the street lighting should remain Off.
When dusk starts it should gradually go On to a maximum brightness.
The latter one should occure around ‘midnight’.
*/
#define FASTLED_INTERNAL
#include <FastLED.h>
#include <millisDelay.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // SET THE LCD ADDRESS TO 0x27 FOR A 16 CHARS AND 4 LINE DISPLAY - SDA = PIN 21 & SCL = PIN 21
//-----------------------------
//-----------TIMING------------
unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
unsigned long speedOfNightToMorning = 1; // DURATION MINUTES nightDayCycle ROUTINE (10 minutes: one step = 2343.75 milliseconds ~ 2.3 seconds)
unsigned long speedOfMorningToNoon = 2; // DURATION MINUTES dayNoonCycle ROUTINE (10 minutes: one step = 2343.75 milliseconds ~ 2.3 seconds)
unsigned long speedOfNoonToEvening = 2; // DURATION MINUTES noonEveningCycle ROUTINE (10 minutes: one step = 2343.75 milliseconds ~ 2.3 seconds)
unsigned long speedOfEveningToNight = 1; // DURATION MINUTES eveningNightCycleROUTINE (10 minutes: one step = 2343.75 milliseconds ~ 2.3 seconds)
unsigned long timeBaseInterval = speedOfEveningToNight + speedOfNightToMorning; // TIMEBASE FOR THE EVENING + NIGHT CYCLE
unsigned long time_Now = 0; // USED FOR THE STREETLIGHT ROUTINE
unsigned long interval_STREET = 1000; // INTERVAL BETWEEN UP CYCLE & DOWN CYCLE (milliseconds) MUST BE EQUAL TO THE SUM OF BOTH "NIGHT" CYCLES
int STREET_LED = 44; // THE PIN THAT THE STREET LEDs ARE ATTACHED TO (Uno = 3,5,6,9,10 & 11 - Mega = 2->13 & 44 -> 46)
int minBrightness_STREET = 0; // BRIGHTNESS OF THJE STREET LEDs AT STARTUP
int maxBrightness_STREET = 128; // BRIGHTNESS OF THJE STREET LEDs AT END OF ROUTINE
int fadeAmount = 1; // KEEP THIS AMOUNT UNDER 5 FOR A NICE FADING OF THE STREET LEDs - 1 = BEST RESULTS
int i = 0; // LED INTENSITY
int dir = 1; // DIRECTION 1 = UP & 0 = DOWN
bool ledOn_STREET = false; // KEEP TRACK OF THE STREET_LED STATUS
millisDelay ledDelay_STREET; // DELAY AGAINST WHICH TO DRIFT
const byte DATA_PIN = 6;
const byte NUM_LEDS = 12;
const byte maxBrightnessStrip = 128; // BETWEEN 0 & 255
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;
//------------------------------
//------------SETUP-------------
void setup()
{
Serial.begin(9600);
FastLED.addLeds<WS2811, DATA_PIN, BRG>(leds, NUM_LEDS);
pinMode(STREET_LED, OUTPUT);
analogWrite(STREET_LED, LOW); // TURN STREET_LED OFF
ledOn_STREET = false; // DELAY AGAINST WHICH TO DRIFT
ledDelay_STREET.start(interval_STREET); // START THE DELAY
lcd.init(); //INITIALIZE THE LCD
lcd.backlight(); //OPEN THE LCD BACKLIGHT
lcd.print("Sunset_To_Sunrise_Loop_TEST-Ver2.ino");
}
//-----------------------------
//------------LOOP-------------
void loop()
{
currentMillis = millis(); // CAPTURE THE LATEST VALUE OF millis() IS EQUIVALENT TO NOTING THE TIME FROM A CLOCK
if(millis() >= time_Now + timeBaseInterval){
time_Now += timeBaseInterval;
streetLightFading();
}
eveningNightCycle();
nightDayCycle();
dayNoonCycle();
noonEveningCycle();
FastLED.show();
}
//--------------------------------
//-------4 CYCLES OF 1 DAY--------
void nightDayCycle()
{
//static const float transitionDuration1 = 2; // Minutes (10 minutes: one step = 2343.75 milliseconds ~ 2.3 seconds)
static const float interval = ((float)(speedOfNightToMorning * 60) / 256) * 1000; // STEPS IN milliseconds
static uint8_t paletteIndex = 0; // CURRENT GRADIENT PALETTE COLOUR
CRGB colour = ColorFromPalette(myPal1, paletteIndex, maxBrightnessStrip, LINEARBLEND);
fill_solid(leds, NUM_LEDS, colour); // ILLUMINATE THE WHOLE STRIP WITH THE COLOUR FETCHED FROM THE PALETTE
EVERY_N_MILLISECONDS(interval) { // TRAVERSE THE COMPLETE PALETTE
if (paletteIndex < 255) {
paletteIndex++;
}
if (paletteIndex == 255) {
paletteIndex = 0;
}
}
} // End of nightDayCycle
void dayNoonCycle()
{
//static const float transitionDuration2 = 2; // Minutes (10 minutes: one step = 2343.75 milliseconds ~ 2.3 seconds)
static const float interval = ((float)(speedOfMorningToNoon * 60) / 256) * 1000; // STEPS IN milliseconds
static uint8_t paletteIndex = 0; // CURRENT GRADIENT PALETTE COLOUR
CRGB colour = ColorFromPalette(myPal2, paletteIndex, maxBrightnessStrip, LINEARBLEND);
fill_solid(leds, NUM_LEDS, colour); // ILLUMINATE THE WHOLE STRIP WITH THE COLOUR FETCHED FROM THE PALETTE
EVERY_N_MILLISECONDS(interval) { // TRAVERSE THE COMPLETE PALETTE
if (paletteIndex < 255) {
paletteIndex++;
}
if (paletteIndex == 255) {
paletteIndex = 0;
}
}
} // End of dayNoonCycle
void noonEveningCycle()
{
//static const float transitionDuration3 = 2; // Minutes (10 minutes: one step = 2343.75 milliseconds ~ 2.3 seconds)
static const float interval = ((float)(speedOfNoonToEvening * 60) / 256) * 1000; // STEPS IN milliseconds
static uint8_t paletteIndex = 0; // CURRENT GRADIENT PALETTE COLOUR
CRGB colour = ColorFromPalette(myPal3, paletteIndex, maxBrightnessStrip, LINEARBLEND);
fill_solid(leds, NUM_LEDS, colour); // ILLUMINATE THE WHOLE STRIP WITH THE COLOUR FETCHED FROM THE PALETTE
EVERY_N_MILLISECONDS(interval) { // TRAVERSE THE COMPLETE PALETTE
if (paletteIndex < 255) {
paletteIndex++;
}
if (paletteIndex == 255) {
paletteIndex = 0;
}
}
} // End of noonEveningCycle
void eveningNightCycle()
{
//static const float transitionDuration4 = 2; // Minutes (10 minutes: one step = 2343.75 milliseconds ~ 2.3 seconds)
static const float interval = ((float)(speedOfEveningToNight * 60) / 256) * 1000; // STEPS IN milliseconds
static uint8_t paletteIndex = 0; // CURRENT GRADIENT PALETTE COLOUR
CRGB colour = ColorFromPalette(myPal4, paletteIndex, maxBrightnessStrip, LINEARBLEND);
fill_solid(leds, NUM_LEDS, colour); // ILLUMINATE THE WHOLE STRIP WITH THE COLOUR FETCHED FROM THE PALETTE
EVERY_N_MILLISECONDS(interval) { // TRAVERSE THE COMPLETE PALETTE
if (paletteIndex < 255) {
paletteIndex++;
}
if (paletteIndex == 255) {
paletteIndex = 0;
}
}
} // End of eveningNightCycle
//-------------------------------------
//--------FADING STREET LIGHTS---------
void streetLightFading()
{
unsigned long currentMillis = millis();
Serial.println(i); // ONLY FOR TEST PURPOSES
if(i <= minBrightness_STREET) {
dir = 1;
}
if(i >= maxBrightness_STREET) {
dir = 0;
// checkToggleLed();
}
if(currentMillis - previousMillis > interval_STREET && dir == 1)
//if(currentMillis - previousMillis > timeBaseInterval && dir == 1)
{
// save the last time you blinked the LED
previousMillis = currentMillis;
i = i + 2;
analogWrite(STREET_LED, i); // set the LED brightness
}
if(currentMillis - previousMillis > interval_STREET && dir == 0)
//if(currentMillis - previousMillis > timeBaseInterval && dir == 0)
{
// save the last time you blinked the LED
previousMillis = currentMillis;
i = i - 2;
analogWrite(STREET_LED, i); // set the LED brightness
}
}
//--------------------------------- ----
//--------MONITOR STREET LIGHTS---------
void checkToggleLed() {
if (ledDelay_STREET.justFinished()) { // CHECK IF THE DELAY HAS TIMED OUT
ledDelay_STREET.repeat(); // REPEAT THE DELAY & TOGGLE STREET_LED
ledOn_STREET = !ledOn_STREET;
if (ledOn_STREET) {
analogWrite(STREET_LED, HIGH);
} else {
analogWrite(STREET_LED, LOW);
}
}
}
[/code]