Skip to content

Instantly share code, notes, and snippets.

@JCMais
Created May 23, 2014 03:01
Show Gist options
  • Save JCMais/54c871f7945a373d763f to your computer and use it in GitHub Desktop.
Save JCMais/54c871f7945a373d763f to your computer and use it in GitHub Desktop.
#include <SPI.h>
#include <Adafruit_WS2801.h>
#include <FastLED.h>
#define ADAFRUIT 1
#define NUM_LEDS 17
#define DATA_PIN 11
#define CLOCK_PIN 13
// Set the first variable to the NUMBER of pixels. 25 = 25 pixels in a row
CRGB leds[NUM_LEDS];
Adafruit_WS2801 strip;
void setup() {
#ifdef ADAFRUIT
strip = Adafruit_WS2801( NUM_LEDS, DATA_PIN, CLOCK_PIN );
strip.begin();
// Update LED contents, to start they are all 'off'
strip.show();
#else
int i = NUM_LEDS;
FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RGB>( leds, NUM_LEDS );
for ( i = 0; i < NUM_LEDS; i++ ) {
leds[i] = CRGB::Black;
}
FastLED.show();
#endif
delay( 4000 );
}
void loop() {
// Some example procedures showing how to display to the pixels
colorWipe( Color( 255, 0, 0 ), 50 );
colorWipe( Color( 0, 255, 0 ), 50 );
colorWipe( Color( 0, 0, 255 ), 50 );
rainbow( 20 );
rainbowCycle( 20 );
}
void rainbow( uint8_t wait ) {
int i, j;
// 3 cycles of all 256 colors in the wheel
for ( j=0; j < 256; j++ ) {
for ( i=0; i < strip.numPixels(); i++ ) {
setStripLedColor( i, Wheel( ( i + j ) % 255 ) );
}
updateStrip(); // write all the pixels out
delay( wait );
}
}
// Slightly different, this one makes the rainbow wheel equally distributed
// along the chain
void rainbowCycle( uint8_t wait ) {
int i, j;
// 5 cycles of all 25 colors in the wheel
for ( j=0; j < 256 * 5; j++ ) {
for ( i=0; i < NUM_LEDS; i++ ) {
// tricky math! we use each pixel as a fraction of the full 96-color wheel
// (thats the i / NUM_LEDS part)
// Then add in j which makes the colors go around per pixel
// the % 96 is to make the wheel cycle around
setStripLedColor( i, Wheel( ( ( i * 256 / NUM_LEDS ) + j ) % 256 ) );
}
updateStrip(); // write all the pixels out
delay( wait );
}
}
// fill the dots one after the other with said color
// good for testing purposes
void colorWipe( uint32_t c, uint8_t wait ) {
int i;
for ( i=0; i < NUM_LEDS; i++ ) {
setStripLedColor( i, c );
delay( wait );
}
}
/* Helper functions */
// Create a 24 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b)
{
uint32_t c;
c = r;
c <<= 8;
c |= g;
c <<= 8;
c |= b;
return c;
}
//Input a value 0 to 255 to get a color value.
//The colours are a transition r - g -b - back to r
uint32_t Wheel( byte WheelPos )
{
if ( WheelPos < 85 ) {
return Color( WheelPos * 3, 255 - WheelPos * 3, 0 );
} else if ( WheelPos < 170 ) {
WheelPos -= 85;
return Color( 255 - WheelPos * 3, 0, WheelPos * 3 );
} else {
WheelPos -= 170;
return Color( 0, WheelPos * 3, 255 - WheelPos * 3 );
}
}
void setStripLedColor( uint16_t i, uint32_t color )
{
#ifdef ADAFRUIT
strip.setPixelColor( i, color );
#else
leds[i] = color;
#endif
}
void updateStrip()
{
#ifdef ADAFRUIT
strip.show();
#else
FastLED.show();
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment