Advertisement
Guest User

1D RGB LED Pong 11-19-2012

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