123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597 |
- /*
- Arduino Spiel
- Autor: Johannes Tümler, Janek Franz, Marvin Richter
- Letzte Änderung: 14.07.2020
- Zweck: Beleg 3 Hilfscode für den Start
- */
- unsigned char gesamtpunktzahl;
- byte x;
- byte y;
- byte lauf;
- byte xsetz;
- byte ysetz;
- byte bew;
- const int red = 13, blue = 11, green = 12;
- void setup()
- {
- Serial.begin(9600);
- gesamtpunktzahl = 0;
- randomSeed(analogRead(0));
- pinMode(red, OUTPUT);
- pinMode(green, OUTPUT);
- pinMode(blue, OUTPUT);
- pinMode(10, INPUT_PULLUP);
- pinMode(9, INPUT_PULLUP);
- pinMode(8, INPUT_PULLUP);
- pinMode(7, INPUT_PULLUP);
- pinMode(6, INPUT_PULLUP);
- pinMode(5, INPUT_PULLUP);
- pinMode(4, INPUT_PULLUP);
- pinMode(3, INPUT_PULLUP);
- pinMode(2, INPUT_PULLUP);
- pinMode(1, INPUT_PULLUP);
- pinMode(0, INPUT_PULLUP);
- resetAllPins();
- }
- void resetAllPins()
- {
- // alle Pins auf definierte Werte setzen (sonst wissen wir nicht, welchen Wert sie gerade haben.)
- digitalWrite(red, LOW);
- digitalWrite(green, LOW);
- digitalWrite(blue, LOW);
- // Alle nachfolgenden Pins werden auf HIGH gesetzt, sodass sie auf ein GND/Masse-Signal reagieren können.
- digitalWrite(0, HIGH);
- digitalWrite(1, HIGH);
- digitalWrite(2, HIGH);
- digitalWrite(3, HIGH);
- digitalWrite(4, HIGH);
- digitalWrite(5, HIGH);
- digitalWrite(6, HIGH);
- digitalWrite(7, HIGH);
- digitalWrite(8, HIGH);
- digitalWrite(9, HIGH);
- digitalWrite(10, HIGH);
- }
- void blink(const int *pin)
- {
- digitalWrite(*pin, HIGH);
- delay(500);
- digitalWrite(*pin, LOW);
- delay(500);
- }
- void loop()
- {
- if(digitalRead(0) == HIGH)
- {
- blink(&green);
- blink(&green);
- blink(&red);
- blink(&red);
- blink(&red);
- x = 0;
- y = 0;
- digitalWrite(0,LOW);
-
- }
-
- if (digitalRead(2) == LOW)
- {
- digitalWrite(2,HIGH);
- level1();
-
- }
- if (digitalRead(3) == LOW)
- {
- digitalWrite(3,HIGH);
- level2();
-
-
- }
-
- if (digitalRead(5) == LOW) // Hier im Beispiel: Wenn ein Masse-Signal / GND an Pin 5 liegt, dann blinkt es.
- {
- blink(&red);
- digitalWrite(5, HIGH);
- }
- resetAllPins();
- }
- void level1()
- {
- Serial.println("Level 1");
- if(x == 0 && y == 0){
- x = random(1,6);
- y = random(1,6);
- Serial.println(String(x));
- Serial.println(String(y));
- }
- coordsx1();
-
-
- }
- void level2()
- {
- Serial.print("Level 2");
- if(x == 0 && y == 0){
- x = random(1,6);
- y = random(1,6);
- }
- digitalWrite(3,HIGH);
- Serial.println("Anfangs Koords");
- Serial.println(String(x));
- Serial.println(String(y));
- coordsx2();
- }
- void coordsx1()
- {
-
- while (xsetz == 0){
- digitalWrite(green, HIGH);
- if (digitalRead(6) == LOW)
- {
- digitalWrite(green, LOW);
- xsetz = 1;
- delay(500);
- coordsy1();
- }
-
- if (digitalRead(7) == LOW)
- {
- digitalWrite(green, LOW);
- xsetz = 2;
- delay(500);
- coordsy1();
- }
-
-
- if (digitalRead(8) == LOW)
- {
- digitalWrite(green, LOW);
- xsetz = 3;
- delay(500);
- coordsy1();
- }
-
- if (digitalRead(9) == LOW)
- {
- digitalWrite(green, LOW);
- xsetz = 4;
- delay(500);
- coordsy1();
- }
-
- if (digitalRead(10) == LOW)
- {
- digitalWrite(green, LOW);
- xsetz = 5;
- delay(500);
- coordsy1();
- }
-
- }
- }
- void coordsy1()
- {
- digitalWrite(blue, HIGH);
- while (ysetz == 0){
- if (digitalRead(6) == LOW)
- {
- digitalWrite(blue, LOW);
- ysetz = 1;
-
- }
-
- if (digitalRead(7) == LOW)
- {
- digitalWrite(blue, LOW);
- ysetz = 2;
-
- }
-
-
- if (digitalRead(8) == LOW)
- {
- digitalWrite(blue, LOW);
- ysetz = 3;
-
- }
-
- if (digitalRead(9) == LOW)
- {
- digitalWrite(blue, LOW);
- ysetz = 4;
-
- }
-
- if (digitalRead(10) == LOW)
- {
- digitalWrite(blue, LOW);
- ysetz = 5;
-
- }
- Serial.println(String(xsetz));
- Serial.println(String(ysetz));
- }
- delay(1000);
- digitalWrite(blue,HIGH);
- digitalWrite(green,HIGH);
- delay(200);
- digitalWrite(blue,LOW);
- digitalWrite(green,LOW);
- posi1();
- }
- void posi1()
- {
- gesamtpunktzahl ++;
- if(x == xsetz && y == ysetz)
- {
- Serial.print("Treffer");
- xsetz = 0;
- ysetz = 0;
- digitalWrite(2, HIGH);
- digitalWrite(6, HIGH);
- digitalWrite(7, HIGH);
- digitalWrite(8, HIGH);
- digitalWrite(9, HIGH);
- digitalWrite(10, HIGH);
- gewonnen();
- }
- else if((x + 1 == xsetz or x - 1 == xsetz or x == xsetz)&& (y == ysetz or y + 1 == ysetz or y - 1 == ysetz))
- {
- digitalWrite(red, HIGH);
- Serial.print("1");
- delay(2000);
- xsetz = 0;
- ysetz = 0;
- digitalWrite(2,HIGH);
- digitalWrite(6, HIGH);
- digitalWrite(7, HIGH);
- digitalWrite(8, HIGH);
- digitalWrite(9, HIGH);
- digitalWrite(10, HIGH);
- digitalWrite(red, LOW);
- coordsx1();
- }
- else if((x + 2 == xsetz or x - 2 == xsetz or x == xsetz)&& (y == ysetz or y + 2 == ysetz or y - 2 == ysetz))
- {
- digitalWrite(blue, HIGH);
- Serial.print("2");
- delay(2000);
- xsetz = 0;
- ysetz = 0;
- digitalWrite(2, HIGH);
- digitalWrite(6, HIGH);
- digitalWrite(7, HIGH);
- digitalWrite(8, HIGH);
- digitalWrite(9, HIGH);
- digitalWrite(10, HIGH);
- digitalWrite(blue, LOW);
- coordsx1();
- }
- else if ((x + 2 >= xsetz or x - 2 <= xsetz or x == xsetz)&& (y == ysetz or y + 2 >= ysetz or y - 2 <= ysetz))
- {
- xsetz = 0;
- ysetz = 0;
- digitalWrite(2, HIGH);
- digitalWrite(6, HIGH);
- digitalWrite(7, HIGH);
- digitalWrite(8, HIGH);
- digitalWrite(9, HIGH);
- digitalWrite(10, HIGH);
- coordsx1();
- }
- }
- void gewonnen()
- {
-
- for(byte lauf = 3; lauf >= 1;lauf--)
- {
- digitalWrite(red,HIGH);
- delay(100);
- digitalWrite(red,LOW);
- digitalWrite(green, HIGH);
- delay(100);
- digitalWrite(green,LOW);
- digitalWrite(blue,HIGH);
- delay(100);
- digitalWrite(blue, LOW);
- }
- for(gesamtpunktzahl; gesamtpunktzahl >= 1;gesamtpunktzahl --)
- {
-
- digitalWrite(red,HIGH);
- digitalWrite(green,HIGH);
- digitalWrite(blue,HIGH);
- delay(300);
- digitalWrite(red,LOW);
- digitalWrite(green,LOW);
- digitalWrite(blue, LOW);
- delay(300);
-
- }
-
- loop();
- }
- void coordsx2()
- {
-
- while (xsetz == 0){
- digitalWrite(green, HIGH);
- if (digitalRead(6) == LOW)
- {
- digitalWrite(green, LOW);
- xsetz = 1;
- delay(500);
- coordsy2();
- }
-
- if (digitalRead(7) == LOW)
- {
- digitalWrite(green, LOW);
- xsetz = 2;
- delay(500);
- coordsy2();
- }
-
-
- if (digitalRead(8) == LOW)
- {
- digitalWrite(green, LOW);
- xsetz = 3;
- delay(500);
- coordsy2();
- }
-
- if (digitalRead(9) == LOW)
- {
- digitalWrite(green, LOW);
- xsetz = 4;
- delay(500);
- coordsy2();
- }
-
- if (digitalRead(10) == LOW)
- {
- digitalWrite(green, LOW);
- xsetz = 5;
- delay(500);
- coordsy2();
- }
-
- }
- }
- void coordsy2()
- {
- digitalWrite(blue, HIGH);
- while (ysetz == 0){
- if (digitalRead(6) == LOW)
- {
- digitalWrite(blue, LOW);
- ysetz = 1;
-
- }
-
- if (digitalRead(7) == LOW)
- {
- digitalWrite(blue, LOW);
- ysetz = 2;
-
- }
-
-
- if (digitalRead(8) == LOW)
- {
- digitalWrite(blue, LOW);
- ysetz = 3;
-
- }
-
- if (digitalRead(9) == LOW)
- {
- digitalWrite(blue, LOW);
- ysetz = 4;
-
- }
-
- if (digitalRead(10) == LOW)
- {
- digitalWrite(blue, LOW);
- ysetz = 5;
-
- }
- Serial.println(String(xsetz));
- Serial.println(String(ysetz));
- }
- delay(1000);
- digitalWrite(blue,HIGH);
- digitalWrite(green,HIGH);
- delay(200);
- digitalWrite(blue,LOW);
- digitalWrite(green,LOW);
- posi2();
- }
- void posi2()
- {
- gesamtpunktzahl ++;
- if(x == xsetz && y == ysetz)
- {
- Serial.print("Treffer");
- xsetz = 0;
- ysetz = 0;
- digitalWrite(2, HIGH);
- digitalWrite(6, HIGH);
- digitalWrite(7, HIGH);
- digitalWrite(8, HIGH);
- digitalWrite(9, HIGH);
- digitalWrite(10, HIGH);
- gewonnen();
- }
- else if((x + 1 == xsetz or x - 1 == xsetz or x == xsetz)&& (y == ysetz or y + 1 == ysetz or y - 1 == ysetz))
- {
- digitalWrite(red, HIGH);
- Serial.print("1");
- delay(2000);
- xsetz = 0;
- ysetz = 0;
- digitalWrite(2,HIGH);
- digitalWrite(6, HIGH);
- digitalWrite(7, HIGH);
- digitalWrite(8, HIGH);
- digitalWrite(9, HIGH);
- digitalWrite(10, HIGH);
- digitalWrite(red, LOW);
- bewegung();
- }
- else if((x + 2 == xsetz or x - 2 == xsetz or x == xsetz)&& (y == ysetz or y + 2 == ysetz or y - 2 == ysetz))
- {
- digitalWrite(blue, HIGH);
- Serial.print("2");
- delay(2000);
- xsetz = 0;
- ysetz = 0;
- digitalWrite(2, HIGH);
- digitalWrite(6, HIGH);
- digitalWrite(7, HIGH);
- digitalWrite(8, HIGH);
- digitalWrite(9, HIGH);
- digitalWrite(10, HIGH);
- digitalWrite(blue, LOW);
- bewegung();
- }
- else if ((x + 2 >= xsetz or x - 2 <= xsetz or x == xsetz)&& (y == ysetz or y + 2 >= ysetz or y - 2 <= ysetz))
- {
- xsetz = 0;
- ysetz = 0;
- digitalWrite(2, HIGH);
- digitalWrite(6, HIGH);
- digitalWrite(7, HIGH);
- digitalWrite(8, HIGH);
- digitalWrite(9, HIGH);
- digitalWrite(10, HIGH);
- bewegung();
- }
-
-
- }
- void bewegung()
- {
- bew = random(1,9);
- if (bew == 1)
- {
- x++;
- y++;
- if ((x > 5 or x < 1) or (y > 5 or y < 1))
- {
- x--;
- y--;
- bewegung();
- }
- }
- else if (bew == 2)
- {
- x++;
- y--;
- if ((x > 5 or x < 1) or (y > 5 or y < 1))
- {
- x--;
- y++;
- bewegung();
- }
- }
- else if (bew == 3)
- {
- x--;
- y++;
- if ((x > 5 or x < 1) or (y > 5 or y < 1))
- {
- x++;
- y--;
- bewegung();
- }
- }
- else if (bew == 4)
- {
- x--;
- y--;
- if ((x > 5 or x < 1) or (y > 5 or y < 1))
- {
- x++;
- y++;
- bewegung();
- }
- }
- else if (bew == 5)
- {
- x++;
- if ((x > 5 or x < 1) or (y > 5 or y < 1))
- {
- x--;
- bewegung();
- }
- }
- else if (bew == 6)
- {
- y++;
- if ((x > 5 or x < 1) or (y > 5 or y < 1))
- {
- y--;
- bewegung();
- }
- }
- else if (bew == 7)
- {
- x--;
- if ((x > 5 or x < 1) or (y > 5 or y < 1))
- {
- x++;
- bewegung();
- }
- }
- else if (bew == 8)
- {
- y--;
- if ((x > 5 or x < 1) or (y > 5 or y < 1))
- {
- y++;
- bewegung();
- }
- }
- Serial.print("Neue Koords");
- Serial.println(String(x));
- Serial.println(String(y));
- coordsx2();
- }
-
|