joebataz

Untitled

Jul 18th, 2015
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.23 KB | None | 0 0
  1.  
  2.  
  3. #include "FastLED.h" // FastLED library. Preferably the latest copy of FastLED 2.1.
  4. //#include <SoftwareSerial.h>
  5.  
  6. //potentionmeters
  7. int p0 = 0;
  8. int p1 = 0;
  9. int p2 = 0;
  10. int p3 = 0;
  11. int p4 = 0;
  12. int p5 = 0;
  13. int p6 = 0;
  14. int p7 = 0;
  15. int p8 = 0;
  16. int p9 = 0;
  17. int p10 = 0;
  18. int CurPatt = 0; //main function selector
  19.  
  20. //strip definitions
  21. #define COLOR_ORDER GRB
  22. #define LED_TYPE WS2812 //WS2811
  23. #define NUM_LEDS 120
  24. struct CRGB leds[NUM_LEDS]; //init LED array
  25. uint8_t UPDATES_PER_SECOND = 1000;
  26.  
  27. //pin definitions
  28. #define ledData 5 // Serial data pin for WS2812 or WS2801.
  29. #define ledSwitch 11
  30. //#define BUTTON_PIN 11
  31.  
  32. // color macros
  33. #define qsubd(x, b) ((x>b)?wavebright:0) // Use qsuba for smooth pixel colouring and qsubd for non-smooth pixel colouring
  34. #define qsuba(x, b) ((x>b)?x-b:0)
  35.  
  36. //constants
  37. #define h0 1 // Starting height, in meters, of the ball (strip length)
  38. const int NUM_BALLS = 3; // Number of bouncing balls you want (recommend < 7, but 20 is fun in its own way)
  39.  
  40. // fire function vars
  41. int SPARKING = 20;
  42. int COOLING = 80;
  43.  
  44. //bouncer vars
  45. float GRAVITY = -2.81; // Downward (negative) acceleration of gravity in m/s^2
  46. float h[NUM_BALLS] ; // An array of heights
  47. float vImpact0 = sqrt( -2 * GRAVITY * h0 ); // Impact velocity of the ball when it hits the ground if "dropped" from the top of the strip
  48. float vImpact[NUM_BALLS] ; // As time goes on the impact velocity will change, so make an array to store those values
  49. float tCycle[NUM_BALLS] ; // The time since the last time the ball struck the ground
  50. int pos[NUM_BALLS] ; // The integer position of the dot on the strip (LED index)
  51. long tLast[NUM_BALLS] ; // The clock time of the last ground strike
  52. float COR[NUM_BALLS] ; // Coefficient of Restitution (bounce damping)
  53.  
  54. //generic variables
  55. uint8_t max_bright = 128; // for patts that need to be dimmer
  56. uint8_t CurSpeed = 4; // main speed parmeter
  57. uint8_t CurBright = 4; //main brightness parameter
  58. bool oldState = HIGH;
  59.  
  60. //ripple definitions and variables
  61. #define maxsteps 16 // Case statement wouldn't allow a variable.
  62. uint8_t colour; // Ripple colour is randomized.
  63. int center = 0; // Center of the current ripple.
  64. int step = -1; // -1 is the initializing step.
  65. uint8_t myfade = 128; // Starting brightness.
  66. uint8_t bgcol = 8; // Background colour rotates.
  67. int thisdelay = 50; // Standard delay value.
  68.  
  69. //move defs
  70. uint8_t wavebright = 128; //You can change the brightness of the waves/bars rolling across the screen. Best to make them not as bright as the sparkles.
  71. uint8_t thishue = 0; //starting hue value for the this wave.
  72. uint8_t thathue = 140; //starting hue value for the that wave.
  73. uint8_t thisrot = 0; //how quickly the hue rotates for this wave.
  74. uint8_t thatrot = 0; //how quickly the hue rotates for that wave.
  75. uint8_t allsat = 255; //saturated with colour.
  76. uint8_t allfreq = 32; //frequency/width of bars.
  77. uint8_t thisphase = 0; //this phase change
  78. uint8_t thatphase = 0; //thatchange
  79. uint8_t thiscutoff = 192; //cutoff value to display this wave. Lower value = longer wave.
  80. uint8_t thatcutoff = 192; //cutoff value to display that wave.
  81. uint8_t twinkrun = 0; //Enable/disable twinkles.
  82. uint8_t thisdir = 0; //direction
  83. int8_t thisspeed = 4; //this speed.
  84. int8_t thatspeed = -4; //that speed.
  85. typedef struct { //structure for the twinkles
  86. int twinkled; // Supports a long strand of LED's.
  87. int twinkbright; // Defined as 'int', so that we can trigger change on a negative brightness value.
  88. } twinks;
  89. #define numtwinks 8 // You can change the number of twinkles.
  90. twinks mytwinks[numtwinks];
  91.  
  92.  
  93. // palette definitions
  94. CRGBPalette16 currentPalette;
  95. CRGBPalette16 gPal;
  96. TBlendType currentBlending;
  97. const TProgmemRGBPalette16 SALS_p PROGMEM =
  98. {
  99. 0xFF6600,
  100. 0xFF0000,
  101. 0xFF6600,
  102. 0x993300,
  103.  
  104. 0xFF6600,
  105. 0x880000,
  106. 0xFF6600,
  107. 0x880000,
  108.  
  109. 0xFF6600,
  110. 0xFF0000,
  111. 0xFF6600,
  112. 0x993300,
  113.  
  114. 0xFF6600,
  115. 0x880000,
  116. 0xFF6600,
  117. 0x880000
  118.  
  119. };
  120. const TProgmemPalette16 RWB_p PROGMEM =
  121. {
  122. CRGB::Red,
  123. CRGB::Gray, // 'white' is too bright compared to red and blue
  124. CRGB::Blue,
  125. CRGB::Black,
  126.  
  127. CRGB::Red,
  128. CRGB::Gray,
  129. CRGB::Blue,
  130. CRGB::Black,
  131.  
  132. CRGB::Red,
  133. CRGB::Red,
  134. CRGB::Gray,
  135. CRGB::Gray,
  136. CRGB::Blue,
  137. CRGB::Blue,
  138. CRGB::Black,
  139. CRGB::Black
  140. };
  141. void setup() {
  142.  
  143. Serial.begin(4800);
  144. pinMode ( ledSwitch, INPUT );
  145.  
  146. //palettes
  147. LEDS.addLeds<LED_TYPE, ledData,COLOR_ORDER>(leds, NUM_LEDS); // WS2812
  148. FastLED.setBrightness(max_bright);
  149. set_max_power_in_volts_and_milliamps(5, 8000);
  150.  
  151. currentPalette = RainbowColors_p;
  152.  
  153. //bouncer
  154. for (int i = 0 ; i < NUM_BALLS ; i++) { // Initialize variables
  155. tLast[i] = millis();
  156. h[i] = h0;
  157. pos[i] = 0; // Balls start on the ground
  158. vImpact[i] = vImpact0; // And "pop" up at vImpact0
  159. tCycle[i] = 0;
  160. COR[i] = 0.90 - float(i)/pow(NUM_BALLS,2);
  161. }
  162. } // setup()
  163.  
  164.  
  165. //**********************************************************************************************************************************
  166. void loop()
  167. {
  168. getParams();
  169. if (CurPatt == 12)
  170. {
  171. random16_add_entropy(random());
  172. Fire2012WithPalette();
  173. FastLED.show(); // display this frame
  174. FastLED.delay(map(p2,0,1023,1,20) );
  175. }
  176. else if (CurPatt == 13)
  177. {
  178. bouncer();
  179. }
  180. else if (CurPatt == 14)
  181. {
  182. ripple();
  183. show_at_max_brightness_for_power();
  184. delay_at_max_brightness_for_power(thisdelay*1.5);
  185. }
  186. else if (CurPatt == 15)
  187. {
  188. ChangeMe(); // Muck those variable around.
  189. two_sin(); // Simple call to the routine.
  190. show_at_max_brightness_for_power();
  191. delay_at_max_brightness_for_power(thisdelay*1.5);
  192. }
  193. else if (CurPatt == 16)
  194. {
  195. ChangeMe(); // Muck those variable around.
  196. two_sin(); // Simple call to the routine.
  197. twinkleover(); // You can keep or lose the twinkles.
  198. show_at_max_brightness_for_power();
  199. delay_at_max_brightness_for_power(thisdelay*1.5);
  200. }
  201. else
  202. {
  203. RunManual();
  204. static uint8_t startIndex = 0;
  205. startIndex = startIndex + CurSpeed; /* motion speed */
  206. FillLEDsFromPaletteColors( startIndex);
  207. FastLED.show();
  208. FastLED.delay(30);
  209. }
  210. }
  211.  
  212. //**********************************************************************************************************************************
  213.  
  214.  
  215.  
  216. void getParams()
  217. {
  218. bool newState = digitalRead(ledSwitch);
  219. String settings;
  220. p0 = analogRead(A0);
  221. p0 = analogRead(A0);
  222. p1 = analogRead(A1);
  223. p1 = analogRead(A1);
  224. p2 = analogRead(A2);
  225. p2 = analogRead(A2);
  226. p3 = analogRead(A3);
  227. p3 = analogRead(A3);
  228. p4 = analogRead(A4);
  229. p4 = analogRead(A4);
  230. p5 = analogRead(A5);
  231. p5 = analogRead(A5);
  232. p6 = analogRead(A6);
  233. p6 = analogRead(A6);
  234. p7 = analogRead(A7);
  235. p7 = analogRead(A7);
  236. p8 = analogRead(A8);
  237. p8 = analogRead(A8);
  238. p9 = analogRead(A9);
  239. p9 = analogRead(A9);
  240. p10 = analogRead(A10);
  241. p10 = analogRead(A10);
  242. CurPatt = map(p0,0,1023,0,17);
  243. CurBright = map(p1,0,1023,1,255);
  244. CurSpeed = map(p2,0,1023,1,50);
  245. if (newState == LOW) {
  246. settings = "P0:"+ p0;
  247. settings = settings + " P1:"+ p1 + " P2:"+ p2 + " P3:"+ p3 + " P4:" + p4 + " P5:"+ p5 + " P6:" + p6 + " P7:"+ p7 + " P8:" + p8 + " P9:"+ p9 + " P10:"+ p10;
  248. Serial.println(settings);
  249. }
  250. }
  251.  
  252.  
  253. void RunManual()
  254. {
  255. switch (CurPatt)
  256. {
  257. case 0:
  258. currentPalette = SALS_p;
  259. break;
  260. case 1:
  261. currentPalette = RainbowColors_p;
  262. break;
  263. case 2:
  264. currentPalette = RainbowStripeColors_p;
  265. break;
  266. case 3:
  267. currentPalette = PartyColors_p;
  268. break;
  269. case 4:
  270. currentPalette = HeatColors_p;
  271. break;
  272. case 5:
  273. currentPalette = OceanColors_p;
  274. break;
  275. case 6:
  276. currentPalette = ForestColors_p;
  277. break;
  278. case 7:
  279. currentPalette = LavaColors_p;
  280. break;
  281. case 8:
  282. currentPalette = CloudColors_p;
  283. break;
  284. case 9:
  285. currentPalette = RWB_p;
  286. break;
  287. case 10:
  288. SetupPurpleAndGreenPalette();
  289. break;
  290. case 11:
  291. SetupBlackAndWhiteStripedPalette();
  292. break;
  293. }
  294. }// end manual
  295.  
  296. void FillLEDsFromPaletteColors( uint8_t colorIndex)
  297. {
  298. uint8_t brightness = CurBright;
  299.  
  300. for( int i = 0; i < NUM_LEDS; i++) {
  301. leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
  302. colorIndex += 3;
  303. }
  304. }
  305.  
  306. void SetupPurpleAndGreenPalette()
  307. {
  308. CRGB purple = CHSV( HUE_PURPLE, 255, 255);
  309. CRGB green = CHSV( HUE_GREEN, 255, 255);
  310. CRGB black = CRGB::Black;
  311.  
  312. currentPalette = CRGBPalette16(
  313. green, green, black, black,
  314. purple, purple, black, black,
  315. green, green, black, black,
  316. purple, purple, black, black );
  317. }
  318. void SetupBlackAndWhiteStripedPalette()
  319. {
  320. // 'black out' all 16 palette entries...
  321. fill_solid( currentPalette, 16, CRGB::Black);
  322. // and set every fourth one to white.
  323. currentPalette[0] = CRGB::White;
  324. currentPalette[4] = CRGB::White;
  325. currentPalette[8] = CRGB::White;
  326. currentPalette[12] = CRGB::White;
  327.  
  328. }
  329.  
  330. void Fire2012WithPalette()
  331. {
  332. currentPalette = HeatColors_p;
  333. SPARKING = map(p5,0,1023,5,80);
  334. COOLING = map(p4,0,1023,5,80);
  335. // Array of temperature readings at each simulation cell
  336. static byte heat[NUM_LEDS];
  337.  
  338. // Step 1. Cool down every cell a little
  339. for( int i = 0; i < NUM_LEDS; i++) {
  340. heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
  341. }
  342.  
  343. // Step 2. Heat from each cell drifts 'up' and diffuses a little
  344. for( int k= NUM_LEDS - 1; k >= 2; k--) {
  345. heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
  346. }
  347.  
  348. // Step 3. Randomly ignite new 'sparks' of heat near the bottom
  349. if( random8() < SPARKING ) {
  350. int y = random8(7);
  351. heat[y] = qadd8( heat[y], random8(160,240) );
  352. }
  353.  
  354. // Step 4. Map from heat cells to LED colors
  355. for( int j = 0; j < NUM_LEDS; j++) {
  356. // Scale the heat value from 0-255 down to 0-240
  357. // for best results with color palettes.
  358. byte colorindex = scale8( heat[j], 230);
  359. leds[j] = ColorFromPalette( currentPalette, colorindex);
  360. }
  361. }
  362. void bouncer() {
  363. for (int i = 0 ; i < NUM_BALLS ; i++) {
  364. tCycle[i] = millis() - tLast[i] ; // Calculate the time since the last time the ball was on the ground
  365. // A little kinematics equation calculates positon as a function of time, acceleration (gravity) and intial velocity
  366. h[i] = 0.5 * GRAVITY * pow( tCycle[i]/1000 , 2.0 ) + vImpact[i] * tCycle[i]/1000;
  367. if ( h[i] < 0 ) {
  368. h[i] = 0; // If the ball crossed the threshold of the "ground," put it back on the ground
  369. vImpact[i] = COR[i] * vImpact[i] ; // and recalculate its new upward velocity as it's old velocity * COR
  370. tLast[i] = millis();
  371. if ( vImpact[i] < 0.01 ) vImpact[i] = vImpact0; // If the ball is barely moving, "pop" it back up at vImpact0
  372. }
  373. pos[i] = round( h[i] * (NUM_LEDS - 1) / h0); // Map "h" to a "pos" integer index position on the LED strip
  374. }
  375. //Choose color of LEDs, then the "pos" LED on
  376. for (int i = 0 ; i < NUM_BALLS ; i++) leds[pos[i]] = CHSV( uint8_t (i * 40) , 255, 255);
  377. FastLED.show();
  378. //Then off for the next loop around
  379. for (int i = 0 ; i < NUM_BALLS ; i++) {
  380. leds[pos[i]] = CRGB::Black;
  381. }
  382. }
  383. void ripple() {
  384.  
  385. thisdelay = map(p2,0,1023,1,20);
  386.  
  387. for (int i = 0; i < NUM_LEDS; i++) leds[i] = CHSV(bgcol++, 255, CurBright/2); // Rotate background colour.
  388.  
  389. switch (step) {
  390.  
  391. case -1: // Initialize ripple variables.
  392. center = random(NUM_LEDS);
  393. colour = random16(0,256);
  394. step = 0;
  395. break;
  396.  
  397. case 0:
  398. leds[center] = CHSV(colour, 255, 255); // Display the first pixel of the ripple.
  399. step ++;
  400. break;
  401.  
  402. case maxsteps: // At the end of the ripples.
  403. step = -1;
  404. break;
  405.  
  406. default: // Middle of the ripples.
  407. leds[wrap(center + step)] += CHSV(colour, 255, myfade/step*3); // Display the next pixels in the range for one side.
  408. leds[wrap(center - step)] += CHSV(colour, 255, myfade/step*3); // Display the next pixels in the range for the other side.
  409. step ++; // Next step.
  410. break;
  411. } // switch step
  412. } // ripple()
  413.  
  414.  
  415. int wrap(int step) {
  416. if(step < 0) return NUM_LEDS + step;
  417. if(step > NUM_LEDS - 1) return step - NUM_LEDS;
  418. return step;
  419. } // wrap()
  420.  
  421.  
  422. void ChangeMe() {
  423.  
  424. wavebright = map(p1,0,1023,0,128);
  425. allfreq = map(p2,0,1023,1,128);
  426.  
  427. thisspeed = map(p3,0,1023,-50,50);
  428. thatspeed = map(p4,0,1023,-50,50);
  429.  
  430.  
  431. thisrot = map(p5,0,1023,0,255);
  432. thatrot = map(p6,0,1023,0,255);
  433.  
  434. thishue = map(p7,0,1023,0,255);
  435. thathue = map(p8,0,1023,0,255);
  436.  
  437. thiscutoff = map(p9,0,1023,1,240);
  438. thatcutoff = map(p10,0,1023,1,240);
  439. } // ChangeMe()
  440.  
  441.  
  442. void two_sin() { // This is the heart of this program. Sure is short.
  443.  
  444. if (thisdir == 0) {thisphase += thisspeed; thatphase += thatspeed;}
  445. else {thisphase -= thisspeed; thatphase -= thatspeed;}
  446.  
  447. thishue = thishue + thisrot; // Hue rotation is fun for thiswave.
  448. thathue = thathue + thatrot; // It's also fun for thatwave.
  449.  
  450. for (int k=0; k<NUM_LEDS-1; k++) {
  451. int thisbright = qsubd(cubicwave8((k*allfreq)+thisphase), thiscutoff); // qsub sets a minimum value called thiscutoff. If < thiscutoff, then bright = 0. Otherwise, bright = 128 (as defined in qsub)..
  452. int thatbright = qsubd(cubicwave8((k*allfreq)+128+thatphase), thatcutoff); // This wave is 180 degrees out of phase (with the value of 128).
  453.  
  454. leds[k] = CHSV(thishue, allsat, thisbright); // Assigning hues and brightness to the led array.
  455. leds[k] += CHSV(thathue, allsat, thatbright);
  456. }
  457. } // two_sin()
  458.  
  459.  
  460. void twinkleover() { // This has been added for Mark Kriegsman.
  461. for (int i = 0; i < numtwinks; i++) {
  462. if (mytwinks[i].twinkbright <0) {
  463. mytwinks[i].twinkled = random8(0, NUM_LEDS-1);
  464. mytwinks[i].twinkbright = random8(230, 250);
  465. }
  466. leds[mytwinks[i].twinkled] = CHSV(150, 200, mytwinks[i].twinkbright); // Trying to make a soft white twinkle
  467. mytwinks[i].twinkbright -= 8;
  468. }
  469. } // twinkle()
Advertisement
Add Comment
Please, Sign In to add comment