Skip to content

Instantly share code, notes, and snippets.

@matzipan
Created January 19, 2015 17:41
Show Gist options
  • Save matzipan/25d1d22ca7150217aa43 to your computer and use it in GitHub Desktop.
Save matzipan/25d1d22ca7150217aa43 to your computer and use it in GitHub Desktop.
Arduino Christmas rainbow lights
#include "FastLED.h"
#define DATA_PIN 3
#define CLOCK_PIN 13
#define kMatrixWidth 150
#define kMatrixHeight 1
#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
CRGB leds[kMatrixWidth * kMatrixHeight];
int ledPeriod;
int ledIntensity;
float blinkyBlinkyOffset[kMatrixWidth * kMatrixHeight];
int blinkyBlinkyShineAddress[20];
int blinkyBlinkyShineLeft[20];
int blinkyBlinkyShineInitial[20];
int blinkyBlinkyCount;
int blinkyBlinkyPeriod;
int blinkyBlinkyShines = 5;
int blinkyBlinkyShineIntensity = 100;
void setup() {
Serial.begin(9600);
while(!Serial){}
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
blinkyBlinkyConfigWave();
}
int blinkyBlinkyGetValue(int id, int count, int indexOffset, int lSV, int lST) {
int componentOffset;
switch(indexOffset) {
case 0:
componentOffset = 0;
break;
case 1:
componentOffset = 2.09;
break;
case 2:
componentOffset = 4.18;
break;
}
int componentValue = (sin((count%ledPeriod/(float)ledPeriod)*2*3.14+componentOffset+blinkyBlinkyOffset[id])+1)/2*ledIntensity;
return lSV/(float)lST*blinkyBlinkyShineIntensity + (lST-lSV)/(float)lST* componentValue;
}
void blinkyBlinkyConfigLed(int id) {
int lSV = 0;
int lST = 1;
for(int i =0; i<blinkyBlinkyShines; i++) {
if(blinkyBlinkyShineAddress[i] > id)
break;
if(blinkyBlinkyShineAddress[i]==id) {
lSV = blinkyBlinkyShineLeft[i];
lST = blinkyBlinkyShineInitial[i];
blinkyBlinkyShineLeft[i]--;
break;
}
}
leds[id] = CRGB(blinkyBlinkyGetValue(id, blinkyBlinkyCount, 0, lSV, lST), blinkyBlinkyGetValue(id, blinkyBlinkyCount,1, lSV, lST), blinkyBlinkyGetValue(id, blinkyBlinkyCount, 2, lSV, lST));
}
void blinkyBlinkyConfigWave() {
blinkyBlinkyCount = 0;
blinkyBlinkyPeriod = 5000;
ledIntensity = 30;
ledPeriod = 10;
for (int i = 0; i < kMatrixWidth; i++) {
for (int j = 0; j < kMatrixHeight; j++) {
blinkyBlinkyOffset[(j*kMatrixWidth) + i] = (j*kMatrixWidth + i)/(float)(kMatrixHeight*kMatrixWidth)*3.14*30;
}
}
int intervalCount = NUM_LEDS/blinkyBlinkyShines;
for(int i =0; i< blinkyBlinkyShines; i++) {
blinkyBlinkyShineAddress[i] = i*intervalCount+ random(intervalCount);
blinkyBlinkyShineLeft[i]= 3+random(10);
blinkyBlinkyShineInitial[i] = blinkyBlinkyShineLeft[i];
}
}
void loop() {
blinkyBlinkyCount += 1;
if(blinkyBlinkyCount == blinkyBlinkyPeriod) {
blinkyBlinkyCount = 0;
}
for (int i = 0; i < kMatrixWidth; i++) {
for (int j = 0; j < kMatrixHeight; j++) {
blinkyBlinkyConfigLed((j*kMatrixWidth) + i);
}
}
int intervalCount = NUM_LEDS/blinkyBlinkyShines;
for(int i =0; i< blinkyBlinkyShines; i++) {
if(blinkyBlinkyShineLeft[i] == 0) {
blinkyBlinkyShineAddress[i] = i*intervalCount+ random(intervalCount);
blinkyBlinkyShineLeft[i]= 3+random(10);
blinkyBlinkyShineInitial[i] = blinkyBlinkyShineLeft[i];
}
}
FastLED.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment