Beleg3INFO.ino 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*
  2. Arduino Spiel
  3. Autor: Johannes Tümler, Janek Franz, Marvin Richter
  4. Letzte Änderung: 14.07.2020
  5. Zweck: Beleg 3 Hilfscode für den Start
  6. */
  7. unsigned char gesamtpunktzahl;
  8. byte x;
  9. byte y;
  10. byte lauf;
  11. byte xsetz;
  12. byte ysetz;
  13. byte bew;
  14. const int red = 13, blue = 11, green = 12;
  15. void setup()
  16. {
  17. Serial.begin(9600);
  18. gesamtpunktzahl = 0;
  19. randomSeed(analogRead(0));
  20. pinMode(red, OUTPUT);
  21. pinMode(green, OUTPUT);
  22. pinMode(blue, OUTPUT);
  23. pinMode(10, INPUT_PULLUP);
  24. pinMode(9, INPUT_PULLUP);
  25. pinMode(8, INPUT_PULLUP);
  26. pinMode(7, INPUT_PULLUP);
  27. pinMode(6, INPUT_PULLUP);
  28. pinMode(5, INPUT_PULLUP);
  29. pinMode(4, INPUT_PULLUP);
  30. pinMode(3, INPUT_PULLUP);
  31. pinMode(2, INPUT_PULLUP);
  32. pinMode(1, INPUT_PULLUP);
  33. pinMode(0, INPUT_PULLUP);
  34. resetAllPins();
  35. }
  36. void resetAllPins()
  37. {
  38. // alle Pins auf definierte Werte setzen (sonst wissen wir nicht, welchen Wert sie gerade haben.)
  39. digitalWrite(red, LOW);
  40. digitalWrite(green, LOW);
  41. digitalWrite(blue, LOW);
  42. // Alle nachfolgenden Pins werden auf HIGH gesetzt, sodass sie auf ein GND/Masse-Signal reagieren können.
  43. digitalWrite(0, HIGH);
  44. digitalWrite(1, HIGH);
  45. digitalWrite(2, HIGH);
  46. digitalWrite(3, HIGH);
  47. digitalWrite(4, HIGH);
  48. digitalWrite(5, HIGH);
  49. digitalWrite(6, HIGH);
  50. digitalWrite(7, HIGH);
  51. digitalWrite(8, HIGH);
  52. digitalWrite(9, HIGH);
  53. digitalWrite(10, HIGH);
  54. }
  55. void blink(const int *pin)
  56. {
  57. digitalWrite(*pin, HIGH);
  58. delay(500);
  59. digitalWrite(*pin, LOW);
  60. delay(500);
  61. }
  62. void loop()
  63. {
  64. if(digitalRead(0) == HIGH)
  65. {
  66. blink(&green);
  67. blink(&green);
  68. blink(&red);
  69. blink(&red);
  70. blink(&red);
  71. x = 0;
  72. y = 0;
  73. digitalWrite(0,LOW);
  74. }
  75. if (digitalRead(2) == LOW)
  76. {
  77. digitalWrite(2,HIGH);
  78. level1();
  79. }
  80. if (digitalRead(3) == LOW)
  81. {
  82. digitalWrite(3,HIGH);
  83. level2();
  84. }
  85. if (digitalRead(5) == LOW) // Hier im Beispiel: Wenn ein Masse-Signal / GND an Pin 5 liegt, dann blinkt es.
  86. {
  87. blink(&red);
  88. digitalWrite(5, HIGH);
  89. }
  90. resetAllPins();
  91. }
  92. void level1()
  93. {
  94. Serial.println("Level 1");
  95. if(x == 0 && y == 0){
  96. x = random(1,6);
  97. y = random(1,6);
  98. Serial.println(String(x));
  99. Serial.println(String(y));
  100. }
  101. coordsx1();
  102. }
  103. void level2()
  104. {
  105. Serial.print("Level 2");
  106. if(x == 0 && y == 0){
  107. x = random(1,6);
  108. y = random(1,6);
  109. }
  110. digitalWrite(3,HIGH);
  111. Serial.println("Anfangs Koords");
  112. Serial.println(String(x));
  113. Serial.println(String(y));
  114. coordsx2();
  115. }
  116. void coordsx1()
  117. {
  118. while (xsetz == 0){
  119. digitalWrite(green, HIGH);
  120. if (digitalRead(6) == LOW)
  121. {
  122. digitalWrite(green, LOW);
  123. xsetz = 1;
  124. delay(500);
  125. coordsy1();
  126. }
  127. if (digitalRead(7) == LOW)
  128. {
  129. digitalWrite(green, LOW);
  130. xsetz = 2;
  131. delay(500);
  132. coordsy1();
  133. }
  134. if (digitalRead(8) == LOW)
  135. {
  136. digitalWrite(green, LOW);
  137. xsetz = 3;
  138. delay(500);
  139. coordsy1();
  140. }
  141. if (digitalRead(9) == LOW)
  142. {
  143. digitalWrite(green, LOW);
  144. xsetz = 4;
  145. delay(500);
  146. coordsy1();
  147. }
  148. if (digitalRead(10) == LOW)
  149. {
  150. digitalWrite(green, LOW);
  151. xsetz = 5;
  152. delay(500);
  153. coordsy1();
  154. }
  155. }
  156. }
  157. void coordsy1()
  158. {
  159. digitalWrite(blue, HIGH);
  160. while (ysetz == 0){
  161. if (digitalRead(6) == LOW)
  162. {
  163. digitalWrite(blue, LOW);
  164. ysetz = 1;
  165. }
  166. if (digitalRead(7) == LOW)
  167. {
  168. digitalWrite(blue, LOW);
  169. ysetz = 2;
  170. }
  171. if (digitalRead(8) == LOW)
  172. {
  173. digitalWrite(blue, LOW);
  174. ysetz = 3;
  175. }
  176. if (digitalRead(9) == LOW)
  177. {
  178. digitalWrite(blue, LOW);
  179. ysetz = 4;
  180. }
  181. if (digitalRead(10) == LOW)
  182. {
  183. digitalWrite(blue, LOW);
  184. ysetz = 5;
  185. }
  186. Serial.println(String(xsetz));
  187. Serial.println(String(ysetz));
  188. }
  189. delay(1000);
  190. digitalWrite(blue,HIGH);
  191. digitalWrite(green,HIGH);
  192. delay(200);
  193. digitalWrite(blue,LOW);
  194. digitalWrite(green,LOW);
  195. posi1();
  196. }
  197. void posi1()
  198. {
  199. gesamtpunktzahl ++;
  200. if(x == xsetz && y == ysetz)
  201. {
  202. Serial.print("Treffer");
  203. xsetz = 0;
  204. ysetz = 0;
  205. digitalWrite(2, HIGH);
  206. digitalWrite(6, HIGH);
  207. digitalWrite(7, HIGH);
  208. digitalWrite(8, HIGH);
  209. digitalWrite(9, HIGH);
  210. digitalWrite(10, HIGH);
  211. gewonnen();
  212. }
  213. else if((x + 1 == xsetz or x - 1 == xsetz or x == xsetz)&& (y == ysetz or y + 1 == ysetz or y - 1 == ysetz))
  214. {
  215. digitalWrite(red, HIGH);
  216. Serial.print("1");
  217. delay(2000);
  218. xsetz = 0;
  219. ysetz = 0;
  220. digitalWrite(2,HIGH);
  221. digitalWrite(6, HIGH);
  222. digitalWrite(7, HIGH);
  223. digitalWrite(8, HIGH);
  224. digitalWrite(9, HIGH);
  225. digitalWrite(10, HIGH);
  226. digitalWrite(red, LOW);
  227. coordsx1();
  228. }
  229. else if((x + 2 == xsetz or x - 2 == xsetz or x == xsetz)&& (y == ysetz or y + 2 == ysetz or y - 2 == ysetz))
  230. {
  231. digitalWrite(blue, HIGH);
  232. Serial.print("2");
  233. delay(2000);
  234. xsetz = 0;
  235. ysetz = 0;
  236. digitalWrite(2, HIGH);
  237. digitalWrite(6, HIGH);
  238. digitalWrite(7, HIGH);
  239. digitalWrite(8, HIGH);
  240. digitalWrite(9, HIGH);
  241. digitalWrite(10, HIGH);
  242. digitalWrite(blue, LOW);
  243. coordsx1();
  244. }
  245. else if ((x + 2 >= xsetz or x - 2 <= xsetz or x == xsetz)&& (y == ysetz or y + 2 >= ysetz or y - 2 <= ysetz))
  246. {
  247. xsetz = 0;
  248. ysetz = 0;
  249. digitalWrite(2, HIGH);
  250. digitalWrite(6, HIGH);
  251. digitalWrite(7, HIGH);
  252. digitalWrite(8, HIGH);
  253. digitalWrite(9, HIGH);
  254. digitalWrite(10, HIGH);
  255. coordsx1();
  256. }
  257. }
  258. void gewonnen()
  259. {
  260. for(byte lauf = 3; lauf >= 1;lauf--)
  261. {
  262. digitalWrite(red,HIGH);
  263. delay(100);
  264. digitalWrite(red,LOW);
  265. digitalWrite(green, HIGH);
  266. delay(100);
  267. digitalWrite(green,LOW);
  268. digitalWrite(blue,HIGH);
  269. delay(100);
  270. digitalWrite(blue, LOW);
  271. }
  272. for(gesamtpunktzahl; gesamtpunktzahl >= 1;gesamtpunktzahl --)
  273. {
  274. digitalWrite(red,HIGH);
  275. digitalWrite(green,HIGH);
  276. digitalWrite(blue,HIGH);
  277. delay(300);
  278. digitalWrite(red,LOW);
  279. digitalWrite(green,LOW);
  280. digitalWrite(blue, LOW);
  281. delay(300);
  282. }
  283. loop();
  284. }
  285. void coordsx2()
  286. {
  287. while (xsetz == 0){
  288. digitalWrite(green, HIGH);
  289. if (digitalRead(6) == LOW)
  290. {
  291. digitalWrite(green, LOW);
  292. xsetz = 1;
  293. delay(500);
  294. coordsy2();
  295. }
  296. if (digitalRead(7) == LOW)
  297. {
  298. digitalWrite(green, LOW);
  299. xsetz = 2;
  300. delay(500);
  301. coordsy2();
  302. }
  303. if (digitalRead(8) == LOW)
  304. {
  305. digitalWrite(green, LOW);
  306. xsetz = 3;
  307. delay(500);
  308. coordsy2();
  309. }
  310. if (digitalRead(9) == LOW)
  311. {
  312. digitalWrite(green, LOW);
  313. xsetz = 4;
  314. delay(500);
  315. coordsy2();
  316. }
  317. if (digitalRead(10) == LOW)
  318. {
  319. digitalWrite(green, LOW);
  320. xsetz = 5;
  321. delay(500);
  322. coordsy2();
  323. }
  324. }
  325. }
  326. void coordsy2()
  327. {
  328. digitalWrite(blue, HIGH);
  329. while (ysetz == 0){
  330. if (digitalRead(6) == LOW)
  331. {
  332. digitalWrite(blue, LOW);
  333. ysetz = 1;
  334. }
  335. if (digitalRead(7) == LOW)
  336. {
  337. digitalWrite(blue, LOW);
  338. ysetz = 2;
  339. }
  340. if (digitalRead(8) == LOW)
  341. {
  342. digitalWrite(blue, LOW);
  343. ysetz = 3;
  344. }
  345. if (digitalRead(9) == LOW)
  346. {
  347. digitalWrite(blue, LOW);
  348. ysetz = 4;
  349. }
  350. if (digitalRead(10) == LOW)
  351. {
  352. digitalWrite(blue, LOW);
  353. ysetz = 5;
  354. }
  355. Serial.println(String(xsetz));
  356. Serial.println(String(ysetz));
  357. }
  358. delay(1000);
  359. digitalWrite(blue,HIGH);
  360. digitalWrite(green,HIGH);
  361. delay(200);
  362. digitalWrite(blue,LOW);
  363. digitalWrite(green,LOW);
  364. posi2();
  365. }
  366. void posi2()
  367. {
  368. gesamtpunktzahl ++;
  369. if(x == xsetz && y == ysetz)
  370. {
  371. Serial.print("Treffer");
  372. xsetz = 0;
  373. ysetz = 0;
  374. digitalWrite(2, HIGH);
  375. digitalWrite(6, HIGH);
  376. digitalWrite(7, HIGH);
  377. digitalWrite(8, HIGH);
  378. digitalWrite(9, HIGH);
  379. digitalWrite(10, HIGH);
  380. gewonnen();
  381. }
  382. else if((x + 1 == xsetz or x - 1 == xsetz or x == xsetz)&& (y == ysetz or y + 1 == ysetz or y - 1 == ysetz))
  383. {
  384. digitalWrite(red, HIGH);
  385. Serial.print("1");
  386. delay(2000);
  387. xsetz = 0;
  388. ysetz = 0;
  389. digitalWrite(2,HIGH);
  390. digitalWrite(6, HIGH);
  391. digitalWrite(7, HIGH);
  392. digitalWrite(8, HIGH);
  393. digitalWrite(9, HIGH);
  394. digitalWrite(10, HIGH);
  395. digitalWrite(red, LOW);
  396. bewegung();
  397. }
  398. else if((x + 2 == xsetz or x - 2 == xsetz or x == xsetz)&& (y == ysetz or y + 2 == ysetz or y - 2 == ysetz))
  399. {
  400. digitalWrite(blue, HIGH);
  401. Serial.print("2");
  402. delay(2000);
  403. xsetz = 0;
  404. ysetz = 0;
  405. digitalWrite(2, HIGH);
  406. digitalWrite(6, HIGH);
  407. digitalWrite(7, HIGH);
  408. digitalWrite(8, HIGH);
  409. digitalWrite(9, HIGH);
  410. digitalWrite(10, HIGH);
  411. digitalWrite(blue, LOW);
  412. bewegung();
  413. }
  414. else if ((x + 2 >= xsetz or x - 2 <= xsetz or x == xsetz)&& (y == ysetz or y + 2 >= ysetz or y - 2 <= ysetz))
  415. {
  416. xsetz = 0;
  417. ysetz = 0;
  418. digitalWrite(2, HIGH);
  419. digitalWrite(6, HIGH);
  420. digitalWrite(7, HIGH);
  421. digitalWrite(8, HIGH);
  422. digitalWrite(9, HIGH);
  423. digitalWrite(10, HIGH);
  424. bewegung();
  425. }
  426. }
  427. void bewegung()
  428. {
  429. bew = random(1,9);
  430. if (bew == 1)
  431. {
  432. x++;
  433. y++;
  434. if ((x > 5 or x < 1) or (y > 5 or y < 1))
  435. {
  436. x--;
  437. y--;
  438. bewegung();
  439. }
  440. }
  441. else if (bew == 2)
  442. {
  443. x++;
  444. y--;
  445. if ((x > 5 or x < 1) or (y > 5 or y < 1))
  446. {
  447. x--;
  448. y++;
  449. bewegung();
  450. }
  451. }
  452. else if (bew == 3)
  453. {
  454. x--;
  455. y++;
  456. if ((x > 5 or x < 1) or (y > 5 or y < 1))
  457. {
  458. x++;
  459. y--;
  460. bewegung();
  461. }
  462. }
  463. else if (bew == 4)
  464. {
  465. x--;
  466. y--;
  467. if ((x > 5 or x < 1) or (y > 5 or y < 1))
  468. {
  469. x++;
  470. y++;
  471. bewegung();
  472. }
  473. }
  474. else if (bew == 5)
  475. {
  476. x++;
  477. if ((x > 5 or x < 1) or (y > 5 or y < 1))
  478. {
  479. x--;
  480. bewegung();
  481. }
  482. }
  483. else if (bew == 6)
  484. {
  485. y++;
  486. if ((x > 5 or x < 1) or (y > 5 or y < 1))
  487. {
  488. y--;
  489. bewegung();
  490. }
  491. }
  492. else if (bew == 7)
  493. {
  494. x--;
  495. if ((x > 5 or x < 1) or (y > 5 or y < 1))
  496. {
  497. x++;
  498. bewegung();
  499. }
  500. }
  501. else if (bew == 8)
  502. {
  503. y--;
  504. if ((x > 5 or x < 1) or (y > 5 or y < 1))
  505. {
  506. y++;
  507. bewegung();
  508. }
  509. }
  510. Serial.print("Neue Koords");
  511. Serial.println(String(x));
  512. Serial.println(String(y));
  513. coordsx2();
  514. }