Advertisement
Guest User

Arduino Pong

a guest
Aug 17th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1.  
  2. #include <VGA.h>
  3. #include "pitches.h"
  4.  
  5. int ballX = 240;
  6. int ballY = 240;
  7. int p1X;
  8. int p1YTop;
  9. int p1YBottom;
  10. int p2X;
  11. int p2YTop;
  12. int p2YBottom;
  13. int xVel;
  14. int yVel;
  15. int WHEEL_ONE_PIN= A0;
  16. int WHEEL_TWO_PIN= A1;
  17. int wheelOnePosition;
  18. int wheelTwoPosition;
  19. int paddleWidth = 50;
  20. int velocityX = 1;
  21. int velocityY = -1;
  22. int p1Score = 0;
  23. int p2Score = 0;
  24. int RESET_PIN = 40;
  25. char p1ScoreChar[2];
  26. char p2ScoreChar[2];
  27. String str2;
  28. String str;
  29.  
  30.  
  31. //Tone section
  32. // timers TC0 TC1 TC2 channels 0-2 ids 0-2 3-5 6-8 AB 0 1
  33. // use TC1 channel 0
  34. #define TONE_TIMER TC1
  35. #define TONE_CHNL 0
  36. #define TONE_IRQ TC3_IRQn
  37.  
  38. // TIMER_CLOCK4 84MHz/128 with 16 bit counter give 10 Hz to 656KHz
  39. // piano 27Hz to 4KHz
  40.  
  41. static uint8_t pinEnabled[PINS_COUNT];
  42. static uint8_t TCChanEnabled = 0;
  43. static boolean pin_state = false ;
  44. static Tc *chTC = TONE_TIMER;
  45. static uint32_t chNo = TONE_CHNL;
  46.  
  47. volatile static int32_t toggle_count;
  48. static uint32_t tone_pin;
  49.  
  50. // frequency (in hertz) and duration (in milliseconds).
  51.  
  52. void setup() {
  53. VGA.begin(640,480);
  54. pinMode(40, INPUT);
  55. Serial.begin(9600);
  56.  
  57. }
  58.  
  59. void processInputs() {
  60. VGA.clear();
  61. str=String(p1Score);
  62. str2=String(p2Score);
  63. str2.toCharArray(p2ScoreChar,2);
  64. str.toCharArray(p1ScoreChar,2);
  65. VGA.drawText(p1ScoreChar, 20, 460, 1, -256, 0);
  66. VGA.drawText(p2ScoreChar, 20, 20, 1, -256, 0);
  67. wheelOnePosition = ((analogRead(WHEEL_ONE_PIN)*640)/1023)-50;
  68. if (wheelOnePosition>=0){
  69. VGA.fillRect(wheelOnePosition,35,wheelOnePosition+50,40, 1);
  70. }else{
  71. VGA.fillRect(0,35,50,40,1);
  72. }
  73. wheelTwoPosition = ((analogRead(WHEEL_TWO_PIN)*640)/1023)-50;
  74. if (wheelTwoPosition>=0){
  75. VGA.fillRect(wheelTwoPosition,440,wheelTwoPosition+50,445, 1);
  76. }else{
  77. VGA.fillRect(0,440,50,445,1);
  78. }
  79. impactCheck();
  80. ballUpdate();
  81. VGA.waitSync();
  82. }
  83.  
  84. void loop() {
  85. VGA.clear();
  86. while (digitalRead(RESET_PIN)==1){
  87. printMenu();
  88. }
  89. VGA.clear();
  90. delay(1000);
  91. while(digitalRead(RESET_PIN)==1){
  92. processInputs();
  93. }
  94. p1Score = 0;
  95. p2Score = 0;
  96. VGA.clear();
  97. delay(1000);
  98. VGA.waitSync();
  99. }
  100.  
  101. void printMenu(){
  102. VGA.drawText("Press RESET on Side to Begin", 225, 240, 1, 0, 0);
  103. VGA.drawText("Turn Knobs to Cntrol Paddles", 225, 250, 1, 0, 0);
  104. }
  105.  
  106. void impactCheck(){
  107. if ((ballY == 430) && ((ballX>=wheelTwoPosition) && (ballX<=wheelTwoPosition+50))){
  108. velocityY = -velocityY;
  109. tone(8, NOTE_G3, 250);
  110. }
  111. if ((ballY == 40) && ((ballX>=wheelOnePosition) && (ballX<=wheelOnePosition+50))){
  112. velocityY = -velocityY;
  113. tone(8, NOTE_G3, 250);
  114. }
  115. if ((ballX == 0) || (ballX==630)){
  116. velocityX = -velocityX;
  117. }
  118. if (ballY == 0){
  119. tone(8, NOTE_C3, 1000);
  120. p1Score++;
  121. ballX = 240;
  122. ballY = 240;
  123. VGA.fillEllipse(ballX,ballY,ballX+10,ballY+10,1);
  124. delay(500);
  125.  
  126. }
  127. if (ballY == 640){
  128. tone(8, NOTE_C3, 1000);
  129. p2Score++;
  130. ballX = 240;
  131. ballY = 240;
  132. VGA.fillEllipse(ballX,ballY,ballX+10,ballY+10,1);
  133. delay(500);
  134. }
  135. }
  136.  
  137. void ballUpdate(){
  138. if (velocityX == 1){
  139. ballX+=2;
  140. }else{
  141. ballX-=2;
  142. }
  143. if (velocityY == 1){
  144. ballY+=2;
  145. }else{
  146. ballY-=2;
  147. }
  148. VGA.fillEllipse(ballX,ballY,ballX+10,ballY+10,1);
  149. }
  150.  
  151. void tone(uint32_t ulPin, uint32_t frequency, int32_t duration)
  152. {
  153. const uint32_t rc = VARIANT_MCK / 256 / frequency;
  154. tone_pin = ulPin;
  155. toggle_count = 0; // strange wipe out previous duration
  156. if (duration > 0 ) toggle_count = 2 * frequency * duration / 1000;
  157. else toggle_count = -1;
  158.  
  159. if (!TCChanEnabled) {
  160. pmc_set_writeprotect(false);
  161. pmc_enable_periph_clk((uint32_t)TONE_IRQ);
  162. TC_Configure(chTC, chNo,
  163. TC_CMR_TCCLKS_TIMER_CLOCK4 |
  164. TC_CMR_WAVE | // Waveform mode
  165. TC_CMR_WAVSEL_UP_RC ); // Counter running up and reset when equals to RC
  166.  
  167. chTC->TC_CHANNEL[chNo].TC_IER=TC_IER_CPCS; // RC compare interrupt
  168. chTC->TC_CHANNEL[chNo].TC_IDR=~TC_IER_CPCS;
  169. NVIC_EnableIRQ(TONE_IRQ);
  170. TCChanEnabled = 1;
  171. }
  172. if (!pinEnabled[ulPin]) {
  173. pinMode(ulPin, OUTPUT);
  174. pinEnabled[ulPin] = 1;
  175. }
  176. TC_Stop(chTC, chNo);
  177. TC_SetRC(chTC, chNo, rc); // set frequency
  178. TC_Start(chTC, chNo);
  179. }
  180.  
  181. void noTone(uint32_t ulPin)
  182. {
  183. TC_Stop(chTC, chNo); // stop timer
  184. digitalWrite(ulPin,LOW); // no signal on pin
  185. }
  186.  
  187. // timer ISR TC1 ch 0
  188. void TC3_Handler ( void ) {
  189. TC_GetStatus(TC1, 0);
  190. if (toggle_count != 0){
  191. // toggle pin TODO better
  192. digitalWrite(tone_pin,pin_state= !pin_state);
  193. if (toggle_count > 0) toggle_count--;
  194. } else {
  195. noTone(tone_pin);
  196. }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement