Advertisement
Guest User

1D RGB LED Strip Pong

a guest
Jan 16th, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.76 KB | None | 0 0
  1. #include "SPI.h"
  2. #include <Tone.h>
  3. #include <Wire.h>
  4. #include "Adafruit_WS2801.h"
  5. #include "Adafruit_LEDBackpack.h"
  6. #include "Adafruit_GFX.h"
  7.  
  8. Adafruit_7segment matrix = Adafruit_7segment();
  9.  
  10. // Define pin numbers for each device
  11. // a is left side, b right side
  12. #define abutton 2
  13. #define bbutton 8
  14. #define switch1 3 // fast rainbow
  15. #define switch2 12 // slow rainbow
  16. #define switch3 5 // fast moving rainbow
  17. #define switch4 6 // slow moving rainbow
  18. #define switch5 9 // harder game switch
  19. #define mute 4 // mute switch
  20. #define pot 0 //analog pin
  21. uint8_t SPEAKER = 7;
  22.  
  23. //for reference in hooking up, not used as they are hardware spi pins
  24. //int dataPin = 11;
  25. //int clockPin = 13;
  26.  
  27. // TWI (I2C) pins
  28. // datapin = A4
  29. // clockpin = A5
  30.  
  31. //color list for random color generation
  32. #define numColors 128
  33. int randColors[numColors][3] =
  34. {
  35. {127,0,0},//red
  36. {127,127,0},//yellow
  37. {0,127,0},//green
  38. {0,127,127},//cyan
  39. {0,0,127},//blue
  40. {127,0,127},//violet
  41. {100,100,10},//light blue
  42. {165,42,42},
  43. {178,34,34},
  44. {220,20,60},
  45. {255,99,71},
  46. {255,127,80},
  47. {205,92,92},
  48. {240,128,128},
  49. {233,150,122},
  50. {250,128,114},
  51. {255,160,122},
  52. {255,69,0},
  53. {255,140,0},
  54. {255,165,0},
  55. {255,215,0},
  56. {184,134,11},
  57. {218,165,32},
  58. {238,232,170},
  59. {189,183,107},
  60. {240,230,140},
  61. {128,128,0},
  62. {255,255,0},
  63. {154,205,50},
  64. {85,107,47},
  65. {107,142,35},
  66. {124,252,0},
  67. {127,255,0},
  68. {173,255,47},
  69. {34,139,34},
  70. {50,205,50},
  71. {144,238,144},
  72. {152,251,152},
  73. {143,188,143},
  74. {0,250,154},
  75. {0,255,127},
  76. {46,139,87},
  77. {102,205,170},
  78. {60,179,113},
  79. {32,178,170},
  80. {47,79,79},
  81. {0,128,128},
  82. {0,139,139},
  83. {0,255,255},
  84. {0,255,255},
  85. {224,255,255},
  86. {0,206,209},
  87. {64,224,208},
  88. {72,209,204},
  89. {175,238,238},
  90. {127,255,212},
  91. {176,224,230},
  92. {95,158,160},
  93. {70,130,180},
  94. {100,149,237},
  95. {0,191,255},
  96. {30,144,255},
  97. {173,216,230},
  98. {135,206,235},
  99. {135,206,250},
  100. {25,25,112},
  101. {65,105,225},
  102. {138,43,226},
  103. {75,0,130},
  104. {72,61,139},
  105. {106,90,205},
  106. {123,104,238},
  107. {147,112,219},
  108. {139,0,139},
  109. {148,0,211},
  110. {153,50,204},
  111. {186,85,211},
  112. {128,0,128},
  113. {216,191,216},
  114. {221,160,221},
  115. {238,130,238},
  116. {255,0,255},
  117. {218,112,214},
  118. {199,21,133},
  119. {219,112,147},
  120. {255,20,147},
  121. {255,105,180},
  122. {255,182,193},
  123. {255,192,203},
  124. {250,235,215},
  125. {245,245,220},
  126. {255,228,196},
  127. {255,235,205},
  128. {245,222,179},
  129. {255,248,220},
  130. {255,250,205},
  131. {250,250,210},
  132. {255,255,224},
  133. {139,69,19},
  134. {160,82,45},
  135. {210,105,30},
  136. {205,133,63},
  137. {244,164,96},
  138. {222,184,135},
  139. {210,180,140},
  140. {188,143,143},
  141. {255,228,181},
  142. {255,222,173},
  143. {255,218,185},
  144. {255,228,225},
  145. {255,240,245},
  146. {250,240,230},
  147. {253,245,230},
  148. {255,239,213},
  149. {255,245,238},
  150. {245,255,250},
  151. {112,128,144},
  152. {119,136,153},
  153. {176,196,222},
  154. {230,230,250},
  155. {255,250,240},
  156. {240,248,255},
  157. {248,248,255},
  158. {240,255,240},
  159. {255,255,240},
  160. {240,255,255},
  161. {255,250,250},
  162. {128,128,128},
  163. };
  164.  
  165. // Define notes for win music
  166. // start at A below middle C
  167. // Musical notes are defined differently in tone.h, that's why I had to included these here
  168. #define NOTE_Ab 207.652
  169. #define NOTE_A 220.000
  170. #define NOTE_As 233.082
  171. #define NOTE_Bb NOTE_As
  172. #define NOTE_B 246.942
  173. #define NOTE_C 261.626
  174. #define NOTE_Cs 277.183
  175. #define NOTE_Db NOTE_Cs
  176. #define NOTE_D 293.665
  177. #define NOTE_Ds 311.127
  178. #define NOTE_Eb NOTE_Ds
  179. #define NOTE_E 329.628
  180. #define NOTE_F 349.228
  181. #define NOTE_Fs 369.994
  182. #define NOTE_Gb NOTE_Fs
  183. #define NOTE_G 391.995
  184. #define NOTE_Gs 415.305
  185. #define NOTE_REST 0.0
  186. #define NOTE_SUSTAIN -1.0
  187.  
  188. // Part of Bach 2-part invention #4 in D minor
  189. // http://www.8notes.com/scores/2791.asp
  190. int tempo = 150;
  191. float composition[] = {
  192. NOTE_D, NOTE_E, NOTE_F, NOTE_G, NOTE_A*2, NOTE_As*2,
  193. NOTE_Db, NOTE_As*2, NOTE_A*2, NOTE_G, NOTE_F, NOTE_E,
  194. NOTE_F, NOTE_REST, NOTE_A*2, NOTE_REST, NOTE_D*2, NOTE_REST,
  195. NOTE_G, NOTE_REST, NOTE_Cs*2, NOTE_REST, NOTE_E*2, NOTE_REST,
  196.  
  197. NOTE_D*2, NOTE_E*2, NOTE_F*2, NOTE_G*2, NOTE_A*4, NOTE_As*4,
  198. NOTE_Db*2, NOTE_As*4, NOTE_A*4, NOTE_G*2, NOTE_F*2, NOTE_E*2,
  199. };
  200.  
  201. // uses a non standard tone library
  202. // http://code.google.com/p/rogue-code/wiki/ToneLibraryDocumentation
  203. Tone tone1;
  204.  
  205. // flags
  206. // N: new game, i.e first time plugged in
  207. // S: same game, each round is still same game
  208. // R: right
  209. // L: left
  210. char flag = 'N';
  211. char turn = 'A';
  212. char cheat = 'N';
  213.  
  214. int winningscore = 12;
  215. int nLEDs = 32;
  216.  
  217. int maxdelay = 60;//35
  218. int mindelay = 18;
  219.  
  220. int i = 0;
  221. int j = 0;
  222. byte abuttonPoints = 0;
  223. byte bbuttonPoints = 0;
  224.  
  225. // ball speed variables
  226. byte ballSpeed = 0;
  227. byte ballSpeedIncrease = 0;
  228. int ballInitialSpeed = 0;
  229.  
  230. // how far away you can hit the button without missing is one less than this
  231. // e.g. if bz=5 then you have to hit within five pixels of the edge
  232. int normalbounce = 5;
  233. int reducedbounce = 3;
  234. int bouncezone = normalbounce;
  235.  
  236. // define strip, mine uses ws2801 chips, require ws2801 library
  237. // https://github.com/adafruit/Adafruit-WS2801-Library
  238. Adafruit_WS2801 strip = Adafruit_WS2801(nLEDs);
  239.  
  240. /* Helper functions for RGB strip colors */
  241. // Create a 24 bit color ballInitialSpeedue from R,G,B
  242. uint32_t Color(byte r, byte g, byte b)
  243. {
  244. uint32_t c;
  245. c = r;
  246. c <<= 8;
  247. c |= g;
  248. c <<= 8;
  249. c |= b;
  250. return c;
  251. }
  252.  
  253. //Input a value 0 to 255 to get a color value.
  254. //The colors are a transition r - g -b - back to r
  255. uint32_t Wheel(byte WheelPos)
  256. {
  257. if (WheelPos < 85) {
  258. return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  259. } else if (WheelPos < 170) {
  260. WheelPos -= 85;
  261. return Color(255 - WheelPos * 3, 0, WheelPos * 3);
  262. } else {
  263. WheelPos -= 170;
  264. return Color(0, WheelPos * 3, 255 - WheelPos * 3);
  265. }
  266. }
  267.  
  268. void setup()
  269. {
  270. //assign buttons
  271. pinMode(abutton, INPUT);
  272. pinMode(bbutton, INPUT);
  273. pinMode(switch1, INPUT);
  274. pinMode(mute, INPUT);
  275. pinMode(SPEAKER, OUTPUT);
  276.  
  277. //initialize speaker
  278. tone1.begin(SPEAKER);
  279.  
  280. //initialize LED strip
  281. strip.begin();
  282. // Update LED contents, to start they are all 'off'
  283. strip.show();
  284.  
  285. //initalize 7 segment LED
  286. matrix.begin(0x70);
  287.  
  288. randomSeed(analogRead(1));
  289. }
  290.  
  291. void loop()
  292. {
  293. // points display code
  294. if (abuttonPoints <= 9)
  295. {
  296. matrix.writeDigitNum(1, abuttonPoints);
  297. matrix.writeDigitNum(0, 0);
  298. }
  299. else
  300. {
  301. int temp = abuttonPoints-10;
  302. if (temp==0)
  303. {
  304. matrix.writeDigitNum(1, 0);
  305. }
  306. else
  307. {
  308. matrix.writeDigitNum(1, abuttonPoints-10);
  309. }
  310. matrix.writeDigitNum(0, 1);
  311. }
  312. matrix.drawColon(true);
  313. if (bbuttonPoints <= 9)
  314. {
  315. matrix.writeDigitNum(4, bbuttonPoints);
  316. matrix.writeDigitNum(3, 0);
  317. }
  318. else
  319. {
  320. int temp = bbuttonPoints-10;
  321. if (temp==0)
  322. {
  323. matrix.writeDigitNum(4, 0);
  324. }
  325. else
  326. {
  327. matrix.writeDigitNum(4, bbuttonPoints-10);
  328. }
  329. matrix.writeDigitNum(3, 1);
  330. }
  331. matrix.writeDisplay();
  332.  
  333. // switch for light show - all color rainbow fast
  334. if (digitalRead(switch1) == HIGH)
  335. {
  336. rainbow(5);
  337. // set strip off for new game
  338. for(i=0; i < nLEDs; i++) strip.setPixelColor(i, 0);
  339. strip.show();
  340. }
  341. // switch for light show - all color rainbow slow
  342. if (digitalRead(switch2) == HIGH)
  343. {
  344. rainbow(25);
  345. // set strip off for new game
  346. for(i=0; i < nLEDs; i++) strip.setPixelColor(i, 0);
  347. strip.show();
  348. }
  349.  
  350. // switch for light show - moving rainbow - fast
  351. if (digitalRead(switch3) == HIGH)
  352. {
  353. rainbowCycleabutton(0);
  354. }
  355.  
  356. // switch for light show - moving rainbow - slow
  357. if (digitalRead(switch4) == HIGH)
  358. {
  359. rainbowCyclebbutton(5);
  360. }
  361.  
  362. // switch for harder game - reduce bounce zone
  363. if (digitalRead(switch5) == HIGH)
  364. {
  365. bouncezone = reducedbounce;
  366. }
  367. if (digitalRead(switch5) == LOW)
  368. {
  369. bouncezone = normalbounce;
  370. }
  371.  
  372.  
  373. // a wins
  374. if(abuttonPoints > winningscore)
  375. {
  376. // reset speed increase conuter
  377. ballSpeedIncrease = 0;
  378. // light show
  379. abuttonCelebrate();
  380. }
  381.  
  382. // b wins
  383. if(bbuttonPoints > winningscore)
  384. {
  385. // reset speed increase conuter
  386. ballSpeedIncrease = 0;
  387. // light show
  388. bbuttonCelebrate();
  389. }
  390.  
  391. // flag starts as N
  392. // set pixel light for player who's turn it is
  393. // this runs once, the first time the game is plugged in
  394. if(flag == 'N' && turn == 'A')
  395. {
  396. strip.setPixelColor(0, Color(100, 100, 10));
  397. strip.show();
  398. }
  399. if(flag == 'N' && turn == 'B')
  400. {
  401. strip.setPixelColor(nLEDs-1, Color(100, 100, 10));
  402. strip.show();
  403. }
  404.  
  405. // pot is being read anytime the ball is not moving
  406. if(flag != 'R' && flag != 'L')
  407. {
  408. ballInitialSpeed = analogRead(pot);
  409. ballInitialSpeed = map(ballInitialSpeed, 0, 1023, mindelay, maxdelay);
  410. ballSpeed = ballInitialSpeed - ballSpeedIncrease;
  411. }
  412.  
  413. // flag == right
  414. if(flag == 'R')
  415. {
  416. // increase ball speed by reducing delay
  417. ballSpeedIncrease += 1;
  418. ballSpeed = ballInitialSpeed - ballSpeedIncrease;
  419. if(ballSpeed < 0)
  420. ballSpeed = 0;
  421. if(ballSpeed > maxdelay)
  422. ballSpeed = maxdelay;
  423.  
  424. // play tick sound
  425. if(digitalRead(mute) == LOW)
  426. {
  427. tone1.play(NOTE_F5, 50);
  428. }
  429. delay(50);
  430.  
  431. int rand = random(0,numColors+1);
  432. // random color ball goes to the right
  433. chaseRight(Color(randColors[rand][1], randColors[rand][2], randColors[rand][3]), ballSpeed);
  434. }
  435. // flag = left
  436. if(flag == 'L')
  437. {
  438. // increase ball speed by reducing delay
  439. ballSpeedIncrease += 1;
  440. ballSpeed = ballInitialSpeed - ballSpeedIncrease;
  441. if(ballSpeed < 0)
  442. ballSpeed = 0;
  443. if(ballSpeed > maxdelay)
  444. ballSpeed = maxdelay;
  445. if(digitalRead(mute) == LOW)
  446. {
  447. tone1.play(NOTE_F5, 50);
  448. }
  449.  
  450. delay(50);
  451.  
  452. int rand = random(0,numColors+1);
  453. // random color ball goes to the left
  454. chaseLeft(Color(randColors[rand][1], randColors[rand][2], randColors[rand][3]), ballSpeed);
  455. }
  456.  
  457. int AbuttonState = digitalRead(abutton);
  458. if(AbuttonState == HIGH && turn == 'A' && flag != 'R' && flag != 'L')
  459. {
  460. ballSpeed = ballInitialSpeed;
  461. ballSpeedIncrease = 0;
  462. i = 0;
  463. int rand = random(0,numColors+1);
  464. // random color ball goes to the right
  465. chaseRight(Color(randColors[rand][1], randColors[rand][2], randColors[rand][3]), ballSpeed);
  466. }
  467.  
  468. int BbuttonState = digitalRead(bbutton);
  469. if(BbuttonState == HIGH && turn == 'B' && flag != 'R' && flag != 'L')
  470. {
  471. ballSpeed = ballInitialSpeed;
  472. ballSpeedIncrease = 0;
  473. i = nLEDs;
  474. int rand = random(0,numColors+1);
  475. // random color ball goes to the left
  476. chaseLeft(Color(randColors[rand][1], randColors[rand][2], randColors[rand][3]), ballSpeed);
  477. }
  478. }
  479.  
  480. void win_music()
  481. {
  482. unsigned long note = composition[i];
  483.  
  484. for (i=1; i<37 ; i++)
  485. {
  486. unsigned long note = composition[i];
  487. if (note == NOTE_REST)
  488. tone1.stop();
  489. else if (note == NOTE_SUSTAIN)
  490. ; // Don't do anything, just let the current tone continue
  491. else
  492. tone1.play(note);
  493. delay(tempo);
  494. }
  495.  
  496. }
  497.  
  498. void chaseRight(uint32_t c, uint8_t wait)
  499. {
  500. for(i; i < nLEDs; i++)
  501. {
  502. if(digitalRead(bbutton) == HIGH)
  503. {
  504. cheat = 'Y';
  505. if(digitalRead(mute) == LOW)
  506. {
  507. tone1.play(NOTE_F3,150);
  508. }
  509. }
  510. strip.setPixelColor(i, c); // Set new pixel 'on'
  511. strip.show(); // Refresh LED states
  512. strip.setPixelColor(i, 0); // Erase pixel, but don't refresh!
  513. delay(wait);
  514.  
  515. if(i > nLEDs-bouncezone-1 && cheat != 'Y')
  516. {
  517. int BbuttonState = digitalRead(bbutton);
  518. if(BbuttonState == HIGH)
  519. {
  520. turn = 'B';
  521. flag = 'L';
  522. return;
  523. }
  524. }
  525. }
  526. abuttonPoints++;
  527. if(abuttonPoints <= winningscore)
  528. strip.show();
  529. if(digitalRead(mute) == LOW)
  530. {
  531. tone1.play(NOTE_B3, 500);
  532. }
  533. delay(500);
  534. flag = 'S';
  535. cheat = 'N';
  536.  
  537. // set light at player a side
  538. strip.setPixelColor(0, Color(100, 100, 10));
  539. strip.show();
  540. }
  541.  
  542. void chaseLeft(uint32_t c, uint8_t wait)
  543. {
  544. for(i; i >= 0; i--)
  545. {
  546. if(digitalRead(abutton) == HIGH)
  547. {
  548. cheat = 'Y';
  549. if(digitalRead(mute) == LOW)
  550. {
  551. tone1.play(NOTE_F3,150);
  552. }
  553. }
  554. strip.setPixelColor(i, c); // Set new pixel 'on'
  555. strip.show(); // Refresh LED states
  556. strip.setPixelColor(i, 0); // Erase pixel, but don't refresh!
  557. delay(wait);
  558.  
  559. if(i < bouncezone && cheat != 'Y')
  560. {
  561. int AbuttonState = digitalRead(abutton);
  562. if(AbuttonState == HIGH)
  563. {
  564. //i = 0;
  565. turn = 'A';
  566. flag = 'R';
  567. return;
  568. }
  569. }
  570. }
  571. bbuttonPoints++;
  572. strip.show();
  573. if(bbuttonPoints <= winningscore)
  574. // play lose point sound
  575. if(digitalRead(mute) == LOW)
  576. {
  577. tone1.play(NOTE_B3, 500);
  578. }
  579.  
  580. delay(500);
  581. flag = 'S';
  582. cheat = 'N';
  583.  
  584. // set light at player b side
  585. strip.setPixelColor(nLEDs-1, Color(100, 100, 10));
  586. strip.show();
  587. }
  588.  
  589. void abuttonCelebrate()
  590. {
  591. abuttonPoints = 0;
  592. bbuttonPoints = 0;
  593. if(digitalRead(mute) == LOW)
  594. {
  595. win_music();
  596. }
  597. colorWipeLeft(Color(255, 0, 0), 20);
  598. colorWipeLeft(Color(0, 255, 0), 20);
  599. colorWipeLeft(Color(0, 0, 255), 20);
  600. rainbowCycleabutton(0);
  601. flag = 'N';
  602. turn = 'B';
  603. }
  604.  
  605. void bbuttonCelebrate()
  606. {
  607. abuttonPoints = 0;
  608. bbuttonPoints = 0;
  609. if(digitalRead(mute) == LOW)
  610. {
  611. win_music();
  612. }
  613. colorWipeRight(Color(255, 0, 0), 20);
  614. colorWipeRight(Color(0, 255, 0), 20);
  615. colorWipeRight(Color(0, 0, 255), 20);
  616. rainbowCyclebbutton(0);
  617. flag = 'N';
  618. turn = 'A';
  619. }
  620.  
  621. void rainbowCycleabutton(uint8_t wait) {
  622. int m = nLEDs;
  623. uint16_t k, j;
  624. for (j=0; j < 384 * 3; j++) { // 5 cycles of all 384 colors in the Wheel
  625. for (k=0; k < strip.numPixels(); k++) {
  626. // tricky math! we use each pixel as a fraction of the full 384-color Wheel
  627. // (thats the i / strip.numPixels() part)
  628. // Then add in j which makes the colors go around per pixel
  629. // the % 384 is to make the Wheel cycle around
  630. strip.setPixelColor(k, Wheel( ((k * 384 / strip.numPixels()) + j) % 384) );
  631. }
  632. strip.show(); // write all the pixels out
  633. delay(wait);
  634. }
  635. for(m; m >= 0; m--)
  636. {
  637. strip.setPixelColor(m, 0);
  638. strip.show();
  639. }
  640. }
  641.  
  642. void rainbowCyclebbutton(uint8_t wait) {
  643. int m = 0;
  644. uint16_t k, j;
  645. for (j=384 * 3; j > 0; j--) { // 5 cycles of all 384 colors in the Wheel
  646. for (k=0; k < strip.numPixels(); k++) {
  647. // tricky math! we use each pixel as a fraction of the full 384-color Wheel
  648. // (thats the i / strip.numPixels() part)
  649. // Then add in j which makes the colors go around per pixel
  650. // the % 384 is to make the Wheel cycle around
  651. strip.setPixelColor(k, Wheel( ((k * 384 / strip.numPixels()) + j) % 384) );
  652. }
  653. strip.show(); // write all the pixels out
  654. delay(wait);
  655. }
  656. for(m; m < nLEDs; m++)
  657. {
  658. strip.setPixelColor(m, 0);
  659. strip.show();
  660. }
  661. }
  662.  
  663. void rainbow(uint8_t wait) {
  664. int i, j;
  665.  
  666. for (j=0; j < 256; j++) { // 3 cycles of all 256 colors in the Wheel
  667. for (i=0; i < strip.numPixels(); i++) {
  668. strip.setPixelColor(i, Wheel( (i + j) % 255));
  669. }
  670. strip.show(); // write all the pixels out
  671. delay(wait);
  672. }
  673. }
  674.  
  675. // fill the dots one after the other with said color
  676. // good for testing purposes
  677. // goes to the right
  678. void colorWipeRight(uint32_t c, uint8_t wait)
  679. {
  680. for (int i=0; i < strip.numPixels(); i++)
  681. {
  682. strip.setPixelColor(i, c);
  683. strip.show();
  684. delay(wait);
  685. }
  686. }
  687.  
  688. void colorWipeLeft(uint32_t c, uint8_t wait)
  689. {
  690. for (i=nLEDs; i > 0; i--)
  691. {
  692. strip.setPixelColor(i, c);
  693. strip.show();
  694. delay(wait);
  695. }
  696. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement