vscode_arduino.ino 11 KB

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