Guest User

1D RGB LED Pong

a guest
Nov 17th, 2012
1,529
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "LPD8806.h"
  2. #include "SPI.h"
  3. #include <Tone.h>
  4. #define Abby 5
  5. #define Bre 6
  6. #define pot A0
  7. #define AbbyButtonLight 11
  8. #define BreButtonLight 12
  9. Tone tone1;
  10.  
  11. int nLEDs = 32;
  12. int dataPin = 9;
  13. int clockPin = 10;
  14. char turn = 'A';
  15. char flag = 'N';
  16. char cheat = 'N';
  17. int i = 0;
  18. int j = 0;
  19. byte AbbyPoints = 0;
  20. byte BrePoints = 0;
  21.  
  22. LPD8806 strip = LPD8806(32, dataPin, clockPin);
  23.  
  24. void setup()
  25. {
  26. pinMode(AbbyButtonLight, OUTPUT);
  27. pinMode(BreButtonLight, OUTPUT);
  28. tone1.begin(13);
  29. pinMode(pot, INPUT);
  30. strip.begin();
  31. strip.show();
  32. pinMode(Abby, INPUT);
  33. pinMode(Bre, INPUT);
  34. digitalWrite(AbbyButtonLight, HIGH);
  35. }
  36.  
  37.  
  38. void loop()
  39. {
  40. if(AbbyPoints > 6)
  41. {
  42. tone1.play(NOTE_G5, 200);
  43. delay(200);
  44. tone1.play(NOTE_GS5, 200);
  45. delay(200);
  46. tone1.play(NOTE_A5, 200);
  47. delay(200);
  48. AbbyCelebrate();
  49. }
  50. if(BrePoints > 6)
  51. {
  52. tone1.play(NOTE_G5, 200);
  53. delay(200);
  54. tone1.play(NOTE_GS5, 200);
  55. delay(200);
  56. tone1.play(NOTE_A5, 200);
  57. delay(200);
  58. BreCelebrate();
  59. }
  60. int val = analogRead(pot);
  61. val = map(val, 0, 1023, 1, 100);
  62.  
  63. if(flag == 'F')
  64. {
  65. tone1.play(NOTE_F5, 50);
  66. delay(50);
  67. chaseForward(strip.Color(127, 0, 0), val);
  68. }
  69. if(flag == 'R')
  70. {
  71. tone1.play(NOTE_F5, 50);
  72. delay(50);
  73. chaseReverse(strip.Color(0, 0, 127), val);
  74. }
  75.  
  76. int AbuttonState = digitalRead(Abby);
  77. if(AbuttonState == HIGH && turn == 'A' && flag != 'F' && flag != 'R')
  78. {
  79. i = 0;
  80. digitalWrite(AbbyButtonLight, LOW);
  81. chaseForward(strip.Color(0, 127, 0), val); // White
  82. }
  83.  
  84. int BbuttonState = digitalRead(Bre);
  85. if(BbuttonState == HIGH && turn == 'B' && flag != 'F' && flag != 'R')
  86. {
  87. i = 32;
  88. digitalWrite(BreButtonLight, LOW);
  89. chaseReverse(strip.Color(127, 0, 127), val); // Red
  90. }
  91.  
  92. /*chaseReverse(strip.Color(127, 0, 0), 15); // Red
  93. chaseForward(strip.Color(127, 127, 0), 15); // Yellow
  94. chaseReverse(strip.Color( 0, 127, 0), 15); // Green
  95. chaseForward(strip.Color( 0, 127, 127), 15); // Cyan
  96. chaseReverse(strip.Color( 0, 0, 127), 15); // Blue
  97. chaseForward(strip.Color(127, 0, 127), 15); // Violet
  98. chaseReverse(strip.Color(100, 100, 10), 15); // Blue */
  99. }
  100.  
  101. void chaseForward(uint32_t c, uint8_t wait) {
  102.  
  103. // Start by turning all pixels off:
  104. //for(i=0; i < 32; i++) strip.setPixelColor(i, 0);
  105.  
  106. for(i; i < 32; i++)
  107. {
  108. if(digitalRead(Bre) == HIGH)
  109. cheat = 'Y';
  110. strip.setPixelColor(i, c); // Set new pixel 'on'
  111. strip.show(); // Refresh LED states
  112. strip.setPixelColor(i, 0); // Erase pixel, but don't refresh!
  113. delay(wait);
  114.  
  115. if(i > 28 && cheat != 'Y')
  116. {
  117. int BbuttonState = digitalRead(Bre);
  118. if(BbuttonState == HIGH)
  119. {
  120. //i = 32;
  121. turn = 'B';
  122. flag = 'R';
  123. return;
  124. //chaseReverse(strip.Color(127, 0, 0), 50);
  125. }
  126. }
  127. }
  128. AbbyPoints++;
  129. if(AbbyPoints < 7)
  130. strip.show();
  131. tone1.play(NOTE_B3, 500);
  132. delay(500);
  133. digitalWrite(AbbyButtonLight, HIGH);
  134. flag = 'S';
  135. cheat = 'N';
  136. // Refresh to turn off last pixel
  137. }
  138.  
  139. void chaseReverse(uint32_t c, uint8_t wait) {
  140.  
  141. // Start by turning all pixels off:
  142. //for(i=0; i < 32; i++) strip.setPixelColor(i, 0);
  143.  
  144. for(i; i >= 0; i--)
  145. {
  146. if(digitalRead(Abby) == HIGH)
  147. cheat = 'Y';
  148. strip.setPixelColor(i, c); // Set new pixel 'on'
  149. strip.show(); // Refresh LED states
  150. strip.setPixelColor(i, 0); // Erase pixel, but don't refresh!
  151. delay(wait);
  152.  
  153. if(i < 5 && cheat != 'Y')
  154. {
  155. int BbuttonState = digitalRead(Abby);
  156. if(BbuttonState == HIGH)
  157. {
  158. //i = 0;
  159. turn = 'A';
  160. flag = 'F';
  161. return;
  162. //chaseForward(strip.Color(127, 127, 127), 50);
  163. }
  164. }
  165. }
  166. BrePoints++;
  167. strip.show();
  168. if(BrePoints < 7)
  169. tone1.play(NOTE_B3, 500);
  170. delay(500);
  171. digitalWrite(BreButtonLight, HIGH);
  172. flag = 'S';
  173. cheat = 'N';
  174. // Refresh to turn off last pixel
  175. }
  176.  
  177. void AbbyCelebrate()
  178. {
  179. //rainbow(10);
  180. rainbowCycleAbby(0);
  181. AbbyPoints = 0;
  182. BrePoints = 0;
  183. }
  184.  
  185. void BreCelebrate()
  186. {
  187. //rainbow(10);
  188. rainbowCycleBre(0);
  189. AbbyPoints = 0;
  190. BrePoints = 0;
  191. }
  192.  
  193. void rainbowCycleAbby(uint8_t wait) {
  194. int m = 32;
  195. uint16_t k, j;
  196. digitalWrite(AbbyButtonLight, LOW);
  197. for (j=0; j < 384 * 3; j++) { // 5 cycles of all 384 colors in the wheel
  198. for (k=0; k < strip.numPixels(); k++) {
  199. // tricky math! we use each pixel as a fraction of the full 384-color wheel
  200. // (thats the i / strip.numPixels() part)
  201. // Then add in j which makes the colors go around per pixel
  202. // the % 384 is to make the wheel cycle around
  203. strip.setPixelColor(k, Wheel( ((k * 384 / strip.numPixels()) + j) % 384) );
  204. }
  205. strip.show(); // write all the pixels out
  206. delay(wait);
  207. }
  208. for(m; m >= 0; m--)
  209. {
  210. strip.setPixelColor(m, 0);
  211. strip.show();
  212. }
  213. digitalWrite(AbbyButtonLight, HIGH);
  214. }
  215.  
  216. void rainbowCycleBre(uint8_t wait) {
  217. int m = 0;
  218. uint16_t k, j;
  219. digitalWrite(BreButtonLight, LOW);
  220. for (j=384 * 3; j > 0; j--) { // 5 cycles of all 384 colors in the wheel
  221. for (k=0; k < strip.numPixels(); k++) {
  222. // tricky math! we use each pixel as a fraction of the full 384-color wheel
  223. // (thats the i / strip.numPixels() part)
  224. // Then add in j which makes the colors go around per pixel
  225. // the % 384 is to make the wheel cycle around
  226. strip.setPixelColor(k, Wheel( ((k * 384 / strip.numPixels()) + j) % 384) );
  227. }
  228. strip.show(); // write all the pixels out
  229. delay(wait);
  230. }
  231. for(m; m < 32; m++)
  232. {
  233. strip.setPixelColor(m, 0);
  234. strip.show();
  235. }
  236. digitalWrite(BreButtonLight, HIGH);
  237. }
  238.  
  239. uint32_t Wheel(uint16_t WheelPos)
  240. {
  241. byte r, g, b;
  242. switch(WheelPos / 128)
  243. {
  244. case 0:
  245. r = 127 - WheelPos % 128; //Red down
  246. g = WheelPos % 128; // Green up
  247. b = 0; //blue off
  248. break;
  249. case 1:
  250. g = 127 - WheelPos % 128; //green down
  251. b = WheelPos % 128; //blue up
  252. r = 0; //red off
  253. break;
  254. case 2:
  255. b = 127 - WheelPos % 128; //blue down
  256. r = WheelPos % 128; //red up
  257. g = 0; //green off
  258. break;
  259. }
  260. return(strip.Color(r,g,b));
  261. }
RAW Paste Data