Jump to content

I just watched episode 213, arduino controlled lighting


G+_Derek Souter
 Share

Recommended Posts

I just watched episode 213, arduino controlled lighting. I was planning to do something similar, as I used most of the same techniques to make a light sabre that I can adjust the colours, brightness, and, as Bryan suggested, change the patterns using a 5th pot. I will try and post my code and a video later. When I get home. As its not the most elegant code, and no doubt there are things I could do better. I did attach a USB cable to it, so I can alter the code without disassembling the entire thing. ?

Link to comment
Share on other sites

here is the code - it is cobbled together with various snippets found online

 

 

#include "FastLED.h"

 

FASTLED_USING_NAMESPACE

 

// FastLED "100-lines-of-code" demo reel, showing just a few

// of the kinds of animation patterns you can quickly and easily

// compose using FastLED.

//

// This example also shows one easy way to define multiple

// animations patterns and have them automatically rotate.

//

// -Mark Kriegsman, December 2014

 

#if FASTLED_VERSION < 3001000

#error "Requires FastLED 3.1 or later; check github for latest code."

#endif

 

#define DATA_PIN 4

//#define CLK_PIN 4

#define LED_TYPE WS2811

#define COLOR_ORDER GRB

#define NUM_LEDS 30

 

int BOTTOM_INDEX = 0;

int TOP_INDEX = int(NUM_LEDS/2);

int EVENODD = NUM_LEDS%2;

 

int howbright = 0;

int howred = 0;

int howgreen = 0;

int howblue = 0;

//int patternchoice = 0;

 

int brightnessPin = 0;

int redPin = 1;

int greenPin = 2;

int bluePin = 3;

int patternpin = 4;

 

int brightnessval = 0;

int redval = 0;

int greenval = 0;

int blueval = 0;

int patternval = 0;

 

CRGB leds[NUM_LEDS];

 

#define BRIGHTNESS 200

#define FRAMES_PER_SECOND 120

 

void setup() {

delay(100); // 3 second delay for recovery

Serial.begin(57600);

// tell FastLED about the LED strip configuration

FastLED.addLeds(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);

//FastLED.addLeds(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);

 

// set master brightness control

FastLED.setBrightness(BRIGHTNESS);

for(int i = 0; i < NUM_LEDS; i++)

{

leds.r = 200;

leds.g = 10;

leds.b = 10;

FastLED.setBrightness(map(i, 0, NUM_LEDS, 10, 200));

FastLED.show();

delay(50);

}

delay(1000);

}

 

 

// List of patterns to cycle through. Each is defined as a separate function below.

typedef void (*SimplePatternList[])();

SimplePatternList gPatterns = { lightpaint, rainbow, flame, sinelon, juggle, bpm, Strobe, RGBLoop};

 

uint8_t patternchoice = 0; // Index number of which pattern is current

uint8_t gHue = 0; // rotating "base color" used by many of the patterns

 

void loop()

{

 

//brightnessval = analogRead(brightnessPin);

 

//redval = analogRead(redPin);

//greenval = analogRead(greenPin);

//blueval = analogRead(bluePin);

 

//patternval = analogRead(patternpin);

 

int howbright = map(analogRead(brightnessPin), 0, 1023, 30, 200);

 

int howred = map(analogRead(redPin), 0, 1023, 0, 255);

int howgreen = map(analogRead(greenPin), 0, 1023, 0,255);

int howblue = map(analogRead(bluePin), 0, 1023, 0, 255);

 

int patternchoice = map(analogRead(patternpin), 0, 1023, 0, 7);

 

// Call the current pattern function once, updating the 'leds' array

gPatterns[patternchoice]();

 

FastLED.setBrightness(howbright);

//FastLED.show();

 

// insert a delay to keep the framerate modest

FastLED.delay(100);

 

// do some periodic updates

EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow

 

 

 

}

 

#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))

 

void lightpaint()

{

redval = analogRead(redPin);

greenval = analogRead(greenPin);

blueval = analogRead(bluePin);

int howred = map(redval, 0, 1023, 0, 255);

int howgreen = map(greenval, 0, 1023, 0,255);

int howblue = map(blueval, 0, 1023, 0, 255);

for(int li = 0; li < NUM_LEDS; li++)

{

leds[li].r = howred;

leds[li].g = howgreen;

leds[li].b = howblue;

}

FastLED.show();

}

 

void rainbow()

{

// FastLED's built-in rainbow generator

fill_rainbow( leds, NUM_LEDS, gHue, 7);

FastLED.show();

}

 

void rainbowWithGlitter()

{

// built-in FastLED rainbow, plus some random sparkly glitter

rainbow();

addGlitter(80);

FastLED.show();

}

 

void addGlitter( fract8 chanceOfGlitter)

{

if( random8() < chanceOfGlitter) {

leds[ random16(NUM_LEDS) ] += CRGB::White;

}

}

 

void confetti()

{

// random colored speckles that blink in and fade smoothly

fadeToBlackBy( leds, NUM_LEDS, 10);

int pos = random16(NUM_LEDS);

leds[pos] += CHSV( gHue + random8(64), 200, 255);

}

 

void flame()

{

int acolor[3];

int idelay = random(0,10);

float hmin = 0.1;

float hmax = 45.0;

float hdif = hmax-hmin;

int randtemp = random(0,3);

float hinc = (hdif/float(TOP_INDEX))+randtemp;

int ahue = hmin;

for(int i = 0; i < TOP_INDEX; i++ )

{

ahue = ahue + hinc;

HSVtoRGB(ahue, 255, 255, acolor);

// leds.r = acolor[0];

leds.g = acolor[1];

leds.b = acolor[2];

leds.setPixel(i, acolor[0], acolor[1], acolor[2]);

int ih = horizontal_index(i);

//

leds[ih].r = acolor[0];

leds[ih].g = acolor[1];

leds[ih].b = acolor[2];

leds.setPixel(ih, acolor[0], acolor[1], acolor[2]);

//

leds[TOP_INDEX].r = 255;

leds[TOP_INDEX].g = 255;

leds[TOP_INDEX].b = 255;

leds.setPixel(TOP_INDEX, 255, 255, 255);

leds.show();

delay(idelay);

}

 

}

 

//void police_lightsALL(int idelay)

//{

//-POLICE LIGHTS (TWO COLOR SOLID)

//idex++;

//if (idex >= NUM_LEDS) {idex = 0;

//}

//int idexR = idex;

//int idexB = antipodal_index(idexR);

//leds.setPixel(idexR, 255, 0, 0);

//leds.setPixel(idexB, 0, 0, 255);

//FastLED.show();

//delay(idelay);

//}

 

 

void sinelon()

{

// a colored dot sweeping back and forth, with fading trails

fadeToBlackBy( leds, NUM_LEDS, 20);

int pos = beatsin16(13,0,NUM_LEDS);

leds[pos] += CHSV( gHue, 255, 192);

}

 

void bpm()

{

// colored stripes pulsing at a defined Beats-Per-Minute (BPM)

uint8_t BeatsPerMinute = 30;

CRGBPalette16 palette = PartyColors_p;

uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);

for( int i = 0; i < NUM_LEDS; i++) { //9948

leds = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));

}

}

 

void juggle() {

// eight colored dots, weaving in and out of sync with each other

fadeToBlackBy( leds, NUM_LEDS, 20);

byte dothue = 0;

for( int i = 0; i < 8; i++) {

leds[beatsin16(i+7,0,NUM_LEDS)] |= CHSV(dothue, 200, 255);

dothue += 32;

}

}

 

 

void showStrip() {

#ifdef ADAFRUIT_NEOPIXEL_H

// NeoPixel

strip.show();

#endif

#ifndef ADAFRUIT_NEOPIXEL_H

// FastLED

FastLED.show();

#endif

}

 

void setPixel(int Pixel, byte red, byte green, byte blue) {

#ifdef ADAFRUIT_NEOPIXEL_H

// NeoPixel

strip.setPixelColor(Pixel, strip.Color(red, green, blue));

#endif

#ifndef ADAFRUIT_NEOPIXEL_H

// FastLED

leds[Pixel].r = red;

leds[Pixel].g = green;

leds[Pixel].b = blue;

#endif

}

 

void setAll(byte red, byte green, byte blue) {

for(int i = 0; i < NUM_LEDS; i++ ) {

setPixel(i, red, green, blue);

}

showStrip();

}

 

 

void RGBLoop(){

for(int j = 0; j < 3; j++ ) {

// Fade IN

for(int k = 0; k < 256; k++) {

switch(j) {

case 0: setAll(k,0,0); break;

case 1: setAll(0,k,0); break;

case 2: setAll(0,0,k); break;

}

showStrip();

delay(3);

}

// Fade OUT

for(int k = 255; k >= 0; k--) {

switch(j) {

case 0: setAll(k,0,0); break;

case 1: setAll(0,k,0); break;

case 2: setAll(0,0,k); break;

}

showStrip();

delay(3);

}

}

}

 

void Strobe(){

for(int j = 0; j < 10; j++) {

setAll(0xff,0xff,0xff);

showStrip();

delay(50);

setAll(0,0,0);

showStrip();

delay(50);

}

 

delay(500);

}

 

//-CONVERT HSV VALUE TO RGB

void HSVtoRGB(int hue, int sat, int val, int colors[3])

{

//

hue: 0-359, sat: 0-255, val (lightness): 0-255 int r, g, b, base;

if (sat == 0)

{

//

Achromatic color (gray). colors[0]=val;

colors[1]=val; colors[2]=val;

}

else

{

base = ((255 - sat) * val)>>8;

switch(hue/60)

{

case 0:

r = val;

g = (((val-base)*hue)/60)+base;

b = base;

break;

case 1:

r = (((val-base)*(60-(hue%60)))/60)+base;

g = val;

b = base;

break;

case 2:

r = base;

g = val;

b = (((val-base)*(hue%60))/60)+base;

break;

case 3:

r = base;

g = (((val-base)*(60-(hue%60)))/60)+base;

b = val;

break;

case 4:

r = (((val-base)*(hue%60))/60)+base;

g = base;

b = val;

break;

case 5:

r = val;

g = base;

b = (((val-base)*(60-(hue%60)))/60)+base;

break;

}

colors[0]=r;

colors[1]=g;

colors[2]=b;

}

}

Link to comment
Share on other sites

 Share

×
×
  • Create New...