Skip to content

Instantly share code, notes, and snippets.

@niko-h
Created January 12, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niko-h/c8188fd9fe6c6ff883ca to your computer and use it in GitHub Desktop.
Save niko-h/c8188fd9fe6c6ff883ca to your computer and use it in GitHub Desktop.
Small program for Arduino + 32 RGB-LED-Strip
#define DEBUG // Uncomment for Serial Debug Output
#include "HL1606stripPWM.h"
// This library uses Timer 2 for PWM counting and 3 * numLEDs bytes of RAM
// and Arduino pins 11 & 13 (Mega, 51 & 52)
// First value passed is how many LEDs are in your HL1606 strand
// fewer LEDs means less work
// This library uses hardware SPI so pins 11 and 13 are used
// for Data and Clock output (for the Mega, pins 51 and 52)
// The latch pin (L) can be any pin but pin 10 (Arduino) or 53 (Mega)
// MUST BE AN OUTPUT!
int latchPin = 10;
int micPin = 2; //sound-input
int volPin = 1; //input gain poti
int potPin = 0; //poti
float micmax = 1; //
int miccount = 0;
byte r=0;
byte g=0;
byte b=0;
HL1606stripPWM strip = HL1606stripPWM(32, latchPin);
void setup() {
Serial.begin(9600);
// You can customize/control the pulse width modulation and color
// resolution by setting how many bits of PWM you want per LED
// For example, 3 bits is 8 different PWM values per LED and 9 bits, 512
// values for full color. 4 bits is 16 PWM per LED, 12 bit color with
// 4096 different colors available.
// Increasing the PWMbits by 1 means you need *TWICE* as much CPU !!!
// We suggest starting with 3 and tweaking the other variables to get
// the fastest SPI and maximum CPU. Then try upping this to 4. For short
// strips (like 1 meter) that are ok with SPIdiv of 16, you can try 5
strip.setPWMbits(4);
// We use the built-in hardware SPI module. We can change the speed
// of the module to push data out faster. In theory, HL1606's should work with
// the SPI divider set to 16 but we found that this makes some strips
// spaz out. Start with 32 and once it works try reducing it to 16
// If you're lucky, you can even try 8
// Valid divider values are: 2, 4, 8, 16, 32, 64, and 128, dont try others!
strip.setSPIdivider(32);
// all the hard work of running the strip is done in an interrupt
// we can configure the interrupt so that we spend more or less
// time running the strip, letting you do other stuff like sensors
// or an LED or whatever. Set it between 0 and 100, where 100 means
// higher quality colorstrip display but no time for anything else.
strip.setCPUmax(70); // 70% is what we start at
// For Arduinos, pin 10 MUST be an output before you begin!
// (if using a Mega, pin 53 must be an output (instead of 10) )
// We suggest using pin 10 for the latchPin
strip.begin();
// print out what the timing is (for debugging)
double time = strip.numLEDs(); // each LED
time *= 8; // 8 bits of data for each LED;
time *= strip.getSPIdivider(); // larger dividers = more time per bit
time *= 1000; // time in milliseconds
time /= F_CPU; // multiplied by how long it takes for one instruction (nverse of cpu)
Serial.print("Time per LED strip write: ");
Serial.print(time); Serial.println(" millis");
time *= 100;
time /= strip.getCPUmax();
Serial.print("Time allowed per interrupt ");
Serial.print(time);
Serial.println(" millis");
// get that in Hz
time = 1000 / time;
Serial.print("Interrupt freq: ");
Serial.print(time);
Serial.println(" Hz");
// Now find the final 'color cycle' frequency
Serial.print("Color cycle freq: ");
Serial.print(time / (1 << strip.getPWMbits()));
Serial.println(" Hz");
// Try to keep the color frequency above 60 Hz (flickery). 100 Hz or higher looks good to most people
}
int micin;
int pot;
float micgain;
// <<<<<<< Start Loop >>>>>>>
void loop() {
pot = analogRead(potPin);
micgain = analogRead(volPin);
micin = 1+analogRead(micPin);
//if(micin>1) Serial.println(micin);
#ifdef DEBUG
Serial.print('Poti: ')
Serial.println(pot);
#ifdef DEBUG
if(miccount < 1000) {
if(micmax < micin) {
micmax = micin;
}
miccount++;
} else {
miccount = 0;
micmax = (micmax/3)*2;
}
#ifdef DEBUG
Serial.print(micgain);
Serial.print(" | ");
Serial.print(micmax);
Serial.print(" | ");
Serial.print((micmax/1023)*micgain);
Serial.print(" | ");
Serial.println(micin);
#endif
select(pot, micin, micmax, micgain);
}
void select(int pot, int micin, float micmax, float micgain) {
if(pot<=40) {
vu1(micin, micmax, micgain);
} else if(pot>=40 && pot<70) {
strobe1(micin, micmax, micgain);
} else if(pot>=70 && pot<200) {
boolean strobe = true;
ambient();
} else if(pot>=200 && pot<400) {
boolean strobe = false;
kamin(micin, micmax, strobe, micgain);
} else if(pot>=400 && pot<570) {
boolean strobe = true;
kamin(micin, micmax, strobe, micgain);
} else if(pot>=570 && pot<700) {
multicolor(micin, micmax, micgain);
} else if(pot>=700 && pot<900) {
putzlicht();
} else if(pot>=900 && pot<1000) {
//metronome(micin, micmax);
aus1();
} else if(pot>= 1000) {
aus1();
}
}
void aus1() {
for (uint8_t i=0; i< strip.numLEDs() ; i++) {
strip.setLEDcolorPWM(i, (0 & 0x1F) << 3, ((0>>10) & 0x1F) << 3, ((0>>5) & 0x1F) << 3);
}
}
void an(int i){
if(i>=0 && i<=strip.numLEDs()) strip.setLEDcolorPWM(i, (31 & 0x1F) << 3, ((0>>10) & 0x1F) << 3, ((0>>5) & 0x1F) << 3);
}
void aus(int i){
if(i>=0 && i<strip.numLEDs()) strip.setLEDcolorPWM(i, (0 & 0x1F) << 3, ((0>>10) & 0x1F) << 3, ((0>>5) & 0x1F) << 3);
}
void putzlicht() {
for (uint8_t i=0; i< strip.numLEDs() ; i++) {
r=31;
g=r;
b=r;
uint16_t c = Color(r,g,b);
strip.setLEDcolorPWM(i, (c & 0x1F) << 3, ((c>>10) & 0x1F) << 3, ((c>>5) & 0x1F) << 3);
}
}
void multicolor(int micin, float micmax, float micgain) {
r=0;
g=r;
b=r;
if(micin > micgain) {
}
for (uint8_t i=0; i< strip.numLEDs() ; i++) {
r=32;
g=0;
b=0;
if(i>(strip.numLEDs()/2)) {
r=0;
g=0;
b=32;
}
uint16_t c = Color(r,g,b);
strip.setLEDcolorPWM(i, (c & 0x1F) << 3, ((c>>10) & 0x1F) << 3, ((c>>5) & 0x1F) << 3);
delay(1);
}
}
void kamin(int micin, float micmax, boolean strobe, float micgain) {
int j;
r=0;
g=r;
b=r;
if(micin > (micgain && strobe == true)) {
strobe1(micin, micmax, micgain);
}
for (uint8_t i=0; i< strip.numLEDs() ; i++) {
r=21+random(0,10);
g=0;
b=0;
if(j>=4) {
r=31;
g=random(0,5);
j=0;
}
uint16_t c = Color(r,g,b);
strip.setLEDcolorPWM(i, (c & 0x1F) << 3, ((c>>10) & 0x1F) << 3, ((c>>5) & 0x1F) << 3);
delay(1);
}
}
void strobe1(int micin, float micmax, float micgain) {
for (uint8_t i=0; i<strip.numLEDs()/2 ; i++) {
//Serial.println(i);
r = 0;
g = 0;
b = 0;
if((micin) > ((micmax/1024)*micgain)) {
r = 31;
g = 31;
b = 31;
} else {
r = 0;
g = 0;
b = 0;
}
uint16_t c = Color(r,g,b);
//uint16_t c = Wheel((i+j) % 96);
// the 16 bit color we get from Wheel is actually made of 5 bits RGB, we can use bitwise notation to get it out and
// convert it to 8 bit
strip.setLEDcolorPWM(i, (c & 0x1F) << 3, ((c>>10) & 0x1F) << 3, ((c>>5) & 0x1F) << 3);
strip.setLEDcolorPWM(31- i, (c & 0x1F) << 3, ((c>>10) & 0x1F) << 3, ((c>>5) & 0x1F) << 3);
delay(1);
}
}
void vu1(int micin, float micmax, float micgain) {
for (uint8_t i=0; i< strip.numLEDs()/2 ; i++) {
//Serial.println(i);
uint16_t c = vucalc(i, micin, micmax, micgain);
//uint16_t c = Wheel((i+j) % 96);
// the 16 bit color we get from Wheel is actually made of 5 bits RGB, we can use bitwise notation to get it out and
// convert it to 8 bit
strip.setLEDcolorPWM(i, (c & 0x1F) << 3, ((c>>10) & 0x1F) << 3, ((c>>5) & 0x1F) << 3);
strip.setLEDcolorPWM(31-i, (c & 0x1F) << 3, ((c>>10) & 0x1F) << 3, ((c>>5) & 0x1F) << 3);
}
//j++;
// there's only 96 colors in the 'wheel' so wrap around
//if (j > 96) { j = 0; }
delay(4);
}
unsigned int vucalc(uint8_t i, int micin, float micmax, float micgain) {
float micvol = (micmax/100)-((micgain/1024)*10);
r = 0;
g = 0;
b = 0;
if(i==15) {
r=0; // Grün
g=16+random(0,15);
b=0;
} else if(micin>micvol && i==14) {
r=0; // GrünBlau
g=21;
b=13;
} else if(micin>micvol*10 && i==13) {
r=0; // BlauGrün
g=13;
b=21;
} else if(micin>micvol*20 && i==12) {
r=0; // Blau
g=0;
b=21;
} else if(micin>micvol*30 && i==11) {
r=13; // Violett
g=0;
b=21;
} else if(micin>micvol*40 && i==10) {
r=21; // Pink
g=0;
b=13;
} else if(micin>micvol*50 && i==9) {
r=31; // Rot
g=0;
b=0;
} else if(micin>micvol*58 && i==8) {
r=21; // Orange
g=13;
b=0;
} else if(micin>micvol*66 && i==7) {
r=13; // Gelb
g=21;
b=0;
} else if(micin>micvol*73 && i==6) {
r=0; // Grün
g=21;
b=0;
} else if(micin>micvol*80 && i==5) {
r=0; // Teal
g=21;
b=13;
} else if(micin>micvol*90 && i<5) {
r=31; // Weiss
g=31;
b=31;
} else {
r-=0;
g-=0;
b-=0;
}
return(Color(r,g,b));
}
unsigned int vucalcALT(uint8_t i, int micin, int micmax) {
int micmax1 = micmax/100;
r = 0;
g = 0;
b = 0;
if(i==15) {
r=0; // Grün
g=16+random(0,15);
b=0;
} else if(micin>micmax1 && i==14) {
r=0; // GrünBlau
g=21;
b=13;
} else if(micin>micmax1*10 && i==13) {
r=0; // BlauGrün
g=13;
b=21;
} else if(micin>micmax1*20 && i==12) {
r=0; // Blau
g=0;
b=21;
} else if(micin>micmax1*30 && i==11) {
r=13; // Violett
g=0;
b=21;
} else if(micin>micmax1*40 && i==10) {
r=21; // Pink
g=0;
b=13;
} else if(micin>micmax1*50 && i==9) {
r=31; // Rot
g=0;
b=0;
} else if(micin>micmax1*58 && i==8) {
r=21; // Orange
g=13;
b=0;
} else if(micin>micmax1*66 && i==7) {
r=13; // Gelb
g=21;
b=0;
} else if(micin>micmax1*73 && i==6) {
r=0; // Grün
g=21;
b=0;
} else if(micin>micmax1*80 && i==5) {
r=0; // Teal
g=21;
b=13;
} else if(micin>micmax1*90 && i<5) {
r=31; // Weiss
g=31;
b=31;
} else {
r-=0;
g-=0;
b-=0;
}
return(Color(r,g,b));
}
uint8_t j=0;
void ambient() {
for (uint8_t i=0; i< strip.numLEDs() ; i++) {
uint16_t c = Wheel((i+j) % 96);
// the 16 bit color we get from Wheel is actually made of 5 bits RGB, we can use bitwise notation to get it out and
// convert it to 8 bit
strip.setLEDcolorPWM(i, (c & 0x1F) << 3, ((c>>10) & 0x1F) << 3, ((c>>5) & 0x1F) << 3);
}
j++;
// there's only 96 colors in the 'wheel' so wrap around
if (j > 96) { j = 0; }
delay(100);
}
//Input a value 0 to 127 to get a color value.
//The colours are a transition r - g -b - back to r
unsigned int Wheel(byte WheelPos)
{
switch(WheelPos >> 5)
{
case 0:
r=31- WheelPos % 32; //Red down
g=WheelPos % 32; // Green up
b=0; //blue off
break;
case 1:
g=31- WheelPos % 32; //green down
b=WheelPos % 32; //blue up
r=0; //red off
break;
case 2:
b=31- WheelPos % 32; //blue down
r=WheelPos % 32; //red up
g=0; //green off
break;
}
return(Color(r,g,b));
}
// Create a 15 bit color value from R,G,B
unsigned int Color(byte r, byte g, byte b)
{
//Take the lowest 5 bits of each value and append them end to end
return( ((unsigned int)b & 0x1F )<<10 | ((unsigned int)g & 0x1F)<<5 | (unsigned int)r & 0x1F);
}
// METRONOM
//globale Variable fürs delay
int del= 0;
//globale Variable zum speichern der letzten Zeit
int time_old;
void metronome(int micin, int micmax) {
int time_new;
if(micin > 1){
time_new = millis();
if(time_new > time_old ){
del = (time_new-time_old)/strip.numLEDs();
time_old = time_new ;
}
}
metrochase(del);
}
void metrochase(int del) {
uint8_t i;
// turn everything off
for (i=0; i< strip.numLEDs() ; i++) {
strip.setLEDcolorPWM(i, (0 & 0x1F) << 3, ((0>>10) & 0x1F) << 3, ((0>>5) & 0x1F) << 3);
}
for (i=0; i < strip.numLEDs(); i++) {
an(i);
aus(i-1);
delay(del);
strip.setLEDcolorPWM(0, (31 & 0x1F) << 3, (0 & 0x1F) << 3, (0 & 0x1F) << 3);
strip.setLEDcolorPWM(strip.numLEDs()-1, (31 & 0x1F) << 3, (0 & 0x1F) << 3, (0 & 0x1F) << 3);
}
for (i=strip.numLEDs()-1; i >0 ; i--) {
// einmal hin, einmal her
an(i);
aus(i+1);
delay(del);
strip.setLEDcolorPWM(0, (31 & 0x1F) << 3, (0 & 0x1F) << 3, (0 & 0x1F) << 3);
strip.setLEDcolorPWM(strip.numLEDs()-1, (31 & 0x1F) << 3, (0 & 0x1F) << 3, (0 & 0x1F) << 3);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment