Guest User

Matrix Code

a guest
Dec 18th, 2015
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.66 KB | None | 0 0
  1. //Code includes samples written by Mark Kriegsman and Andrew Tuline. Thanks so much for all your help guys!
  2.  
  3. #include <FastLED.h>
  4. #include <SPI.h>
  5. #include <LEDMatrix.h>
  6. #include <LEDText.h>
  7. #include <FontRobotron.h>
  8. #include <Font12x16.h>
  9.  
  10.  
  11. //---LED SETUP STUFF
  12. #define BUTTON_PIN 5 //button pin
  13. #define LED_PIN 4
  14. #define COLOR_ORDER GRB
  15. #define CHIPSET WS2812B
  16. #define BRIGHTNESS 255
  17. #define LED_TYPE WS2812B
  18.  
  19.  
  20. //TEXT Matrix
  21. #define MATRIX_WIDTH 79
  22. #define MATRIX_HEIGHT 16
  23. #define MATRIX_TYPE HORIZONTAL_ZIGZAG_MATRIX
  24. //TEXT Matrix END//
  25.  
  26. /////////////////////other effect variables
  27. int SATURATION = 255;
  28. int HUE = 200;
  29. int STEPS =4;
  30. int SPEEDO = 100;
  31. int dataSmoothing = 50;
  32. /////////////////////other effect variables END
  33.  
  34. //Noise Matrix
  35. const uint8_t kMatrixWidth = 79;
  36. const uint8_t kMatrixHeight = 16;
  37. const bool kMatrixSerpentineLayout = true;
  38. #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
  39. #define MAX_DIMENSION ((kMatrixWidth>kMatrixHeight) ? kMatrixWidth : kMatrixHeight)
  40.  
  41. // The leds
  42. CRGB leds2[kMatrixWidth * kMatrixHeight];
  43. //CRGB leds2[kMatrixWidth * kMatrixHeight];
  44.  
  45. // The 16 bit version of our coordinates
  46. static uint16_t x;
  47. static uint16_t y;
  48. static uint16_t z;
  49. uint16_t speed = 20; // speed is set dynamically once we've started up
  50. uint16_t scale = 30; // scale is set dynamically once we've started up
  51. uint8_t noise[MAX_DIMENSION][MAX_DIMENSION];
  52.  
  53. //CRGBPalette16 currentPalette( PartyColors_p );
  54. uint8_t colorLoop = 1;
  55. //Noise Matrix calls END//
  56.  
  57.  
  58.  
  59. cLEDMatrix<MATRIX_WIDTH, MATRIX_HEIGHT, MATRIX_TYPE> leds;
  60.  
  61. cLEDText ScrollingMsg1, ScrollingMsg2, ScrollingMsg3;
  62.  
  63. const unsigned char TxtDemo1[] = { EFFECT_HSV_CH "\x00\xff\xff\x40\xff\xff" EFFECT_SCROLL_LEFT " Ariel Sucks"};
  64. const unsigned char TxtDemo2[] = { EFFECT_SCROLL_LEFT " Touchdown! "};
  65. const unsigned char TxtDemo3[] = { EFFECT_SCROLL_LEFT " Precision Pass! "};
  66. const unsigned char TxtDemo4[] = { EFFECT_SCROLL_LEFT " What a Dime! "};
  67. const unsigned char TxtDemo5[] = { EFFECT_SCROLL_LEFT " Thread the Needle! "};
  68. const unsigned char TxtDemo6[] = { EFFECT_SCROLL_LEFT " On the Money! "};
  69.  
  70.  
  71. //BUTTON SETUP STUFF
  72. byte prevKeyState = HIGH;
  73.  
  74.  
  75. int ledMode = 0; //FIRST ACTIVE MODE
  76.  
  77. CRGBPalette16 currentPalette;
  78. TBlendType currentBlending;
  79.  
  80.  
  81. //------------------SETUP------------------
  82. void setup()
  83. {
  84.  
  85. delay(3000);
  86. FastLED.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds[0],leds.Size()); //WAS leds2
  87. FastLED.setBrightness(200);
  88. FastLED.clear(true);
  89. delay(500);
  90. FastLED.showColor(CRGB::Red);
  91. delay(1000);
  92. FastLED.showColor(CRGB::Lime);
  93. delay(1000);
  94. FastLED.showColor(CRGB::Blue);
  95. delay(1000);
  96. FastLED.showColor(CRGB::White);
  97. delay(1000);
  98. FastLED.show();
  99.  
  100. //ScrollingMsg.SetFont(RobotronFontData);
  101. ScrollingMsg1.SetFont(Font12x16Data);
  102. ScrollingMsg2.SetFont(Font12x16Data);
  103. ScrollingMsg3.SetFont(Font12x16Data);
  104. ScrollingMsg1.Init(&leds, leds.Width(), ScrollingMsg1.FontHeight() + 1, 0, 0);
  105. ScrollingMsg1.SetText((unsigned char *)TxtDemo1, sizeof(TxtDemo1) - 1);
  106. ScrollingMsg2.Init(&leds, leds.Width(), ScrollingMsg1.FontHeight() + 1, 0, 0);
  107. ScrollingMsg2.SetText((unsigned char *)TxtDemo2, sizeof(TxtDemo2) - 1);
  108. ScrollingMsg3.Init(&leds, leds.Width(), ScrollingMsg1.FontHeight() + 1, 0, 0);
  109. ScrollingMsg3.SetText((unsigned char *)TxtDemo3, sizeof(TxtDemo3) - 1);
  110.  
  111. ScrollingMsg1.SetTextColrOptions(COLR_RGB | COLR_SINGLE, 0xff, 0x00, 0xff);
  112.  
  113. //FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds[0], leds.Size());
  114. LEDS.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds2,NUM_LEDS);
  115. LEDS.setBrightness(BRIGHTNESS);
  116.  
  117. pinMode(BUTTON_PIN, INPUT_PULLUP);
  118.  
  119. currentBlending = LINEARBLEND;
  120.  
  121. }
  122.  
  123. #define NUM_MODES 6
  124.  
  125.  
  126. void Message1()
  127. {
  128. // if (ScrollingMsg1.UpdateText() == -1)
  129. // ScrollingMsg1.SetText((unsigned char *)TxtDemo1, sizeof(TxtDemo1) - 1);
  130. // else
  131. FastLED.show();
  132. delay(10);
  133. }
  134.  
  135. void Message2()
  136. {
  137. // if (ScrollingMsg2.UpdateText() == -1)
  138. ScrollingMsg2.SetText((unsigned char *)TxtDemo2, sizeof(TxtDemo2) - 1);
  139. // else
  140. FastLED.show();
  141. delay(10);
  142. }
  143.  
  144. void Message3()
  145. {
  146. if (ScrollingMsg3.UpdateText() == -1)
  147. ScrollingMsg3.SetText((unsigned char *)TxtDemo3, sizeof(TxtDemo3) - 1);
  148. else
  149. FastLED.show();
  150. delay(10);
  151. }
  152. ///////////////////////
  153.  
  154.  
  155.  
  156.  
  157. // Input a value 0 to 255 to get a color value.
  158. // The colours are a transition r - g - b - back to r.
  159. CRGB Wheel(byte WheelPos) {
  160. if(WheelPos < 85) {
  161. return CRGB(WheelPos * 3, 255 - WheelPos * 3, 0);
  162. }
  163. else if(WheelPos < 170) {
  164. WheelPos -= 85;
  165. return CRGB(255 - WheelPos * 3, 0, WheelPos * 3);
  166. }
  167. else {
  168. WheelPos -= 170;
  169. return CRGB(0, WheelPos * 3, 255 - WheelPos * 3);
  170. }
  171. }
  172.  
  173. CRGB randomColor(){
  174. return Wheel(random(256));
  175. }
  176. ////////////////
  177. // Changes all LEDS to given color
  178. void allColor(CRGB c){
  179. for(int i=0; i<NUM_LEDS; i++){
  180. leds2[i] = c;
  181. }
  182. FastLED.show();
  183. }
  184. /////////////////////
  185.  
  186. // Flashes given color
  187. // If c==NULL, random color flash
  188. void flash(CRGB c, int count, int speed){
  189. for(int i=0; i<count; i++){
  190. if(c){
  191. allColor(c);
  192. }
  193. else{
  194. allColor(randomColor());
  195. }
  196. delay(speed);
  197. allColor(CRGB::Black);
  198. delay(speed);
  199. }
  200. }
  201.  
  202. // This function fills the palette with totally random colors.
  203. void SetupTotallyRandomPalette()
  204. {
  205. for( int i = 0; i < 16; i++) {
  206. currentPalette[i] = CHSV( random8(), 255, random8());
  207. }
  208. }
  209.  
  210. // This function sets up a palette of black and white stripes,
  211. // using code. Since the palette is effectively an array of
  212. // sixteen CRGB colors, the various fill_* functions can be used
  213. // to set them up.
  214. void SetupBlackAndWhiteStripedPalette()
  215. {
  216. // 'black out' all 16 palette entries...
  217. fill_solid( currentPalette, 16, CRGB::Black);
  218. // and set every fourth one to white.
  219. currentPalette[0] = CRGB::White;
  220. currentPalette[4] = CRGB::White;
  221. currentPalette[8] = CRGB::White;
  222. currentPalette[12] = CRGB::White;
  223.  
  224. }
  225.  
  226. // This function sets up a palette of purple and green stripes.
  227. void SetupPurpleAndGreenPalette()
  228. {
  229. CRGB purple = CHSV( HUE_PURPLE, 255, 255);
  230. CRGB green = CHSV( HUE_GREEN, 255, 255);
  231. CRGB black = CRGB::Black;
  232.  
  233. currentPalette = CRGBPalette16(
  234. green, green, black, black,
  235. purple, purple, black, black,
  236. green, green, black, black,
  237. purple, purple, black, black );
  238. }
  239.  
  240. void SetupTargetPalette()
  241. {
  242. CRGB red = CHSV( HUE_RED, 255, 255);
  243. //CRGB white = CHSV( HUE_GREEN, 255, 255);
  244. CRGB white = CRGB::White;
  245. //CRGB black = CRGB::Black;
  246.  
  247. currentPalette = CRGBPalette16(
  248. red, red, red, red,
  249. red, red, red, red,
  250. red, red, red, red,
  251. white, white, white, white );
  252.  
  253. }
  254.  
  255. void MyRedWhiteBluePalette()
  256. {
  257. CRGB red = CHSV( HUE_RED, 255, 255);
  258. CRGB blue = CHSV( HUE_BLUE, 255, 255);
  259. CRGB white = CRGB::White;
  260. CRGB black = CRGB::Black;
  261.  
  262. currentPalette = CRGBPalette16(
  263. red, red, blue, blue,
  264. red, red, white, black,
  265. black, red, white, blue,
  266. blue, black, black, black );
  267.  
  268. }
  269.  
  270.  
  271.  
  272.  
  273. /////////////// NOISE AUTO SWITCH PALETTE ///////////////////////////
  274. void ChangePalettePeriodically()
  275. {
  276. uint8_t secondHand = (millis() / 1000) % 60;
  277. static uint8_t lastSecond = 99;
  278.  
  279. if( lastSecond != secondHand) {
  280. lastSecond = secondHand;
  281. if( secondHand == 0) { currentPalette = ForestColors_p; speed = 2; scale = 10; colorLoop = 1; currentBlending = LINEARBLEND; }
  282. if( secondHand == 5) { SetupTargetPalette(); speed = 20; scale = 10; colorLoop = 1;currentBlending = LINEARBLEND; }
  283. if( secondHand == 5) { currentPalette = PartyColors_p; speed = 10; scale = 10; colorLoop = 1; currentBlending = LINEARBLEND; }
  284. if( secondHand == 10) { currentPalette = RainbowStripeColors_p; speed = 30; scale = 10; colorLoop = 1; currentBlending = NOBLEND; }
  285. if( secondHand == 15) { currentPalette = LavaColors_p; speed = 8; scale = 10; colorLoop = 1; currentBlending = LINEARBLEND; }
  286. if( secondHand == 20) { SetupPurpleAndGreenPalette(); speed = 34; scale = 10; colorLoop = 1;currentBlending = LINEARBLEND; }
  287. if( secondHand == 25) { SetupTotallyRandomPalette(); speed = 2; scale = 20; colorLoop = 1;currentBlending = LINEARBLEND; }
  288. if( secondHand == 30) { SetupBlackAndWhiteStripedPalette(); speed = 2; scale = 30; colorLoop = 1;currentBlending = NOBLEND; }
  289. if( secondHand == 35) { SetupBlackAndWhiteStripedPalette(); speed = 20; scale = 40; colorLoop = 1;currentBlending = LINEARBLEND; }
  290. if( secondHand == 40) { currentPalette = CloudColors_p; speed = 4; scale = 30; colorLoop = 1; currentBlending = LINEARBLEND; }
  291. if( secondHand == 45) { currentPalette = PartyColors_p; speed = 2; scale = 20; colorLoop = 1; currentBlending = LINEARBLEND; }
  292. if( secondHand == 50) { MyRedWhiteBluePalette(); speed = 18; scale = 50; colorLoop = 1;currentBlending = NOBLEND; }
  293. if( secondHand == 55) { MyRedWhiteBluePalette(); speed = 2; scale = 10; colorLoop = 1;currentBlending = LINEARBLEND; }
  294. }
  295. }
  296.  
  297.  
  298. //////////////////////// End timed cycle ///////////////////////
  299.  
  300.  
  301. //
  302. // Mark's xy coordinate mapping code. See the XYMatrix for more information on it.
  303. //
  304. uint16_t XY( uint8_t x, uint8_t y)
  305. {
  306. uint16_t i;
  307. if( kMatrixSerpentineLayout == false) {
  308. i = (y * kMatrixWidth) + x;
  309. }
  310. if( kMatrixSerpentineLayout == true) {
  311. if( y & 0x01) {
  312. // Odd rows run backwards
  313. uint8_t reverseX = (kMatrixWidth - 1) - x;
  314. i = (y * kMatrixWidth) + reverseX;
  315. } else {
  316. // Even rows run forwards
  317. i = (y * kMatrixWidth) + x;
  318. }
  319. }
  320. return i;
  321. }
  322.  
  323.  
  324. void mapNoiseToLEDsUsingPalette()
  325. {
  326. static uint8_t ihue=0;
  327.  
  328. for(int i = 0; i < kMatrixWidth; i++) {
  329. for(int j = 0; j < kMatrixHeight; j++) {
  330. // We use the value at the (i,j) coordinate in the noise
  331. // array for our brightness, and the flipped value from (j,i)
  332. // for our pixel's index into the color palette.
  333.  
  334. uint8_t index = noise[j][i];
  335. uint8_t bri = noise[i][j];
  336.  
  337. // if this palette is a 'loop', add a slowly-changing base value
  338. if( colorLoop) {
  339. index += ihue;
  340. }
  341.  
  342. // brighten up, as the color palette itself often contains the
  343. // light/dark dynamic range desired
  344. if( bri > 127 ) {
  345. bri = 255;
  346. } else {
  347. bri = dim8_raw( bri * 2);
  348. }
  349.  
  350. CRGB color = ColorFromPalette( currentPalette, index, bri);
  351. leds2[XY(i,j)] = color;
  352. }
  353. }
  354.  
  355. ihue+=1;
  356. }
  357.  
  358. void fillnoise8() {
  359. if( speed < 50) {
  360. dataSmoothing = 200 - (speed * 4);
  361. }
  362.  
  363. for(int i = 0; i < MAX_DIMENSION; i++) {
  364. int ioffset = scale * i;
  365. for(int j = 0; j < MAX_DIMENSION; j++) {
  366. int joffset = scale * j;
  367.  
  368. uint8_t data = inoise8(x + ioffset,y + joffset,z);
  369.  
  370. // The range of the inoise8 function is roughly 16-238.
  371. // These two operations expand those values out to roughly 0..255
  372. // You can comment them out if you want the raw noise data.
  373. data = qsub8(data,16);
  374. data = qadd8(data,scale8(data,39));
  375.  
  376. if( dataSmoothing ) {
  377. uint8_t olddata = noise[i][j];
  378. uint8_t newdata = scale8( olddata, dataSmoothing) + scale8( data, 256 - dataSmoothing);
  379. data = newdata;
  380. }
  381.  
  382. noise[i][j] = data;
  383. }
  384. }
  385.  
  386. z += speed;
  387.  
  388. // apply slow drift to X and Y, just for visual variation.
  389. x += speed / 8;
  390. y -= speed / 16;
  391. }
  392.  
  393. ////////////////////////////////////////
  394. ///////////////////////////////////////////////////////////////////////////////
  395. void NoiseFill() {
  396. // Periodically choose a new palette, speed, and scale
  397. ChangePalettePeriodically();
  398.  
  399. // generate noise data
  400. fillnoise8();
  401.  
  402. // convert the noise data to colors in the LED array
  403. // using the current palette
  404. mapNoiseToLEDsUsingPalette();
  405.  
  406. LEDS.show();
  407. // delay(10);
  408. }
  409.  
  410. // Fill the x/y array of 8-bit noise values using the inoise8 function.
  411.  
  412. /////////////////////////////////
  413.  
  414.  
  415.  
  416. ////////////////END Toms add/////////////////////////////
  417.  
  418. // SOLID COLOR -------------------------------------
  419. void Solid()
  420. {
  421. fill_solid(leds2, NUM_LEDS, CHSV(HUE, SATURATION, BRIGHTNESS));
  422. FastLED.show();
  423. }
  424.  
  425. //BUTTON CONTROL STUFF
  426. // called when button is pressed
  427. void shortKeyPress() {
  428. Serial.println("short");
  429. ledMode++;
  430. if (ledMode > NUM_MODES){
  431. ledMode=0; }
  432. }
  433.  
  434. // called when key goes from pressed to not pressed
  435. void keyRelease() {
  436. Serial.println("key release");
  437. shortKeyPress();
  438. }
  439.  
  440.  
  441. //------------------MAIN LOOP------------------
  442. void loop() {
  443.  
  444.  
  445. switch (ledMode) {
  446. case 0: Serial.println("case0"); NoiseFill(); delay(10); break; //NoiseFill(); break; //NOISE 1
  447. case 1: Serial.println("case1"); Message1(); delay(10); break; //Message1(); break; //Message 1
  448. case 2: Serial.println("case2"); flash(NULL,5,10); delay(10); break; //flash(NULL,5,100); break;
  449. case 3: Serial.println("case3"); Message2(); delay(10); break; //Message2(); break;
  450. case 4: Serial.println("case4"); NoiseFill(); delay(10); break; //NoiseFill(); break;
  451. case 5: Serial.println("case5"); Message3(); delay(10); break; //Message3(); break;
  452. case 6: Serial.println("case6"); NoiseFill(); delay(10); break; //NoiseFill(); break;
  453.  
  454. }
  455.  
  456.  
  457. byte currKeyState = digitalRead(BUTTON_PIN);
  458.  
  459. if (prevKeyState != currKeyState) {
  460.  
  461. keyRelease();
  462. }
  463.  
  464. prevKeyState = currKeyState;
  465. }
  466.  
  467. ///////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment