/* 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(); }