Skip to content

Instantly share code, notes, and snippets.

@Jylanthas
Created August 26, 2016 06:49
Show Gist options
  • Save Jylanthas/0adcab4a77257e1505c539479a7b5a98 to your computer and use it in GitHub Desktop.
Save Jylanthas/0adcab4a77257e1505c539479a7b5a98 to your computer and use it in GitHub Desktop.
Hermia's LED Cape
#include <FastLED.h>
#define DATA_PIN 6
#define COLOR_ORDER GRB
#define CHIPSET WS2812B
#define BRIGHTNESS 192
// Defining button
const int buttonPin = 2;
int buttonState = 0;
bool oldState = HIGH;
// Palette
CRGBPalette16 currentPalette;
TBlendType currentBlending;
extern CRGBPalette16 myTestPalette2;
extern const TProgmemPalette16 myTestPalette_p PROGMEM;
extern const TProgmemPalette16 myTestPalette2_p PROGMEM;
extern const TProgmemPalette16 myTestPalette3_p PROGMEM;
// ============================= ** Matrix set up ** ================================
const uint8_t kMatrixWidth = 28;
const uint8_t kMatrixHeight = 10;
const bool kMatrixSerpentineLayout = true;
uint16_t XY( uint8_t x, uint8_t y)
{
uint16_t i;
if( kMatrixSerpentineLayout == true) {
if( y & 0x01) {
// Odd rows run backwards
uint8_t reverseX = (kMatrixWidth - 1) - x;
i = (y * kMatrixWidth) + reverseX;
} else {
// Even rows run forwards
i = (y * kMatrixWidth) + x;
}
}
return i;
}
#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
CRGB leds[kMatrixWidth * kMatrixHeight];
// ================================== ** SETUP ** =============================================
void setup() {
delay(3000);
FastLED.clear();
FastLED.addLeds<CHIPSET, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalSMD5050);
FastLED.setBrightness( BRIGHTNESS );
// button
//pinMode(DATA_PIN, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
// palette
currentPalette = myTestPalette_p;
currentBlending = NOBLEND;
}
// ======================== ** FUNCTIONS ** ==================================================
void Line(int x0, int y0, int x1, int y1, byte color) {
int dx = abs(x1-x0), sx = x0 < x1 ? 1 : -1;
int dy = -abs(y1-y0), sy = y0 < y1 ? 1 : -1;
int err = dx + dy, e2;
for(;;) {
leds[XY(x0, y0)] = CHSV(color, 255, BRIGHTNESS);
if (x0 == x1 && y0 == y1) break;
e2 = 2 * err;
if (e2 > dy) {
err += dy;
x0 += sx;
}
if (e2 < dx) {
err += dx;
y0 += sy;
}
}
}
// write one pixel with HSV color to coordinates
void Pixel(int x, int y, byte color) {
leds[XY(x, y)] = CHSV(color, 255, 255);
}
void Caleidoscope1() {
for(int x = 0; x < kMatrixWidth / 2 ; x++) {
for(int y = 0; y < kMatrixHeight / 2; y++) {
leds[XY( kMatrixWidth - 1 - x, y )] = leds[XY( x, y )]; // copy to A
leds[XY( x, kMatrixHeight - 1 - y )] = leds[XY( x, y )]; // copy to B
leds[XY( kMatrixWidth - 1 - x, kMatrixHeight - 1 - y )] = leds[XY( x, y )]; // copy to C
}
}
}
// Case 1 - Palettes ----------------------------------------------------
void runPalette(){
cyclePalette();
static uint8_t startIndex = 0;
startIndex = startIndex + 1;
showPalette( startIndex );
delay(15);
FastLED.show();
}
void showPalette( uint8_t colorIndex){
for (int i=0; i<NUM_LEDS; i++){
leds[i] = ColorFromPalette( currentPalette, colorIndex, BRIGHTNESS, currentBlending);
colorIndex +=3;
}
}
void cyclePalette(){
uint8_t secondHand = ( millis() / 1000 ) % 60;
static uint8_t lastSecond = 99;
if ( lastSecond != secondHand ) {
lastSecond = secondHand;
if ( secondHand == 0 ) { currentPalette = myTestPalette_p; currentBlending = NOBLEND; }
if ( secondHand == 15 ) { currentPalette = myTestPalette2_p; currentBlending = LINEARBLEND; }
if ( secondHand == 30 ) { currentPalette = myTestPalette3_p; currentBlending = NOBLEND; }
if ( secondHand == 45 ) { setupCottonCandyPalette(); currentBlending = LINEARBLEND; }
}
}
// Case 2 ...crappy ----------------
void diag_diamond(){
uint8_t hue = 0;
int j = 0;
while ( j < 5 ){
for(int i = 0; i < (kMatrixWidth + kMatrixHeight) / 2 ; i++ ) {
Line( i - kMatrixHeight, kMatrixHeight - 1, i, 0, hue) ;
Caleidoscope1();
hue+=16;
delay(50);
FastLED.show();
}
for(int i = (kMatrixWidth + kMatrixHeight) / 2 ; i > 0 ; i-- ) {
Line( i,0,i - kMatrixHeight, kMatrixHeight - 1, hue) ;
hue+=16;
Caleidoscope1();
delay(50);
FastLED.show();
}
j++;
}
for(int i = 0; i < (kMatrixWidth + kMatrixHeight) / 2 ; i++ ) {
Line( i - kMatrixHeight, kMatrixHeight - 1, i, 0, random8()) ;
Caleidoscope1();
delay(50);
FastLED.show();
}
for(int i = (kMatrixWidth + kMatrixHeight) / 2 ; i > 0 ; i-- ) {
Line( i,0,i - kMatrixHeight, kMatrixHeight - 1, CRGB::Green ) ;
Caleidoscope1();
delay(50);
FastLED.show();
}
for(int i = 0; i < (kMatrixWidth + kMatrixHeight) / 2 ; i++ ) {
Line( i,0,i - kMatrixHeight, kMatrixHeight - 1, CRGB::FireBrick) ;
Caleidoscope1();
delay(50);
FastLED.show();
}
for(int i = (kMatrixWidth + kMatrixHeight) / 2 ; i > 0 ; i-- ) {
Line( i,0,i - kMatrixHeight, kMatrixHeight - 1, 0x615049) ;
Caleidoscope1();
delay(50);
FastLED.show();
}
}
/*
void diagmirror2(){
uint8_t hue = 0;
for(int i = 0; i < (kMatrixWidth + kMatrixHeight) / 2 ; i++ ) {
Line( i,0,kMatrixWidth/4, kMatrixHeight/4, hue) ;
Caleidoscope1();
hue+=16;
delay(1000);
FastLED.show();
}
} */
// Case 5 --------------------------------
void randSparkle(){
// random pixels
uint8_t x = random8(kMatrixWidth);
uint8_t y = random8(kMatrixHeight);
leds[XY(x,y)] = CHSV( random8(), random8(64,255), random8(64,255) );
delay(50);
FastLED.show();
// black
uint8_t x1 = random8(kMatrixWidth);
uint8_t y1 = random8(kMatrixHeight);
leds[XY(x1,y1)] = CRGB( 0x000000 );
delay(50);
FastLED.show();
uint8_t x2 = random8(kMatrixWidth);
uint8_t y2 = random8(kMatrixHeight);
leds[XY(x2,y2)] = CRGB( 0xCC3284 );
delay(50);
FastLED.show();
}
//====================================== ** LOOP ** =================================================
void loop() {
runPalette();
//diag_diamond();
//turnOFF();
//randSparkle();
}
/*
void loop() {
bool newState = digitalRead( buttonPin );
if ( newState == LOW && oldState == HIGH){
delay(20);
newState = digitalRead( buttonPin );
if ( newState == LOW ){
buttonState++;
if ( buttonState > 3 )
{buttonState = 0;}
startShow( buttonState );
}
}
oldState = newState;
}
void startShow( int i ){
switch(i) {
case 0: runPalette();
break;
case 1: diag_diamond();
break;
case 2: randSparkle();
break;
//case 4: noise();
//case 5: fadeout();
}
}
*/
// =================================== ** PALETTES ** ============================================
const TProgmemPalette16 myTestPalette_p PROGMEM =
{
CRGB::Indigo,
CRGB::DarkOrange,
CRGB::Turquoise,
CRGB::Black,
CRGB::Indigo,
CRGB::DarkOrange,
CRGB::Turquoise,
CRGB::Black,
CRGB::Indigo,
CRGB::Indigo,
CRGB::DarkOrange,
CRGB::DarkOrange,
CRGB::Turquoise,
CRGB::Turquoise,
CRGB::Black,
CRGB::Black
};
const TProgmemPalette16 myTestPalette2_p PROGMEM =
{
CRGB::DarkOrange,
CRGB::Maroon,
CRGB::DarkGoldenrod,
CRGB::Black,
CRGB::DarkOrange,
CRGB::Maroon,
CRGB::DarkGoldenrod,
CRGB::Black,
CRGB::DarkOrange,
CRGB::Maroon,
CRGB::DarkGoldenrod,
CRGB::Black,
CRGB::Black
};
const TProgmemPalette16 myTestPalette3_p PROGMEM =
{
CRGB::DeepPink,
CRGB::LightSeaGreen,
CRGB::Green,
CRGB::Black,
};
void setupCottonCandyPalette(){
CRGB pink = CRGB(231, 139, 189);
CRGB yellow = CRGB(255, 231, 154);
CRGB blue = CRGB(132, 176, 209);
CRGB black = CRGB(0, 0, 0);
currentPalette = CRGBPalette16(
pink, yellow, blue, black,
pink, yellow, blue, black,
pink, black, yellow, black,
blue, black, black, black);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment