|
@@ -0,0 +1,179 @@
|
|
|
|
+/*
|
|
|
|
+ Arduino Spiel
|
|
|
|
+ Autor: Johannes Tümler
|
|
|
|
+ Letzte Änderung: 20.05.2022
|
|
|
|
+ Zweck: Beleg 3 Hilfscode für einen leichteren Einstieg
|
|
|
|
+*/
|
|
|
|
+
|
|
|
|
+unsigned char gesamtpunktzahl;
|
|
|
|
+const int red = 13, blue = 12, green = 11;
|
|
|
|
+int wahl;
|
|
|
|
+int eingabe[10];
|
|
|
|
+int rand(void);
|
|
|
|
+bool prufmode=false;
|
|
|
|
+bool gameover=false;
|
|
|
|
+
|
|
|
|
+void setup()
|
|
|
|
+{
|
|
|
|
+ gesamtpunktzahl = 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);
|
|
|
|
+ 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(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()
|
|
|
|
+{
|
|
|
|
+wahl=menu();
|
|
|
|
+ if(wahl==2)
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ game();}
|
|
|
|
+ else if (wahl==3)
|
|
|
|
+ {game();}
|
|
|
|
+ resetAllPins();
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+int menu() //Erstellung des Menue
|
|
|
|
+{
|
|
|
|
+
|
|
|
|
+ blink(&green); //Erscheinung der Leuchte
|
|
|
|
+ blink(&green);
|
|
|
|
+ blink(&red);
|
|
|
|
+ blink(&red);
|
|
|
|
+ blink(&red);
|
|
|
|
+ if (digitalRead(2) == LOW)
|
|
|
|
+ {
|
|
|
|
+ for(int i=1;i<=2;i++)
|
|
|
|
+ {digitalWrite(red, HIGH);digitalWrite(green, HIGH);digitalWrite(blue, HIGH);
|
|
|
|
+ delay(500);
|
|
|
|
+ digitalWrite(red, LOW);digitalWrite(green, LOW);digitalWrite(blue, LOW);
|
|
|
|
+ delay(500);
|
|
|
|
+ }
|
|
|
|
+ return 2;
|
|
|
|
+ }
|
|
|
|
+ else if (digitalRead(3) == LOW)
|
|
|
|
+ {
|
|
|
|
+ for(int i=1;i<=3;i++)
|
|
|
|
+ {digitalWrite(red, HIGH);digitalWrite(green, HIGH);digitalWrite(blue, HIGH);
|
|
|
|
+ delay(500);
|
|
|
|
+ digitalWrite(red, LOW);digitalWrite(green, LOW);digitalWrite(blue, LOW);
|
|
|
|
+ delay(500);}
|
|
|
|
+ return 3;
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {return 0;}
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void game()
|
|
|
|
+{ int j=0,k=0;
|
|
|
|
+ for(;;){
|
|
|
|
+
|
|
|
|
+ if(!prufmode)
|
|
|
|
+ {
|
|
|
|
+ digitalWrite(green, HIGH);
|
|
|
|
+ for(int i=1;i<=10;i++)
|
|
|
|
+ {if (digitalRead(i) == LOW)
|
|
|
|
+ {
|
|
|
|
+ for(int s=1;s<=i;s++){
|
|
|
|
+ blink(&green);
|
|
|
|
+ }
|
|
|
|
+ eingabe[j]=i;
|
|
|
|
+ blink(&green);
|
|
|
|
+ prufmode=true;
|
|
|
|
+ j++;k=0;
|
|
|
|
+ digitalWrite(green, LOW);
|
|
|
|
+ if(wahl==3){
|
|
|
|
+ int a=rand()%10+1;
|
|
|
|
+ eingabe[j]=a;
|
|
|
|
+ j++;k=0;
|
|
|
|
+ for(int i=1;i<=a;i++){
|
|
|
|
+ blink(&red);}
|
|
|
|
+ prufmode=true;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ /*困难模式*/
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {digitalWrite(green, LOW);digitalWrite(blue, HIGH);
|
|
|
|
+ for(int i=1;i<=10;i++)
|
|
|
|
+ {if (digitalRead(i) == LOW)
|
|
|
|
+ {
|
|
|
|
+ if(eingabe[k]==i)
|
|
|
|
+ {
|
|
|
|
+ digitalWrite(blue, LOW);delay(500);digitalWrite(blue, HIGH);
|
|
|
|
+ k++;
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {gameover=true;}
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if(k==j)
|
|
|
|
+ prufmode=false;
|
|
|
|
+ }
|
|
|
|
+ if(gameover)
|
|
|
|
+
|
|
|
|
+ { for(int i=1;i<=j;i++){
|
|
|
|
+ digitalWrite(red, HIGH);digitalWrite(green, HIGH);digitalWrite(blue, HIGH);
|
|
|
|
+ delay(500);
|
|
|
|
+ digitalWrite(red, LOW);digitalWrite(green, LOW);digitalWrite(blue, LOW);
|
|
|
|
+ delay(500);
|
|
|
|
+ }
|
|
|
|
+ for(int i=0;i>10;i++)
|
|
|
|
+ {eingabe[i]=0;}gameover=false;prufmode=false;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+resetAllPins();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|