Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. #include "mbed.h"
  2. #include <iostream>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <stdio.h>
  6.  
  7. using namespace std;
  8.  
  9. InterruptIn P1Button(p5);
  10. InterruptIn P2Button(p6);
  11. InterruptIn slutt(p7);
  12.  
  13. DigitalOut myled(LED1);
  14. DigitalOut bargraph[5] = {p26, p27, p28, p29, p30};
  15. DigitalOut bsone[4] = {p21, p22, p23, p24};
  16. DigitalOut led1(LED1), led2(LED2);
  17. BusOut buss(p26, p27, p28, p29, p30);
  18.  
  19. const int P1 = 0, P2 = 1;
  20.  
  21. int P1Score = 0;
  22. int P2Score = 0;
  23. int vanskelighetsgrad;
  24. int serveside = P2;
  25.  
  26. float ventetid;
  27. bool P1Timer = false, P2Timer = false, P1Truffet = false, P2Truffet = false;
  28. bool spillTapt = false;
  29.  
  30. Serial pc(USBTX, USBRX);
  31.  
  32. void loopFW ()
  33. {
  34. for(int a = 0; a < 5; a++) {
  35. bargraph[a] = 1;
  36. wait_ms(ventetid);
  37. bargraph[a] = 0;
  38. }
  39. }
  40.  
  41. void loopBW ()
  42. {
  43.  
  44. for(int a = 4; a >= 0; a--) {
  45. bargraph[a] = 1;
  46. wait_ms(ventetid);
  47. bargraph[a] = 0;
  48. }
  49. }
  50.  
  51. void P1Avspark()
  52. {
  53. bsone[0] = 0;
  54. bsone[1] = 1;
  55. bsone[2] = 1;
  56. bsone[3] = 1;
  57. loopBW ();
  58. bsone[0] = 1;
  59. bsone[1] = 0;
  60. loopBW ();
  61. bsone[1] = 1;
  62. bsone[2] = 0;
  63. loopBW ();
  64. bsone[2] = 1;
  65. bsone[3] = 0;
  66. loopBW ();
  67. }
  68.  
  69. void P2Avspark()
  70. {
  71. bsone[0] = 1;
  72. bsone[1] = 1;
  73. bsone[2] = 1;
  74. bsone[3] = 0;
  75. loopFW ();
  76. bsone[2] = 0;
  77. bsone[3] = 1;
  78. loopFW ();
  79. bsone[1] = 0;
  80. bsone[2] = 1;
  81. loopFW ();
  82. bsone[0] = 0;
  83. bsone[1] = 1;
  84. loopFW ();
  85. }
  86.  
  87. void tap (int spiller)
  88. {
  89. if (spiller == P1) {
  90. P2Score++;
  91. cout << "P2 scoret, stilling: " << P1Score << ":" << P2Score << endl;
  92. cout << "Spill tapt: " << spillTapt << endl;
  93. cout << "//" << endl;
  94. } else {
  95. P1Score++;
  96. cout << "P1 scoret, stilling: " << P1Score << ":" << P2Score << endl;
  97. cout << "Spill tapt: " << spillTapt << endl;
  98. cout << "//" << endl;
  99. }
  100. }
  101.  
  102. void P1Skudd()
  103. {
  104. if (P1Timer) {
  105. P1Timer = false;
  106. P1Truffet = true;
  107. } else {
  108. tap(P1);
  109. spillTapt = true;
  110. }
  111. }
  112.  
  113. void P2Skudd()
  114. {
  115. if (P2Timer) {
  116. P2Timer = false;
  117. P2Truffet = true;
  118. } else {
  119. tap(P2);
  120. spillTapt = true;
  121. }
  122. }
  123.  
  124. void avslutt ()
  125. {
  126. //add senere
  127. exit (0);
  128. }
  129.  
  130. int main()
  131. {
  132. P1Button.rise(&P1Skudd);
  133. P2Button.rise(&P2Skudd);
  134. slutt.rise(&avslutt);
  135.  
  136. cout << "1: 200ms " << endl;
  137. cout << "2: 250ms" << endl;
  138. cout << "3: 500ms" << endl;
  139. start:
  140. cout << "Tast inn vanskelighetsgrad: ";
  141. cin >> vanskelighetsgrad;
  142. cout << vanskelighetsgrad << endl;
  143.  
  144. if(vanskelighetsgrad == 1) {
  145. ventetid = 200;
  146. } else if (vanskelighetsgrad == 2) {
  147. ventetid = 250;
  148. } else if (vanskelighetsgrad == 3) {
  149. ventetid = 500;
  150. } else {
  151. cout << "Vennligst velg et tall mellom 1-3." << endl;
  152. goto start;
  153. }
  154.  
  155. while(1) {
  156. P1Serve:
  157. if(spillTapt) {
  158. break;
  159. } else {
  160. P1Avspark ();
  161.  
  162. P2Timer = true;
  163. wait_ms(ventetid);
  164. P2Timer = false;
  165. if (!P2Truffet) {
  166. tap(P2);
  167. break;
  168. }
  169. }
  170.  
  171. P2Serve:
  172. if(spillTapt) {
  173. break;
  174. } else {
  175. P2Avspark ();
  176.  
  177. P1Timer = true;
  178. wait_ms(ventetid);
  179. P1Timer = false;
  180. if (!P1Truffet) {
  181. tap(P1);
  182. break;
  183. }
  184. }
  185. }
  186. cout << "Ute av loop" << endl;
  187. spillTapt = false;
  188. cout << "Spill tapt: " << spillTapt << endl;
  189.  
  190. if(serveside == P1) {
  191. serveside = P2;
  192. goto P1Serve;
  193. } else {
  194. serveside = P1;
  195. goto P2Serve;
  196. }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement