Hello,
I am experimenting with WS2801 type LED-strips.
Having LEDs lit in specific colors works fine. However, I am stuck in making animations.
I want to light up specific sections (called 'strands' in my code).
Virtually I split the LED strip up to 14 sections of 9 LEDs per section.
I want section 1 (or 0 however you like to call it) to fade on to 100% brightness, with speed which could be defined in a variable. Right now, I use a delay(1), and the fading is to slow now.
Also, I guess I should not use delay. Why? Because I dont want to wait for section one to lit up completely. I want section 2 starting to fade short before section 1 is finished, so I gives an almost smooth transition.
And after all sections have been lit up, wait for 2 full seconds, and start to fade out in the exact same way.
Short: The sections should fade in one section at a time from left to right, and 2 seconds after they all lit up, start fade out from left to right, one section at a time. (With being able to start next section transition while the active one is not completely finished yet)
My actual code:
// Initialize libraries
#include "Adafruit_WS2801.h"
#include "SPI.h"
#define NUM_LEDS 128
#define STRANDS 14
#define DATA_PIN 5
#define CLOCK_PIN 6
// Initialize LED strip
Adafruit_WS2801 strip = Adafruit_WS2801(NUM_LEDS, DATA_PIN, CLOCK_PIN);
// Variables
int ledsPerStrain = 0;
// Initialize
void setup(){
ledsPerStrain = (NUM_LEDS/STRANDS); // Current outcome: 9 LEDs per strand
bootSequence();
}
// Run application
void loop() {
runAnimation(0, 0, 255, 1);
}
void bootSequence(){
strip.begin(); // Initialize strip
strip.show(); // Empty strip
// TODO: Create boot sequence (self-test) animation
}
void runAnimation(uint8_t red, uint8_t green, uint8_t blue, uint8_t wait) {
unsigned long currentMillis = millis();
//
// Turn on all strands
//
for(uint8_t s=0; s < STRANDS; s++){
for(uint8_t b=0; b <255; b++) {
for(uint8_t i=(s*ledsPerStrain); i < ((s*ledsPerStrain)+ledsPerStrain); i++) {
strip.setPixelColor(i, red*b/255, green*b/255, blue*b/255);
}
strip.show();
delay(wait);
}
}
delay(2000); // Wait 2 seconds after everything is lit
//
// Turn off all strands
//
for(uint8_t s=0; s < STRANDS; s++){
for(uint8_t b=0; b <255; b--) {
for(uint8_t i=(s*ledsPerStrain); i < ((s*ledsPerStrain)+ledsPerStrain); i++) {
strip.setPixelColor(i, red*b/255, green*b/255, blue*b/255);
}
strip.show();
delay(wait);
}
}
delay(2000); // Wait 2 seconds after everything went dark
};
// Create a 24 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b){
uint32_t c;
c = r;
c <<= 8;
c |= g;
c <<= 8;
c |= b;
return c;
}