Holiday BlinkyTape!

edited December 2013 in BlinkyTape Fun
Hey all -

I just wrote a quick sketch for Red/Green/White lights with a quick transition fade between that might be of interest to those who are looking to use BlinkyTape for Christmas lighting. Cut and paste code below and upload using the Arduino IDE - it keeps the serial loop so will still work with PatternPaint etc.

And share yours!

// Cut and paste from here!

#include <FastSPI_LED2.h>
#include <Animation.h>

#define LED_COUNT 60
struct CRGB leds[LED_COUNT];

#ifdef REVB // RevB boards have a slightly different pinout.

#define LED_OUT 5
#define BUTTON_IN 13
#define ANALOG_INPUT A11
#define IO_A 15

#else

#define LED_OUT 13
#define BUTTON_IN 10
#define ANALOG_INPUT A9
#define IO_A 7
#define IO_B 11

#endif

#define BRIGHT_STEP_COUNT 5
uint8_t brightnesSteps[BRIGHT_STEP_COUNT] = {5,15,40,70,93};
uint8_t brightness = 4;
uint8_t lastButtonState = 1;

long last_time;
long waiting_time = 1000;
boolean transition = false;
int offset = 0;

void setup()
{
Serial.begin(57600);

LEDS.addLeds<WS2811, LED_OUT, GRB>(leds, LED_COUNT);
LEDS.showColor(CRGB(0, 0, 0));
LEDS.setBrightness(93); // Limit max current draw to 1A
LEDS.show();

pinMode(BUTTON_IN, INPUT_PULLUP);
pinMode(ANALOG_INPUT, INPUT_PULLUP);
pinMode(IO_A, INPUT_PULLUP);
pinMode(IO_B, INPUT_PULLUP);

last_time = millis();
}


void color_loop() {
static uint8_t i = 0;
int done = 0;

static int pixelIndex;

while (transition == true) {
for (uint8_t i = 0; i < LED_COUNT; i++) {
switch ( (i + offset) % 3 ) {
case 0:
// starts red
if (leds[i].r < 255) {
leds[i].r++;
}
if (leds[i].g > 0) {
leds[i].g--;
}
if (leds[i].b > 0) {
leds[i].b--;
}
if (leds[i].r == 255 && leds[i].g == 0 && leds[i].b == 0)
done++;
break;
case 1:
// starts green
if (leds[i].r > 0) {
leds[i].r--;
}
if (leds[i].g < 255) {
leds[i].g++;
}
if (leds[i].b > 0) {
leds[i].b--;
}
if (leds[i].r == 0 && leds[i].g == 255 && leds[i].b == 0)
done++;
break;
case 2:
// starts white red green
if (leds[i].r < 255) {
leds[i].r++;
}
if (leds[i].g < 255) {
leds[i].g++;
}
if (leds[i].b < 255) {
leds[i].b++;
}
if (leds[i].r == 255 && leds[i].g == 255 && leds[i].b == 255)
done++;
break;
}

}
LEDS.show();
if (done > LED_COUNT) {
transition = false;
}
}

offset = offset + 1 % 3;

delay(waiting_time);
transition = true;
}

void serialLoop() {
static int pixelIndex;

while(true) {

if(Serial.available() > 2) {

uint8_t buffer[3]; // Buffer to store three incoming bytes used to compile a single LED color

for (uint8_t x=0; x<3; x++) { // Read three incoming bytes
uint8_t c = Serial.read();

if (c < 255) {
buffer[x] = c; // Using 255 as a latch semaphore
}
else {
LEDS.show();
pixelIndex = 0;

// BUTTON_IN (D10): 07 - 0111
// IO_A(D7): 11 - 1011
// IO_B (D11): 13 - 1101
// ANALOG_INPUT (A9): 14 - 1110

char c = (digitalRead(BUTTON_IN) <<3)
| (digitalRead(IO_A) << 2)
| (digitalRead(IO_B) << 1)
| (digitalRead(ANALOG_INPUT) );
Serial.write(c);

break;
}

if (x == 2) { // If we received three serial bytes
leds[pixelIndex] = CRGB(buffer[0], buffer[1], buffer[2]);
pixelIndex++;
}
}
}
}
}

void loop()
{
// If'n we get some data, switch to passthrough mode
if(Serial.available() > 0) {
serialLoop();
}

uint8_t buttonState = digitalRead(BUTTON_IN);
if((buttonState != lastButtonState) && (buttonState == 0)) {
brightness = (brightness + 1) % BRIGHT_STEP_COUNT;
LEDS.setBrightness(brightnesSteps[brightness]);
}
lastButtonState = buttonState;

color_loop();
}

// That's all!

Comments

  • I tried to run this code, but it didn't seem to work. I notice there are 2 empty include lines at the top. Could those be missing something?
  • Indeed it seems like this was stripped like tags.

    You need (I presume)
    #include <FastSPI_LED2.h>
    #include <Animation.h>
  • Oh weird! Yes - that's what should be in there. Nice catch!
  • For future reference, you can use &lt; and &gt; HTML entities to get that to show up.
  • Thanks, I fixed the above post.
  • datdat
    edited December 2013
    The code still doesn't seem to work for me. This is the Arduino IDE error outpit when I try to compile:

    ChristmasLights.ino: In function 'void setup()':
    ChristmasLights:38: error: no matching function for call to 'CFastSPI_LED2::addLeds(CRGB [60], int)'
    C:\Users\Dat Phan\Documents\Arduino\libraries\FastSPI_LED2/FastSPI_LED2.h:49: note: candidates are: CLEDController* CFastSPI_LED2::addLeds(CLEDController*, const CRGB*, int, int)
  • datdat
    edited December 2013
    I figured out the problem by looking for a similar function call in ColorSwirl example code. I had to change line 38 from this:

    LEDS.addLeds(leds, LED_COUNT);

    to

    LEDS.addLeds<WS2811, LED_OUT, GRB>(leds, LED_COUNT);

    It seems HTML stripped out the <WS2811, LED_OUT, GRB> part



    It also seems this forum's text input puts in < br /> tags into the code when you copy and paste it. There are a few < br /> tags left in the code that break it at around line 143:

    char c = (digitalRead(BUTTON_IN) << 3)<br /> | (digitalRead(IO_A) << 2)<br /> | (digitalRead(IO_B) << 1)<br /> | (digitalRead(ANALOG_INPUT) );

    The line above needs to be changed to:

    char c = (digitalRead(BUTTON_IN) << 3) | (digitalRead(IO_A) << 2) | (digitalRead(IO_B) << 1) | (digitalRead(ANALOG_INPUT) );
  • XanXan
    edited December 2013
    I suggest using https://gist.github.com/ for code snippets.
Sign In or Register to comment.