Sunrise - Sunset

Hello,

I am busy to make a sunrise and sunset for my aquarium with Arduino uno R3.
My knowledge of the programming language is not very great. I search a lot on the internet.
I have an external time switch.
When the Arduino turns on, it goes from red slowly to white. So far so good.
I can adjust the sunrise time.

If it is completely white (end of sunrise) it must remain white.
But now he goes out and starts all over again, from red to white and that is not the intention.
The light must remain white.
I can’t get this done to leave it white.

If it is white for a few hours, then after a contact of my time switch must start sunset and go from white to red (sunset).
When the lights are off, they should not be on until my timer switches the system off. The same again the next day, ect, ect.

I read on the internet that HeatColors_p goes from red to white by default. True?
The colors are good.

I hope everyone understands what I mean and I hope someone has a solution.

Greeting,

Rob

Here is my code till sofar.

#include <Adafruit_NeoPixel.h>
#include “FastLED.h”
int buttonPin = 2; // momentary push button on pin 0
int oldButtonVal = 0;
#define pixels 40
#define PIN 6 // Parameter 1 = number of pixels in strip

/*DEFINE_GRADIENT_PALETTE( Sunup ) {
0, 0, 0, 0, //black
127, 255, 0, 0, //red
224, 255, 255, 0, //bright yellow
255, 255, 255, 255
}; //full white

DEFINE_GRADIENT_PALETTE( Sundown ) {
255, 255, 255, 255,
224, 255, 255, 0,
127, 255, 0, 0,
0, 0, 0, 0
};*/

CRGB leds[pixels];
Adafruit_NeoPixel strip = Adafruit_NeoPixel(pixels, PIN, NEO_GRB + NEO_KHZ800);

int nPatterns = 2;
int lightPattern = 1;
uint16_t currentPixel = 0;// what pixel are we operating on
uint16_t led = 0;

unsigned long previousmillis = 0;
unsigned long time = 50;
// the setup routine runs once when you press reset:
void setup() {
FastLED.addLeds<NEOPIXEL, PIN>(leds, pixels);
strip.begin();
strip.show(); // initialize all pixels to ‘off’
currentPixel = 0; // initialize the BUTTON pin as an input
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH); // button pin is HIGH, so it drops to 0 if pressed
}

void loop() {
// read that state of the pushbutton value;
int buttonVal = digitalRead(buttonPin);
if (buttonVal == LOW && oldButtonVal == HIGH) {// button has just been pressed
lightPattern = lightPattern + 1;
}
if (lightPattern > nPatterns) lightPattern = 1;
oldButtonVal = buttonVal;

switch (lightPattern) {
case 1:
if ((unsigned long)(millis() - previousmillis) >= time) {
previousmillis = millis();
Sunrise();
break;
case 2:
if ((unsigned long)(millis() - previousmillis) >= time)
previousmillis = millis();
Sunset();
break;
}
}
}

//Sunrise
void Sunrise() {
static const uint8_t sunriseLength = 30; // total sunrise length, in minutes
//7 seconds x 256 gradient steps = 1,792 seconds = ~30 minutes
static const uint8_t interval = (sunriseLength * 10) / 256; //De duur van het in faden
static uint8_t heatIndex = 0; // start out at 0 // current gradient palette color index
CRGB color = ColorFromPalette(HeatColors_p, heatIndex );
fill_solid(leds, pixels, color); // fill the entire strip with the current color

EVERY_N_SECONDS(interval) {
if (heatIndex < 240) {
heatIndex++;
}
FastLED.show();
}
}

//Sunset
void Sunset() {
static const uint8_t sunsetLength = 30; // total sunset length, in minutes
//7 seconds x 256 gradient steps = 1,792 seconds = ~30 minutes
static const uint8_t interval1 = (sunsetLength * 10) / 256; //De duur van het in faden
static uint8_t heatIndex1 = 200; // start out at 0 // current gradient palette color index
CRGB color1 = ColorFromPalette(HeatColors_p, heatIndex1 );
fill_solid(leds, pixels, color1); // fill the entire strip with the current color

EVERY_N_SECONDS(interval1) {
if (heatIndex1 > 0) {
heatIndex1–;
}
FastLED.show();
}
}

check the braces [{}] in the switch statement of loop ()

Please edit your post to add code tags, as described in “How to use this forum”.

What are using as your timing? When does sunrise and sunset start?

Hello, I use an external clock.
This switches on, for example, at 10.00, then the lighting comes on.
A second contact leaves the sunset at 9 p.m. and after 30 minutes everything goes out.
thanks for your response, but i think i already have a solution.

but any ide is welcome.

rob

HnkRob:
but any ide is welcome.

To make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to my text editor.

...R

Hello,

I am busy to make a sunrise and sunset for my aquarium with Arduino uno R3.
My knowledge of the programming language is not very great. I search a lot on the internet.
I have an external time switch.
When the Arduino turns on, it goes from red slowly to white. So far so good.
I can adjust the sunrise time.

If it is completely white (end of sunrise) it must remain white.
But now he goes out and starts all over again, from red to white and that is not the intention.
The light must remain white.
I can't get this done to leave it white.

If it is white for a few hours, then after a contact of my time switch must start sunset and go from white to red (sunset).
When the lights are off, they should not be on until my timer switches the system off. The same again the next day, ect, ect.

I read on the internet that HeatColors_p goes from red to white by default. True?
The colors are good.

I hope everyone understands what I mean and I hope someone has a solution.

Greeting,

Rob


Here is my code till sofar.

#include <Adafruit_NeoPixel.h>
#include "FastLED.h"
int buttonPin = 2; // momentary push button on pin 0
int oldButtonVal = 0;
#define pixels 40
#define PIN 6 // Parameter 1 = number of pixels in strip

/*DEFINE_GRADIENT_PALETTE( Sunup ) {
  0, 0, 0, 0, //black
  127, 255, 0, 0, //red
  224, 255, 255, 0, //bright yellow
  255, 255, 255, 255
}; //full white

DEFINE_GRADIENT_PALETTE( Sundown ) {
  255, 255, 255, 255,
  224, 255, 255, 0,
  127, 255, 0, 0,
  0, 0, 0, 0
};*/

CRGB leds[pixels];
Adafruit_NeoPixel strip = Adafruit_NeoPixel(pixels, PIN, NEO_GRB + NEO_KHZ800);

int nPatterns = 2;
int lightPattern = 1;
uint16_t currentPixel = 0;// what pixel are we operating on
uint16_t led = 0;

unsigned long previousmillis = 0;
unsigned long time = 50;
// the setup routine runs once when you press reset:
void setup() {
  FastLED.addLeds<NEOPIXEL, PIN>(leds, pixels);
  strip.begin();
  strip.show(); // initialize all pixels to 'off'
  currentPixel = 0; // initialize the BUTTON pin as an input
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH); // button pin is HIGH, so it drops to 0 if pressed
}

void loop() {
  // read that state of the pushbutton value;
  int buttonVal = digitalRead(buttonPin);
  if (buttonVal == LOW && oldButtonVal == HIGH) {// button has just been pressed
    lightPattern = lightPattern + 1;
  }
  if (lightPattern > nPatterns) lightPattern = 1;
  oldButtonVal = buttonVal;

  switch (lightPattern) {
    case 1:
      if ((unsigned long)(millis() - previousmillis) >= time) {
        previousmillis = millis();
        Sunrise();
        break;
    case 2:
      if ((unsigned long)(millis() - previousmillis) >= time)
        previousmillis = millis();
        Sunset();
        break;
      }
  }
}

//Sunrise
void Sunrise() {
  static const uint8_t sunriseLength = 30; // total sunrise length, in minutes
  //7 seconds x 256 gradient steps = 1,792 seconds = ~30 minutes
  static const uint8_t interval = (sunriseLength * 10) / 256; //De duur van het in faden
  static uint8_t heatIndex = 0; // start out at 0 // current gradient palette color index
  CRGB color = ColorFromPalette(HeatColors_p, heatIndex );
  fill_solid(leds, pixels, color); // fill the entire strip with the current color

  EVERY_N_SECONDS(interval) {
    if (heatIndex < 240) {
      heatIndex++;
    }
    FastLED.show();
  }
}

//Sunset
void Sunset() {
  static const uint8_t sunsetLength = 30; // total sunset length, in minutes
  //7 seconds x 256 gradient steps = 1,792 seconds = ~30 minutes
  static const uint8_t interval1 = (sunsetLength * 10) / 256; //De duur van het in faden
  static uint8_t heatIndex1 = 200; // start out at 0 // current gradient palette color index
  CRGB color1 = ColorFromPalette(HeatColors_p, heatIndex1 );
  fill_solid(leds, pixels, color1); // fill the entire strip with the current color

  EVERY_N_SECONDS(interval1) {
    if (heatIndex1 > 0) {
      heatIndex1--;
    }
    FastLED.show();
  }
}

You should just use the code button for your code, not for your comments.

You don't seem to have any Serial.print() statements in your program so that you can see if it is doing what you think it is doing. For example, what actual values does lightPattern have?

Maybe you should have two extra cases for when it is day and when it is night. For example at the end of Sunrise() set lightPattern to 'D'

And IMHO the code will be more obvious if you use letters for the states of the system 'S' for sunrise, 'D for day, 'E' for evening (sunset) and 'N' for night.

I would put the timing code into the Sunrise() function rather than in the CASE code - just makes things neater. Then you could have

switch(lightPattern) {
   case 'N':
       nightTime();
       break;
   case 'S':
      Sunrise();
      break;
   case 'D':
      dayTime();
      break;
   case 'E':
      Sunset();
      break;
}

...R

I have already discovered how the monitor works. It makes it a lot easier. I'm going to try this.

Thanks.

Hi, Robin

Can you tell me why case ("a") is used and not case (1). What is the difference and how does it work? I dont understand that.

Rob

HnkRob:
Can you tell me why case ("a") is used and not case (1). What is the difference and how does it work? I dont understand that.

I did not suggest 'a', I suggested S D E and N as abbreviations that make the code easier to understand IMHO. The compiler won't care whether you use numbers or characters, but code should be written to be easily understood by humans.

Note also that it should be case('a'). Single quotes for single characters and double quotes for a null-terminated cstring.

...R

I made it that way now. Daylight stays on for a long time at the end of sunrise (delay). Then through the external switch (case 2) it goes to sunset (). then the daylight can stay on for a long time (delay). Together that should take approximately 7 hours. The switch is not yet completely tasty.

void loop() {
  // read that state of the pushbutton value;
  int buttonVal = digitalRead(buttonPin);
  if (buttonVal == LOW && oldButtonVal == HIGH) {// button has just been pressed
    lightPattern = lightPattern + 1;
  }
  if (lightPattern > nPatterns) lightPattern = 1;
  oldButtonVal = buttonVal;

  switch (lightPattern) {
    case 1:
      if ((unsigned long)(millis() - previousmillis) >= time) {
        previousmillis = millis();
        Sunrise();
        break;
      case 2:
        if ((unsigned long)(millis() - previousmillis) >= time) {
          previousmillis = millis();
          Sunset();
          break;
        }
      }
  }
}

//Sunrise
void Sunrise() {
  static const uint8_t sunriseLength = 30; // total sunrise length, in minutes
  //7 seconds x 256 gradient steps = 1,792 seconds = ~30 minutes
  static const uint8_t interval = (sunriseLength * 10) / 256; //De duur van het in faden
  static uint8_t heatIndex = 0; // start out at 0 // current gradient palette color index
  CRGB color = ColorFromPalette(HeatColors_p, heatIndex );
  fill_solid(leds, pixels, color); // fill the entire strip with the current color

  EVERY_N_SECONDS(interval) {
    if (heatIndex < 100) {
      Serial.println(heatIndex);
      heatIndex++;

    if (heatIndex == 99) {
        Serial.println("heatIndex = Daglicht voor 10000");
        delay(1000);
      }
    }
    FastLED.show();
  }
}

//Sunset
void Sunset() {
  static const uint8_t sunsetLength = 30; // total sunset length, in minutes
  //7 seconds x 256 gradient steps = 1,792 seconds = ~30 minutes
  static const uint8_t interval1 = (sunsetLength * 10) / 256; //De duur van het in faden
  static uint8_t heatIndex1 = 200; // start out at 0 // current gradient palette color index
  CRGB color1 = ColorFromPalette(HeatColors_p, heatIndex1 );
  fill_solid(leds, pixels, color1); // fill the entire strip with the current color

  EVERY_N_SECONDS(interval1) {
    if (heatIndex1 > 0) {
      Serial.println(heatIndex1);
      heatIndex1--;

      if (heatIndex1 > 198) {
        Serial.println("heatIndex1 = 200");
        delay(10000);
      }
     }
    FastLED.show();
  }
}