Saturday, December 13, 2014

Arduino RGB strip

Knight Rider ...

keep track of misc arduino snippets here.

Get yourself some RGB LED light strip like this one.
These are WS2812B type of chip are in a 5050 package including controller to control every single "pixel" i.e. the RGB LED on the strip and set each color individually.

I always wanted one of those :)
Shipping was from Hong-Kong to Germany a pretty reasonable amount of time (less then three weeks).

So what to do? you start with some examples you'll find on the web.
Good starting point is this page. As this page explains there's basically two major arduino libs for controlling this kind of strip the Neopixel one from Adafruit and FastLed.


From there I took another example code found here:

This gives you some "knight rider" effect.

I broaden the moving lights a bit and added some code to control the speed via a simple poti.


#include "FastLED.h"
//#include <OneSheeld.h>

// How many leds are in the strip?
#define NUM_LEDS 60
// Data pin that led data will be written out over
#define DATA_PIN 6
// This is an array of leds.  One item for each led in your strip.
CRGB leds[NUM_LEDS];

const int poti = A1;
const int button8 = 8;


// This function sets up the ledsand tells the controller about them
void setup() {
  delay(3000);

  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);

}

int duration;
void loop() {
  int potiVal = analogRead(poti);
  float voltage = potiVal * (20.0 / 1023.0);
  int duration = (int)voltage;

  byte hue = 8; // some red

  larsonScanner(NUM_LEDS, hue, 255, duration);

}

void larsonScanner(byte ledcount, byte hue, byte sat, byte duration) {
  static unsigned long previousMillis = millis();
  static int i; // Schrittzaehler
  static int led = 0;
  static boolean reverse = false;
  byte dim;

  if(i > 8) {
    i = 0;

    if (reverse) led--;
    else led++;

    // Anfang erreicht
    if (led < 0) {
      led = 0;
      reverse =! reverse;
    }

    // Ende erreicht
    if (led >= ledcount) {
      led = ledcount-1;
      reverse =! reverse;
    }
  }

  if (millis()-previousMillis > duration) {
    previousMillis = millis();
    memset(leds, 0, ledcount*3); // LED Array zuruecksetzen
    dim = i*4;
    i++;

    // Aktuelle Led-4
    if (led-5>=0 && !reverse) {
      leds[led-5] = CHSV(hue, sat, 32-dim); // FadeDown
    }
    else if (led-5>=0 && reverse) {
      leds[led-5] = CHSV(hue, sat, dim); // FadeUp
    }

    // Aktuelle Led-3
    if (led-4>=0 && !reverse) {
      leds[led-4] = CHSV(hue, sat, 64-dim); // FadeDown
    }
    else if (led-4>=0 && reverse) {
      leds[led-4] = CHSV(hue, sat, 32+dim); // FadeUp
    }

    // Aktuelle Led-2
    if (led-3>=0 && !reverse) {
      leds[led-3] = CHSV(hue, sat, 128-dim); // FadeDown
    }
    else if (led-3>=0 && reverse) {
      leds[led-3] = CHSV(hue, sat, 64+dim); // FadeUp
    }

    // Aktuelle Led-1
    if (led-2>=0 && !reverse) {
      leds[led-2] = CHSV(hue, sat, 255-dim); // FadeDown
    }
    else if (led-2>=0 && reverse) {
      leds[led-2] = CHSV(hue, sat, 127+dim); // FadeUp
    }

    // Aktuelle Led
    leds[led-1] = CHSV(hue, sat, 255);
    leds[led] = CHSV(hue, sat, 255);
    leds[led+1] = CHSV(hue, sat, 255);

    // Aktuelle Led+1
    if (led+2<ledcount && !reverse) {
      leds[led+2] = CHSV(hue, sat, 127+dim); // FadeUp
    }
    else if (led+2<ledcount && reverse) {
      leds[led+2] = CHSV(hue, sat, 255-dim); // FadeDown
    }

    // Aktuelle Led+2
    if (led+3<ledcount && !reverse) {
      leds[led+3] = CHSV(hue, sat, 64+dim); // FadeUp
    }
    else if (led+3<ledcount && reverse) {
      leds[led+3] = CHSV(hue, sat, 128-dim); // FadeDown
    }

    // Aktuelle Led+3
    if (led+4<ledcount && !reverse) {
      leds[led+4] = CHSV(hue, sat, 32+dim); // FadeUp
    }
    else if (led+4<ledcount && reverse) {
      leds[led+4] = CHSV(hue, sat, 64-dim); // FadeDown
    }

    // Aktuelle Led+4
    if (led+5<ledcount && !reverse) {
      leds[led+5] = CHSV(hue, sat, dim); // FadeUp
    }
    else if (led+5<ledcount && reverse) {
      leds[led+5] = CHSV(hue, sat, 32-dim); // FadeDown
    }

    FastLED.show();
  }
}


--

The potentiometer is connected to analog 1 and the digital in of the light strip in dio6.