Guest User

Untitled

a guest
Jun 22nd, 2016
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.94 KB | None | 0 0
  1. #include<FastLED.h>
  2.  
  3. #define LED_PIN 10
  4. #define BRIGHTNESS 96
  5. #define NUM_LEDS 50
  6. #define CLOCK_PIN 9
  7. #define LED_TYPE WS2801
  8. #define COLOR_ORDER GRB
  9. #define BUTTON_PIN 6
  10.  
  11. int ledMode = 0;
  12. const uint8_t kMatrixWidth = 16;
  13. const uint8_t kMatrixHeight = 16;
  14. const bool kMatrixSerpentineLayout = true;
  15.  
  16. const TProgmemPalette16 DoopColors_p PROGMEM =
  17. {
  18. CRGB::Purple,
  19. CRGB::Black,
  20. CRGB::Red,
  21. CRGB::Black,
  22.  
  23. CRGB::Purple,
  24. CRGB::Purple,
  25. CRGB::Red,
  26. CRGB::Red,
  27.  
  28. CRGB::DarkViolet,
  29. CRGB::Black,
  30. CRGB::DarkRed,
  31. CRGB::Black,
  32.  
  33. CRGB::Purple,
  34. CRGB::Purple,
  35. CRGB::Crimson,
  36. CRGB::Crimson
  37. };
  38.  
  39. unsigned long keyPrevMillis = 0;
  40. const unsigned long keySampleIntervalMs = 25;
  41. byte longKeyPressCountMax = 80; // 80 * 25 = 2000 ms
  42. byte longKeyPressCount = 0;
  43.  
  44. byte prevKeyState = HIGH;
  45. #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
  46. #define MAX_DIMENSION ((kMatrixWidth>kMatrixHeight) ? kMatrixWidth : kMatrixHeight)
  47.  
  48. // The leds
  49. CRGB leds[kMatrixWidth * kMatrixHeight];
  50.  
  51. // The 16 bit version of our coordinates
  52. static uint16_t x;
  53. static uint16_t y;
  54. static uint16_t z;
  55.  
  56. uint8_t noise[MAX_DIMENSION][MAX_DIMENSION];
  57.  
  58. CRGBPalette16 currentPalette( PartyColors_p );
  59. uint8_t colorLoop = 1;
  60.  
  61. void setup() {
  62. delay(3000);
  63. FastLED.addLeds<WS2801, LED_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  64. LEDS.setBrightness(BRIGHTNESS);
  65.  
  66. // Initialize our coordinates to some random values
  67. x = random16();
  68. y = random16();
  69. z = random16();
  70. }
  71.  
  72.  
  73. // Fill the x/y array of 8-bit noise values using the inoise8 function.
  74. void fillnoise8() {
  75. // If we're runing at a low "speed", some 8-bit artifacts become visible
  76. // from frame-to-frame. In order to reduce this, we can do some fast data-smoothing.
  77. // The amount of data smoothing we're doing depends on "speed".
  78. uint8_t dataSmoothing = 0;
  79. if( speed < 50) {
  80. dataSmoothing = 200 - (speed * 4);
  81. }
  82.  
  83. for(int i = 0; i < MAX_DIMENSION; i++) {
  84. int ioffset = scale * i;
  85. for(int j = 0; j < MAX_DIMENSION; j++) {
  86. int joffset = scale * j;
  87.  
  88. uint8_t data = inoise8(x + ioffset,y + joffset,z);
  89.  
  90. // The range of the inoise8 function is roughly 16-238.
  91. // These two operations expand those values out to roughly 0..255
  92. // You can comment them out if you want the raw noise data.
  93. data = qsub8(data,16);
  94. data = qadd8(data,scale8(data,39));
  95.  
  96. if( dataSmoothing ) {
  97. uint8_t olddata = noise[i][j];
  98. uint8_t newdata = scale8( olddata, dataSmoothing) + scale8( data, 256 - dataSmoothing);
  99. data = newdata;
  100. }
  101.  
  102. noise[i][j] = data;
  103. }
  104. }
  105.  
  106. z += speed;
  107.  
  108. // apply slow drift to X and Y, just for visual variation.
  109. x += speed / 8;
  110. y -= speed / 16;
  111. }
  112.  
  113. void mapNoiseToLEDsUsingPalette()
  114. {
  115. static uint8_t ihue=0;
  116.  
  117. for(int i = 0; i < kMatrixWidth; i++) {
  118. for(int j = 0; j < kMatrixHeight; j++) {
  119. // We use the value at the (i,j) coordinate in the noise
  120. // array for our brightness, and the flipped value from (j,i)
  121. // for our pixel's index into the color palette.
  122.  
  123. uint8_t index = noise[j][i];
  124. uint8_t bri = noise[i][j];
  125.  
  126. // if this palette is a 'loop', add a slowly-changing base value
  127. if( colorLoop) {
  128. index += ihue;
  129. }
  130.  
  131. // brighten up, as the color palette itself often contains the
  132. // light/dark dynamic range desired
  133. if( bri > 127 ) {
  134. bri = 255;
  135. } else {
  136. bri = dim8_raw( bri * 2);
  137. }
  138.  
  139. CRGB color = ColorFromPalette( currentPalette, index, bri);
  140. leds[XY(i,j)] = color;
  141. }
  142. }
  143.  
  144. ihue+=1;
  145. }
  146.  
  147. void loop() {
  148. // Periodically choose a new palette, speed, and scale
  149. ChangePaletteAndSettingsPeriodically();
  150.  
  151. // generate noise data
  152. fillnoise8();
  153.  
  154. // convert the noise data to colors in the LED array
  155. // using the current palette
  156. mapNoiseToLEDsUsingPalette();
  157.  
  158. LEDS.show();
  159. // delay(10);
  160. }
  161.  
  162.  
  163.  
  164.  
  165. //#define HOLD_PALETTES_X_TIMES_AS_LONG 10
  166.  
  167. void ChangePaletteAndSettingsPeriodically()
  168. {
  169.  
  170. byte currKeyState = digitalRead(BUTTON_PIN);
  171.  
  172. if ((prevKeyState == LOW) && (currKeyState == HIGH))
  173. prevKeyState = currKeyState;
  174.  
  175. static uint8_t startIndex = 0;
  176. startIndex = startIndex + 1; /* motion speed */
  177.  
  178. switch (ledMode) {
  179.  
  180. case 0:
  181. {currentPalette = RainbowColors_p; speed = 20; scale = 30; colorLoop = 1;}
  182. break;
  183. case 1:
  184. { currentPalette = DoopColors_p; speed = 8; scale = 50; colorLoop = 0; }
  185. break;
  186. case 2:
  187. { SetupBlackAndWhiteStripedPalette(); speed = 20; scale = 30; colorLoop = 1; }
  188. break;
  189. case 3:
  190. { currentPalette = ForestColors_p; speed = 8; scale =120; colorLoop = 0; }
  191. break;
  192. case 4:
  193. { currentPalette = CloudColors_p; speed = 4; scale = 30; colorLoop = 0; }
  194. break;
  195. case 5:
  196. { currentPalette = LavaColors_p; speed = 8; scale = 50; colorLoop = 0; }
  197. break;
  198. case 6:
  199. { currentPalette = OceanColors_p; speed = 20; scale = 90; colorLoop = 0; }
  200. break;
  201. case 7:
  202. { currentPalette = PartyColors_p; speed = 20; scale = 30; colorLoop = 1; }
  203. break;
  204. case 8:
  205. { SetupRandomPalette(); speed = 20; scale = 20; colorLoop = 1; }
  206. break;
  207. case 9:
  208. { SetupRandomPalette(); speed = 50; scale = 50; colorLoop = 1; }
  209. break;
  210. case 10:
  211. { SetupRandomPalette(); speed = 90; scale = 90; colorLoop = 1; }
  212. break;
  213. case 11:
  214. { currentPalette = RainbowStripeColors_p; speed = 30; scale = 20; colorLoop = 1; }
  215. break;
  216. case 12:
  217. { SetupPurpleAndGreenPalette(); speed = 10; scale = 50; colorLoop = 1; }
  218. break;
  219. }
  220.  
  221.  
  222. // if( lastSecond != secondHand) {
  223. // lastSecond = secondHand;
  224. // if( secondHand == 0) { currentPalette = RainbowColors_p; speed = 20; scale = 30; colorLoop = 1; }
  225. // if( secondHand == 5) { SetupPurpleAndGreenPalette(); speed = 10; scale = 50; colorLoop = 1; }
  226. // if( secondHand == 10) { SetupBlackAndWhiteStripedPalette(); speed = 20; scale = 30; colorLoop = 1; }
  227. // if( secondHand == 15) { currentPalette = ForestColors_p; speed = 8; scale =120; colorLoop = 0; }
  228. // if( secondHand == 20) { currentPalette = CloudColors_p; speed = 4; scale = 30; colorLoop = 0; }
  229. // if( secondHand == 25) { currentPalette = LavaColors_p; speed = 8; scale = 50; colorLoop = 0; }
  230. // if( secondHand == 30) { currentPalette = OceanColors_p; speed = 20; scale = 90; colorLoop = 0; }
  231. // if( secondHand == 35) { currentPalette = PartyColors_p; speed = 20; scale = 30; colorLoop = 1; }
  232. // if( secondHand == 40) { SetupRandomPalette(); speed = 20; scale = 20; colorLoop = 1; }
  233. // if( secondHand == 45) { SetupRandomPalette(); speed = 50; scale = 50; colorLoop = 1; }
  234. // if( secondHand == 50) { SetupRandomPalette(); speed = 90; scale = 90; colorLoop = 1; }
  235. // if( secondHand == 55) { currentPalette = RainbowStripeColors_p; speed = 30; scale = 20; colorLoop = 1; }
  236. // }
  237. }
  238.  
  239. void SetupRandomPalette()
  240. {
  241. currentPalette = CRGBPalette16(
  242. CHSV( random8(), 255, 32),
  243. CHSV( random8(), 255, 255),
  244. CHSV( random8(), 128, 255),
  245. CHSV( random8(), 255, 255));
  246. }
  247.  
  248. // This function sets up a palette of black and white stripes,
  249. // using code. Since the palette is effectively an array of
  250. // sixteen CRGB colors, the various fill_* functions can be used
  251. // to set them up.
  252. void SetupBlackAndWhiteStripedPalette()
  253. {
  254. // 'black out' all 16 palette entries...
  255. fill_solid( currentPalette, 16, CRGB::Black);
  256. // and set every fourth one to white.
  257. currentPalette[0] = CRGB::White;
  258. currentPalette[4] = CRGB::White;
  259. currentPalette[8] = CRGB::White;
  260. currentPalette[12] = CRGB::White;
  261.  
  262. }
  263.  
  264. // This function sets up a palette of purple and green stripes.
  265. void SetupPurpleAndGreenPalette()
  266. {
  267. CRGB purple = CHSV( HUE_PURPLE, 255, 255);
  268. CRGB green = CHSV( HUE_GREEN, 255, 255);
  269. CRGB black = CRGB::Black;
  270.  
  271. currentPalette = CRGBPalette16(
  272. green, green, black, black,
  273. purple, purple, black, black,
  274. green, green, black, black,
  275. purple, purple, black, black );
  276. }
  277.  
  278.  
  279. //
  280. // Mark's xy coordinate mapping code. See the XYMatrix for more information on it.
  281. //
  282. uint16_t XY( uint8_t x, uint8_t y)
  283. {
  284. uint16_t i;
  285. if( kMatrixSerpentineLayout == false) {
  286. i = (y * kMatrixWidth) + x;
  287. }
  288. if( kMatrixSerpentineLayout == true) {
  289. if( y & 0x01) {
  290. // Odd rows run backwards
  291. uint8_t reverseX = (kMatrixWidth - 1) - x;
  292. i = (y * kMatrixWidth) + reverseX;
  293. } else {
  294. // Even rows run forwards
  295. i = (y * kMatrixWidth) + x;
  296. }
  297. }
  298. return i;
  299. }
Advertisement
Add Comment
Please, Sign In to add comment