Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Code includes samples written by Mark Kriegsman and Andrew Tuline. Thanks so much for all your help guys!
- #include <FastLED.h>
- #include <SPI.h>
- #include <LEDMatrix.h>
- #include <LEDText.h>
- #include <FontRobotron.h>
- #include <Font12x16.h>
- //---LED SETUP STUFF
- #define BUTTON_PIN 5 //button pin
- #define LED_PIN 4
- #define COLOR_ORDER GRB
- #define CHIPSET WS2812B
- #define BRIGHTNESS 255
- #define LED_TYPE WS2812B
- //TEXT Matrix
- #define MATRIX_WIDTH 79
- #define MATRIX_HEIGHT 16
- #define MATRIX_TYPE HORIZONTAL_ZIGZAG_MATRIX
- //TEXT Matrix END//
- /////////////////////other effect variables
- int SATURATION = 255;
- int HUE = 200;
- int STEPS =4;
- int SPEEDO = 100;
- int dataSmoothing = 50;
- /////////////////////other effect variables END
- //Noise Matrix
- const uint8_t kMatrixWidth = 79;
- const uint8_t kMatrixHeight = 16;
- const bool kMatrixSerpentineLayout = true;
- #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
- #define MAX_DIMENSION ((kMatrixWidth>kMatrixHeight) ? kMatrixWidth : kMatrixHeight)
- // The leds
- CRGB leds2[kMatrixWidth * kMatrixHeight];
- //CRGB leds2[kMatrixWidth * kMatrixHeight];
- // The 16 bit version of our coordinates
- static uint16_t x;
- static uint16_t y;
- static uint16_t z;
- uint16_t speed = 20; // speed is set dynamically once we've started up
- uint16_t scale = 30; // scale is set dynamically once we've started up
- uint8_t noise[MAX_DIMENSION][MAX_DIMENSION];
- //CRGBPalette16 currentPalette( PartyColors_p );
- uint8_t colorLoop = 1;
- //Noise Matrix calls END//
- cLEDMatrix<MATRIX_WIDTH, MATRIX_HEIGHT, MATRIX_TYPE> leds;
- cLEDText ScrollingMsg1, ScrollingMsg2, ScrollingMsg3;
- const unsigned char TxtDemo1[] = { EFFECT_HSV_CH "\x00\xff\xff\x40\xff\xff" EFFECT_SCROLL_LEFT " Ariel Sucks"};
- const unsigned char TxtDemo2[] = { EFFECT_SCROLL_LEFT " Touchdown! "};
- const unsigned char TxtDemo3[] = { EFFECT_SCROLL_LEFT " Precision Pass! "};
- const unsigned char TxtDemo4[] = { EFFECT_SCROLL_LEFT " What a Dime! "};
- const unsigned char TxtDemo5[] = { EFFECT_SCROLL_LEFT " Thread the Needle! "};
- const unsigned char TxtDemo6[] = { EFFECT_SCROLL_LEFT " On the Money! "};
- //BUTTON SETUP STUFF
- byte prevKeyState = HIGH;
- int ledMode = 0; //FIRST ACTIVE MODE
- CRGBPalette16 currentPalette;
- TBlendType currentBlending;
- //------------------SETUP------------------
- void setup()
- {
- delay(3000);
- FastLED.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds[0],leds.Size()); //WAS leds2
- FastLED.setBrightness(200);
- FastLED.clear(true);
- delay(500);
- FastLED.showColor(CRGB::Red);
- delay(1000);
- FastLED.showColor(CRGB::Lime);
- delay(1000);
- FastLED.showColor(CRGB::Blue);
- delay(1000);
- FastLED.showColor(CRGB::White);
- delay(1000);
- FastLED.show();
- //ScrollingMsg.SetFont(RobotronFontData);
- ScrollingMsg1.SetFont(Font12x16Data);
- ScrollingMsg2.SetFont(Font12x16Data);
- ScrollingMsg3.SetFont(Font12x16Data);
- ScrollingMsg1.Init(&leds, leds.Width(), ScrollingMsg1.FontHeight() + 1, 0, 0);
- ScrollingMsg1.SetText((unsigned char *)TxtDemo1, sizeof(TxtDemo1) - 1);
- ScrollingMsg2.Init(&leds, leds.Width(), ScrollingMsg1.FontHeight() + 1, 0, 0);
- ScrollingMsg2.SetText((unsigned char *)TxtDemo2, sizeof(TxtDemo2) - 1);
- ScrollingMsg3.Init(&leds, leds.Width(), ScrollingMsg1.FontHeight() + 1, 0, 0);
- ScrollingMsg3.SetText((unsigned char *)TxtDemo3, sizeof(TxtDemo3) - 1);
- ScrollingMsg1.SetTextColrOptions(COLR_RGB | COLR_SINGLE, 0xff, 0x00, 0xff);
- //FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds[0], leds.Size());
- LEDS.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds2,NUM_LEDS);
- LEDS.setBrightness(BRIGHTNESS);
- pinMode(BUTTON_PIN, INPUT_PULLUP);
- currentBlending = LINEARBLEND;
- }
- #define NUM_MODES 6
- void Message1()
- {
- // if (ScrollingMsg1.UpdateText() == -1)
- // ScrollingMsg1.SetText((unsigned char *)TxtDemo1, sizeof(TxtDemo1) - 1);
- // else
- FastLED.show();
- delay(10);
- }
- void Message2()
- {
- // if (ScrollingMsg2.UpdateText() == -1)
- ScrollingMsg2.SetText((unsigned char *)TxtDemo2, sizeof(TxtDemo2) - 1);
- // else
- FastLED.show();
- delay(10);
- }
- void Message3()
- {
- if (ScrollingMsg3.UpdateText() == -1)
- ScrollingMsg3.SetText((unsigned char *)TxtDemo3, sizeof(TxtDemo3) - 1);
- else
- FastLED.show();
- delay(10);
- }
- ///////////////////////
- // Input a value 0 to 255 to get a color value.
- // The colours are a transition r - g - b - back to r.
- CRGB Wheel(byte WheelPos) {
- if(WheelPos < 85) {
- return CRGB(WheelPos * 3, 255 - WheelPos * 3, 0);
- }
- else if(WheelPos < 170) {
- WheelPos -= 85;
- return CRGB(255 - WheelPos * 3, 0, WheelPos * 3);
- }
- else {
- WheelPos -= 170;
- return CRGB(0, WheelPos * 3, 255 - WheelPos * 3);
- }
- }
- CRGB randomColor(){
- return Wheel(random(256));
- }
- ////////////////
- // Changes all LEDS to given color
- void allColor(CRGB c){
- for(int i=0; i<NUM_LEDS; i++){
- leds2[i] = c;
- }
- FastLED.show();
- }
- /////////////////////
- // Flashes given color
- // If c==NULL, random color flash
- void flash(CRGB c, int count, int speed){
- for(int i=0; i<count; i++){
- if(c){
- allColor(c);
- }
- else{
- allColor(randomColor());
- }
- delay(speed);
- allColor(CRGB::Black);
- delay(speed);
- }
- }
- // This function fills the palette with totally random colors.
- void SetupTotallyRandomPalette()
- {
- for( int i = 0; i < 16; i++) {
- currentPalette[i] = CHSV( random8(), 255, random8());
- }
- }
- // This function sets up a palette of black and white stripes,
- // using code. Since the palette is effectively an array of
- // sixteen CRGB colors, the various fill_* functions can be used
- // to set them up.
- void SetupBlackAndWhiteStripedPalette()
- {
- // 'black out' all 16 palette entries...
- fill_solid( currentPalette, 16, CRGB::Black);
- // and set every fourth one to white.
- currentPalette[0] = CRGB::White;
- currentPalette[4] = CRGB::White;
- currentPalette[8] = CRGB::White;
- currentPalette[12] = CRGB::White;
- }
- // This function sets up a palette of purple and green stripes.
- void SetupPurpleAndGreenPalette()
- {
- CRGB purple = CHSV( HUE_PURPLE, 255, 255);
- CRGB green = CHSV( HUE_GREEN, 255, 255);
- CRGB black = CRGB::Black;
- currentPalette = CRGBPalette16(
- green, green, black, black,
- purple, purple, black, black,
- green, green, black, black,
- purple, purple, black, black );
- }
- void SetupTargetPalette()
- {
- CRGB red = CHSV( HUE_RED, 255, 255);
- //CRGB white = CHSV( HUE_GREEN, 255, 255);
- CRGB white = CRGB::White;
- //CRGB black = CRGB::Black;
- currentPalette = CRGBPalette16(
- red, red, red, red,
- red, red, red, red,
- red, red, red, red,
- white, white, white, white );
- }
- void MyRedWhiteBluePalette()
- {
- CRGB red = CHSV( HUE_RED, 255, 255);
- CRGB blue = CHSV( HUE_BLUE, 255, 255);
- CRGB white = CRGB::White;
- CRGB black = CRGB::Black;
- currentPalette = CRGBPalette16(
- red, red, blue, blue,
- red, red, white, black,
- black, red, white, blue,
- blue, black, black, black );
- }
- /////////////// NOISE AUTO SWITCH PALETTE ///////////////////////////
- void ChangePalettePeriodically()
- {
- uint8_t secondHand = (millis() / 1000) % 60;
- static uint8_t lastSecond = 99;
- if( lastSecond != secondHand) {
- lastSecond = secondHand;
- if( secondHand == 0) { currentPalette = ForestColors_p; speed = 2; scale = 10; colorLoop = 1; currentBlending = LINEARBLEND; }
- if( secondHand == 5) { SetupTargetPalette(); speed = 20; scale = 10; colorLoop = 1;currentBlending = LINEARBLEND; }
- if( secondHand == 5) { currentPalette = PartyColors_p; speed = 10; scale = 10; colorLoop = 1; currentBlending = LINEARBLEND; }
- if( secondHand == 10) { currentPalette = RainbowStripeColors_p; speed = 30; scale = 10; colorLoop = 1; currentBlending = NOBLEND; }
- if( secondHand == 15) { currentPalette = LavaColors_p; speed = 8; scale = 10; colorLoop = 1; currentBlending = LINEARBLEND; }
- if( secondHand == 20) { SetupPurpleAndGreenPalette(); speed = 34; scale = 10; colorLoop = 1;currentBlending = LINEARBLEND; }
- if( secondHand == 25) { SetupTotallyRandomPalette(); speed = 2; scale = 20; colorLoop = 1;currentBlending = LINEARBLEND; }
- if( secondHand == 30) { SetupBlackAndWhiteStripedPalette(); speed = 2; scale = 30; colorLoop = 1;currentBlending = NOBLEND; }
- if( secondHand == 35) { SetupBlackAndWhiteStripedPalette(); speed = 20; scale = 40; colorLoop = 1;currentBlending = LINEARBLEND; }
- if( secondHand == 40) { currentPalette = CloudColors_p; speed = 4; scale = 30; colorLoop = 1; currentBlending = LINEARBLEND; }
- if( secondHand == 45) { currentPalette = PartyColors_p; speed = 2; scale = 20; colorLoop = 1; currentBlending = LINEARBLEND; }
- if( secondHand == 50) { MyRedWhiteBluePalette(); speed = 18; scale = 50; colorLoop = 1;currentBlending = NOBLEND; }
- if( secondHand == 55) { MyRedWhiteBluePalette(); speed = 2; scale = 10; colorLoop = 1;currentBlending = LINEARBLEND; }
- }
- }
- //////////////////////// End timed cycle ///////////////////////
- //
- // Mark's xy coordinate mapping code. See the XYMatrix for more information on it.
- //
- uint16_t XY( uint8_t x, uint8_t y)
- {
- uint16_t i;
- if( kMatrixSerpentineLayout == false) {
- i = (y * kMatrixWidth) + x;
- }
- 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;
- }
- void mapNoiseToLEDsUsingPalette()
- {
- static uint8_t ihue=0;
- for(int i = 0; i < kMatrixWidth; i++) {
- for(int j = 0; j < kMatrixHeight; j++) {
- // We use the value at the (i,j) coordinate in the noise
- // array for our brightness, and the flipped value from (j,i)
- // for our pixel's index into the color palette.
- uint8_t index = noise[j][i];
- uint8_t bri = noise[i][j];
- // if this palette is a 'loop', add a slowly-changing base value
- if( colorLoop) {
- index += ihue;
- }
- // brighten up, as the color palette itself often contains the
- // light/dark dynamic range desired
- if( bri > 127 ) {
- bri = 255;
- } else {
- bri = dim8_raw( bri * 2);
- }
- CRGB color = ColorFromPalette( currentPalette, index, bri);
- leds2[XY(i,j)] = color;
- }
- }
- ihue+=1;
- }
- void fillnoise8() {
- if( speed < 50) {
- dataSmoothing = 200 - (speed * 4);
- }
- for(int i = 0; i < MAX_DIMENSION; i++) {
- int ioffset = scale * i;
- for(int j = 0; j < MAX_DIMENSION; j++) {
- int joffset = scale * j;
- uint8_t data = inoise8(x + ioffset,y + joffset,z);
- // The range of the inoise8 function is roughly 16-238.
- // These two operations expand those values out to roughly 0..255
- // You can comment them out if you want the raw noise data.
- data = qsub8(data,16);
- data = qadd8(data,scale8(data,39));
- if( dataSmoothing ) {
- uint8_t olddata = noise[i][j];
- uint8_t newdata = scale8( olddata, dataSmoothing) + scale8( data, 256 - dataSmoothing);
- data = newdata;
- }
- noise[i][j] = data;
- }
- }
- z += speed;
- // apply slow drift to X and Y, just for visual variation.
- x += speed / 8;
- y -= speed / 16;
- }
- ////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////
- void NoiseFill() {
- // Periodically choose a new palette, speed, and scale
- ChangePalettePeriodically();
- // generate noise data
- fillnoise8();
- // convert the noise data to colors in the LED array
- // using the current palette
- mapNoiseToLEDsUsingPalette();
- LEDS.show();
- // delay(10);
- }
- // Fill the x/y array of 8-bit noise values using the inoise8 function.
- /////////////////////////////////
- ////////////////END Toms add/////////////////////////////
- // SOLID COLOR -------------------------------------
- void Solid()
- {
- fill_solid(leds2, NUM_LEDS, CHSV(HUE, SATURATION, BRIGHTNESS));
- FastLED.show();
- }
- //BUTTON CONTROL STUFF
- // called when button is pressed
- void shortKeyPress() {
- Serial.println("short");
- ledMode++;
- if (ledMode > NUM_MODES){
- ledMode=0; }
- }
- // called when key goes from pressed to not pressed
- void keyRelease() {
- Serial.println("key release");
- shortKeyPress();
- }
- //------------------MAIN LOOP------------------
- void loop() {
- switch (ledMode) {
- case 0: Serial.println("case0"); NoiseFill(); delay(10); break; //NoiseFill(); break; //NOISE 1
- case 1: Serial.println("case1"); Message1(); delay(10); break; //Message1(); break; //Message 1
- case 2: Serial.println("case2"); flash(NULL,5,10); delay(10); break; //flash(NULL,5,100); break;
- case 3: Serial.println("case3"); Message2(); delay(10); break; //Message2(); break;
- case 4: Serial.println("case4"); NoiseFill(); delay(10); break; //NoiseFill(); break;
- case 5: Serial.println("case5"); Message3(); delay(10); break; //Message3(); break;
- case 6: Serial.println("case6"); NoiseFill(); delay(10); break; //NoiseFill(); break;
- }
- byte currKeyState = digitalRead(BUTTON_PIN);
- if (prevKeyState != currKeyState) {
- keyRelease();
- }
- prevKeyState = currKeyState;
- }
- ///////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment