This outlines a project that which incorporated a LED strip into a clear umbrella. The main components of the project were Adafruits NeoPixels and a Gemma Microcontroller. The program has 5 modes which is changed by a button interrupt and potentiometer which contols the speed of the lights. This project was sinspired by Adafruits tutorial projects: FLORAumbrella an Gemma powered Sound Reactive Drums.
This code was modified from the Adafruits Test LED strip code. The sub functions rainbow and rainbow cycle were from the example code. The first three subs fuctions are origional ideas.
#include <EnableInterrupt.h>
#include <Adafruit_NeoPixel.h>
#define LEDPIN 1
#define buttonPIN 0
#define numLEDs 30
#define potPIN 1 // Analog Input Pin A1 or GPIO 2
#define brightness 50
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(numLEDs, LEDPIN, NEO_GRB + NEO_KHZ800);
uint32_t ledOff = strip.Color(0, 0, 0); // Off
uint8_t colorMode = 0 ;
void setup() {
// Serial.begin(9600); // Not for Gemma
strip.begin();
strip.setBrightness(brightness); //adjust brightness here 0 to 255
strip.show(); // Initialize all pixels to 'off'
pinMode(buttonPIN, INPUT_PULLUP);
enableInterrupt(buttonPIN, buttonIndex, FALLING);
}
////////////////////////////
// Looping LED routines //
////////////////////////////
void loop() {
triCycle(6); //Mode 0
rainbowSnake(10); // Mode 1
rainbow_leads_random();// // Mode 2
rainbow();// Mode 3
rainbowCycle(); // Mode 4
}
//////////////////////////////////////
// Interupt function changes mode //
//////////////////////////////////////
void buttonIndex(){
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
// If interrupts come faster than 200ms, assume it's a bounce and ignore
if (interrupt_time - last_interrupt_time > 200){
colorMode += 1;
if (colorMode == 5){
colorMode = 0;
}
}
last_interrupt_time = interrupt_time;
}
////////////////
// Tricycle // = Mode 0
//////////////// = 450 bytes WAS 574 Bytes
void triCycle(uint8_t snake) {
uint16_t i, n, j;
uint16_t m[] = {255,0,255,255,255,0,0,255,255};
for(i=0; i<numLEDs; i++) {
if (colorMode !=0){return;}
for(n=0; n<3; n++) {
for(j=0; j<snake; j++){
strip.setPixelColor((i+j+n*10)%numLEDs, setColorFade(m[n*3],m[n*3+1],m[n*3+2],255*(j+1)/snake));
}
strip.setPixelColor((i+n*10+numLEDs-1)%numLEDs, ledOff);
}
strip.show();
delay(delayDial(20));
}
}
/////////////////////
// Rainbow Snake // = Mode 1
///////////////////// = 332 bytes
void rainbowSnake(uint16_t snake) {
uint16_t i, j;
for(i=0; i<numLEDs; i++) {
if (colorMode !=1){return;}
strip.setPixelColor((i+numLEDs-1)%numLEDs, ledOff);
for (j=0;j<snake;j++) {
strip.setPixelColor((i+j) % numLEDs, Wheel(j*218/(snake-1) & 255));
}
strip.show();
delay(delayDial(20));
}
}
////////////////////////////
// Rainbow Leads Random // = Mode 2
//////////////////////////// = 640 Bytes
void rainbow_leads_random(){
uint16_t i, a;
for(i=0; i<numLEDs; i++){
if (colorMode !=2){return;}
strip.setPixelColor(i, Wheel(random(255)));
for(a=1; a<11; a++) {
strip.setPixelColor(i+a, Wheel((255/10)*a));
}
strip.show();
delay(delayDial(20));
}
}
///////////////
// Rainbow // = Mode 3
/////////////// = 126 Bytes
// Long slow changing rainbow
void rainbow() {
uint16_t i, j;
for(j=0; j<256; j++) {
if (colorMode !=3){return;}
for(i=0; i<numLEDs; i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
delay(delayDial(1));
}
}
/////////////////////
// Rainbow Cycle // = Mode 4
///////////////////// = 132 bytes
void rainbowCycle() {
uint16_t i, j;
for(j=0; j<256; j++) {
if (colorMode !=4){return;}
for(i=0; i< numLEDs; i++) {
strip.setPixelColor(i, Wheel(((i * 256 / numLEDs) + j) & 255));
}
strip.show();
delay(delayDial(1));
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r=0 - g=85 - b=170 - back to r.
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, 255 - WheelPos * 3, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(WheelPos * 3, 0, 255 - WheelPos * 3);
}
}
uint32_t setColorFade(uint16_t r,uint16_t g,uint16_t b,uint16_t f) {
return strip.Color(r*f/255, g*f/255, b*f/255);
}
uint16_t delayDial(uint8_t min) {
return map(analogRead(potPIN), 0, 1023, 200, min);
}