Hi
I’d like to simulate the colours, brightness and movement of sunrise-to-daytime-to-sunset along a strip of 60 rgb leds (SK6812). Taking 3 minutes to complete the cycle. Using an Arduino Uno.
What i want to be able to write:
All leds are off
On a button push start the program
Led 0 is a dark blue colour and very dim
Wait 2250ms
Led 1 is a lighter blue colour and very dim
Led 0 is the same lighter blue colour and less dim
Wait 2250ms
Led 2 is a purple colour and very dim
Led 1 is the same purple colour and less dim
Led 0 is the same purple colour and a little brighter
Wait 2250ms
And so on, so that the effect is of a light, which increases in brightness and changes colour through blue/purple/red/orange to yellow as it moves from one side of the strip to the centre.
Then, as the brightest light moves towards the end of the strip, the colours are reversed through the cycle and the light fades.
I’ve worked through all the examples in the FASTled and NEOpixel libraries, and I’ve found many similar but not quite what I’m after projects (esp. for fish tanks). I’ve played around, changed colours, dimmed/faded, changed direction etc. I’ve also used some code posted by Grumpy Mike which slows down the speed of pixel dimming and a Larson effect sketch. All the elements are there (I think) - but I haven’t been able to figure out how to put them together.
I’d really like someone to say either - yep you’re going in the right direction, or woah you need to understand xxx before you do anything else.
Posting the code for 2 sketches that are good examples of what I would like to combine, or, if they aren’t compatible, I’d like to understand what i need to know to adapt them to be compatible. Eg, sketch 1 uses CIE for dimming, but sketch 2 uses a fade function.
(sketch 1 is from a forum post here: Adafruit customer service forums • View topic - Rolling Fade Single Color Neopixels and sketch 2 from here: https://forum.arduino.cc/index.php?topic=662473.0 - I’ve adapted both a little so far to suit my needs)
Thanks.
Sketch 1
// Heartbeat lookup table using gamma correction
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define NUMPIXELS 60
#define NUMWAVES 1 // Adjust for number of waves
#define WAIT 200 // Wave speed
#define MAX 255 // Max brightness Range 0 .. 255
#define PIN 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN);
uint8_t CIEL8[] = {
0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 2, 2, 3, 3, 4, 5,
6, 7, 8, 10, 11, 13, 14, 16,
18, 20, 22, 25, 27, 30, 33, 36,
39, 43, 47, 50, 55, 59, 63, 68,
73, 78, 83, 89, 95,101,107,114,
120,127,135,142,150,158,167,175,
184,193,203,213,223,233,244,255,
255,244,233,223,213,203,193,184,
175,167,158,150,142,135,127,120,
114,107,101, 95, 89, 83, 78, 73,
68, 63, 59, 55, 50, 47, 43, 39,
36, 33, 30, 27, 25, 22, 20, 18,
16, 14, 13, 11, 10, 8, 7, 6,
5, 4, 3, 3, 2, 2, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0 };
typedef unsigned short tick_t;
tick_t _previousTick = millis();
tick_t currentTick;
int16_t _fade = 0;
void setup() {
strip.begin();
strip.show();
// strip.setBrightness(MAX);
}
void loop() {
currentTick = millis();
if ( currentTick - _previousTick >= WAIT )
{
_fade = (_fade + 1) % 128;
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor( i, strip.Color(0, CIEL8[(_fade+i*(128*NUMWAVES/strip.numPixels()))%128], 0) );
}
strip.show();
_previousTick = currentTick;
}
}
Sketch 2
// StrandTest from AdaFruit implemented as a state machine
// pattern change by push button
// By Mike Cook Jan 2016
// Fade function added Sept 2017
#define PINforControl 6 // pin connected to the small NeoPixels strip
#define NUMPIXELS1 60 // number of LEDs on strip
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS1, PINforControl, NEO_GRB + NEO_KHZ800);
unsigned long patternInterval = 20 ; // time between steps in the pattern
unsigned long lastUpdate = 0 ; // for millis() when last update occurred
unsigned long intervals [] = { 20} ; // speed for each pattern - add here when adding more cases , 50, 100, 30
int fadeStep = 0; // state variable for fade function
int numberOfCases = 0; // how many case statements or patterns you have
const byte button = 2; // pin to connect button switch to between pin and ground
void setup() {
strip.begin(); // This initialises the NeoPixel library.
wipe(); // wipes the LED buffers
pinMode(button, INPUT_PULLUP); // change pattern button
}
int pos = 0, dir = 1; // Position, direction of "eye"
void loop() {
static int pattern =0, lastReading; // start with the fade function
int reading = digitalRead(button);
if(lastReading == HIGH && reading == LOW){
pattern++ ; // change pattern number
fadeStep = 0; // reset the fade state variable
if(pattern > numberOfCases) pattern = 0; // wrap round if too big
patternInterval = intervals[pattern]; // set speed for this pattern
wipe(); // clear out the buffer
delay(50); // debounce delay
}
lastReading = reading; // save for next time
if(millis() - lastUpdate > patternInterval) updatePattern(pattern);
}
void updatePattern(int pat){ // call the pattern currently being created
switch(pat) {
case 0:
fade(0,255, 0,0, 26,0, 4000); // fade from blue to red. the last number is time to fade
break;
}
}
void fade(int redStartValue, int redEndValue, int greenStartValue, int greenEndValue, int blueStartValue, int blueEndValue, int totalSteps) {
static float redIncrement, greenIncrement, blueIncrement;
static float red, green, blue;
static boolean fadeUp = false;
if (fadeStep == 0){ // first step is to initialise the initial colour and increments
red = redStartValue;
green = greenStartValue;
blue = blueStartValue;
fadeUp = false;
redIncrement = (float)(redEndValue - redStartValue) / (float)totalSteps;
greenIncrement = (float)(greenEndValue - greenStartValue) / (float)totalSteps;
blueIncrement = (float)(blueEndValue - blueStartValue) / (float)totalSteps;
fadeStep = 1; // next time the function is called start the fade
}
else { // all other steps make a new colour and display it
// make new colour
red += redIncrement;
green += greenIncrement;
blue += blueIncrement;
// set up the pixel buffer
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color((int)red,(int)green,(int)blue));
}
// now display it
strip.show();
fadeStep += 1; // go on to next step
if(fadeStep >= totalSteps) { // finished fade
if(fadeUp){ // finished fade up and back
fadeStep = 0;
return; // so next call re-calabrates the increments
}
// now fade back
fadeUp = true;
//redIncrement = -redIncrement;
//greenIncrement = -greenIncrement;
//blueIncrement = -blueIncrement;
//fadeStep = 1; // don't calculate the increments again but start at first change
}
}
}
void wipe(){ // clear all LEDs
for(int i=0;i<strip.numPixels();i++){
strip.setPixelColor(i, strip.Color(0,0,0));
}
}
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}