Guest User

1D RGB LED Pong With Progressive Ball Speed

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