Beleg_3.ino 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. Arduino Spiel
  3. Autor: Johannes Tümler
  4. Letzte Änderung: 20.05.2022
  5. Zweck: Beleg 3 Hilfscode für einen leichteren Einstieg
  6. */
  7. enum Menu{
  8. MENU0,
  9. MENU1,
  10. MENU2,
  11. MENU3,
  12. MENU4,
  13. MENU5,
  14. MENU6,
  15. MENU7
  16. };
  17. const int red = 13, blue = 12, green = 11;
  18. int num=0;int num1=0;
  19. int currentOrder = 0;
  20. Menu menu = MENU0;
  21. const int startPin = 1;
  22. const int endPin = 9;
  23. const int maxAmount = 20;
  24. int numList[maxAmount];
  25. void setup()
  26. {
  27. pinMode(red, OUTPUT);
  28. pinMode(green, OUTPUT);
  29. pinMode(blue, OUTPUT);
  30. pinMode(10, INPUT_PULLUP);
  31. pinMode(9, INPUT_PULLUP);
  32. pinMode(8, INPUT_PULLUP);
  33. pinMode(7, INPUT_PULLUP);
  34. pinMode(6, INPUT_PULLUP);
  35. pinMode(5, INPUT_PULLUP);
  36. pinMode(4, INPUT_PULLUP);
  37. pinMode(3, INPUT_PULLUP);
  38. pinMode(2, INPUT_PULLUP);
  39. pinMode(1, INPUT_PULLUP);
  40. // Serial.begin(9600);
  41. resetAllPins();
  42. }
  43. void resetAllPins()
  44. {
  45. digitalWrite(red, LOW);
  46. digitalWrite(green, LOW);
  47. digitalWrite(blue, LOW);
  48. digitalWrite(1, HIGH);
  49. digitalWrite(2, HIGH);
  50. digitalWrite(3, HIGH);
  51. digitalWrite(4, HIGH);
  52. digitalWrite(5, HIGH);
  53. digitalWrite(6, HIGH);
  54. digitalWrite(7, HIGH);
  55. digitalWrite(8, HIGH);
  56. digitalWrite(9, HIGH);
  57. digitalWrite(10, HIGH);
  58. }
  59. void blink(const int *pin)
  60. {
  61. digitalWrite(*pin, HIGH);
  62. delay(500);
  63. digitalWrite(*pin, LOW);
  64. delay(500);
  65. }
  66. void loop()
  67. {
  68. switch(menu){
  69. case MENU0:
  70. // Serial.print("Menu 0\n");
  71. currentOrder = 0;
  72. for (int i=0; i<maxAmount; i++) {
  73. numList[i] = 0;
  74. }
  75. if (digitalRead(2) == LOW){
  76. menu=MENU2;
  77. digitalWrite(red, HIGH);
  78. digitalWrite(green, HIGH);
  79. digitalWrite(blue, HIGH);
  80. delay(1000);
  81. digitalWrite(red, LOW);
  82. digitalWrite(green, LOW);
  83. digitalWrite(blue, LOW);
  84. delay(500);
  85. }
  86. else if (digitalRead(3) == LOW){
  87. menu=MENU3;
  88. digitalWrite(red, HIGH);
  89. digitalWrite(green, HIGH);
  90. digitalWrite(blue, HIGH);
  91. delay(1000);
  92. digitalWrite(red, LOW);
  93. digitalWrite(green, LOW);
  94. digitalWrite(blue, LOW);
  95. delay(500);
  96. }
  97. else {
  98. blink(&red);
  99. blink(&red);
  100. blink(&red);
  101. blink(&green);
  102. blink(&green);
  103. }
  104. break;
  105. case MENU2:
  106. // Serial.print("Menu 2\n");
  107. digitalWrite(green, HIGH);
  108. for(int i=startPin;i<=endPin;i++){
  109. if (digitalRead(i) == LOW){
  110. numList[currentOrder]=i;
  111. currentOrder++;
  112. for(int k=0;k<maxAmount;k++){
  113. // Serial.print(numList[k]);
  114. // Serial.print(" ");
  115. }
  116. for (int a=0;a<i;a++)
  117. {blink(&green);}
  118. menu=MENU4;
  119. break;
  120. }
  121. }
  122. delay(1000);
  123. break;
  124. case MENU3:
  125. digitalWrite(green, HIGH);
  126. for(int i=1;i<10;i++){
  127. if (digitalRead(i) == LOW){
  128. num=i;menu=MENU5;
  129. for(int a=0;a<i;a++){
  130. blink(&green);
  131. }
  132. }
  133. }
  134. break;
  135. case MENU4:
  136. // Serial.print("Menu 4\n");
  137. bool oneNumberCorrect = true;
  138. bool allNumberCorrect = true;
  139. digitalWrite(blue, HIGH);
  140. for(int i=0;i<currentOrder;i++) {
  141. bool pinInserted = false;
  142. // Serial.print("Current position ");
  143. // Serial.print(i);
  144. // Serial.print("\n");
  145. while(true){
  146. for(int j=startPin;j<=endPin;j++){
  147. if(digitalRead(j) == LOW ){
  148. pinInserted = true;
  149. if(j != numList[i]){
  150. // Serial.print("The number is wrong\n");
  151. oneNumberCorrect = false;
  152. break;
  153. }
  154. // Serial.print("The number is correct\n");
  155. break;
  156. }
  157. }
  158. if(pinInserted) {
  159. break;
  160. }
  161. delay(1000);
  162. }
  163. allNumberCorrect = allNumberCorrect && oneNumberCorrect;
  164. if(!oneNumberCorrect){
  165. menu=MENU0;
  166. break;
  167. }
  168. delay(1000);
  169. }
  170. if(allNumberCorrect){
  171. menu=MENU2;
  172. }
  173. break;
  174. case MENU5:
  175. num1=random(1,9);
  176. for (int a=0;a<num1;a++){
  177. blink(&red);
  178. }
  179. menu=MENU6;
  180. break;
  181. case MENU6:
  182. digitalWrite(blue, HIGH);
  183. for(int i=startPin;i<=endPin;i++){
  184. if (digitalRead(i) == LOW){
  185. if (num==i){
  186. menu=MENU7;
  187. delay(1000);
  188. }
  189. }
  190. else {
  191. menu=MENU0;
  192. delay(1000);
  193. }
  194. }
  195. break;
  196. case MENU7:
  197. for(int i=startPin;i<=endPin;i++){
  198. if (digitalRead(i) == LOW){
  199. if (num1==i){
  200. menu=MENU3;
  201. delay(1000);
  202. }
  203. else{
  204. menu=MENU0;
  205. delay(1000);
  206. }
  207. }
  208. }
  209. break;
  210. default:
  211. break;
  212. }
  213. resetAllPins();
  214. }