miaoyufeng vor 2 Jahren
Ursprung
Commit
5c1ffcfe1d
1 geänderte Dateien mit 219 neuen und 0 gelöschten Zeilen
  1. 219 0
      Beleg_3.ino

+ 219 - 0
Beleg_3.ino

@@ -0,0 +1,219 @@
+/*
+ Arduino Spiel
+ Autor: Johannes Tümler
+ Letzte Änderung: 20.05.2022
+ Zweck: Beleg 3 Hilfscode für einen leichteren Einstieg
+*/
+enum Menu{
+  MENU0,
+  MENU1,
+  MENU2,
+  MENU3,
+  MENU4,
+  MENU5,
+  MENU6,
+  MENU7
+};
+const int red = 13, blue = 12, green = 11;
+int num=0;int num1=0;
+int currentOrder = 0;
+Menu menu = MENU0;
+const int startPin = 1; 
+const int endPin = 9;
+const int maxAmount = 20;
+int numList[maxAmount];
+
+void setup()
+{
+    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);
+    // Serial.begin(9600);
+    resetAllPins();
+   
+}
+
+void resetAllPins()
+{
+    digitalWrite(red, LOW);
+    digitalWrite(green, LOW);
+    digitalWrite(blue, LOW);
+    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()
+{
+  switch(menu){
+    case MENU0:
+      // Serial.print("Menu 0\n");
+      currentOrder = 0;
+      for (int i=0; i<maxAmount; i++) {
+        numList[i] = 0;
+      }
+      if (digitalRead(2) == LOW){
+        menu=MENU2;
+        digitalWrite(red, HIGH);
+        digitalWrite(green, HIGH);
+        digitalWrite(blue, HIGH);
+        delay(1000);
+        digitalWrite(red, LOW);
+        digitalWrite(green, LOW);
+        digitalWrite(blue, LOW);
+        delay(500);   
+      }
+      else if (digitalRead(3) == LOW){
+        menu=MENU3;
+        digitalWrite(red, HIGH);
+        digitalWrite(green, HIGH);
+        digitalWrite(blue, HIGH);
+        delay(1000);
+        digitalWrite(red, LOW);
+        digitalWrite(green, LOW);
+        digitalWrite(blue, LOW);
+        delay(500);   
+      }
+      else {
+        blink(&red);
+        blink(&red);
+        blink(&red);
+        blink(&green);
+        blink(&green);
+      }
+      break;
+    case MENU2:
+    // Serial.print("Menu 2\n");
+      digitalWrite(green, HIGH);
+      for(int i=startPin;i<=endPin;i++){  
+        if (digitalRead(i) == LOW){
+          numList[currentOrder]=i;
+          currentOrder++;
+          for(int k=0;k<maxAmount;k++){
+            // Serial.print(numList[k]);
+            // Serial.print(" ");
+          }
+          for (int a=0;a<i;a++)
+            {blink(&green);} 
+          menu=MENU4; 
+          break;            
+        } 
+      }
+      delay(1000); 
+      break;
+    case MENU3:
+      digitalWrite(green, HIGH);
+      for(int i=1;i<10;i++){  
+        if (digitalRead(i) == LOW){
+          num=i;menu=MENU5;
+          for(int a=0;a<i;a++){
+            blink(&green);
+          }
+        }
+      }
+      break;
+    case MENU4:
+      // Serial.print("Menu 4\n");
+      bool oneNumberCorrect = true;
+      bool allNumberCorrect = true;
+      digitalWrite(blue, HIGH);
+      for(int i=0;i<currentOrder;i++) {
+        bool pinInserted = false;
+        // Serial.print("Current position ");
+        // Serial.print(i);
+        // Serial.print("\n");
+        while(true){
+          for(int j=startPin;j<=endPin;j++){
+            if(digitalRead(j) == LOW ){
+              pinInserted = true;
+              if(j != numList[i]){
+                  // Serial.print("The number is wrong\n");
+                  oneNumberCorrect = false;
+                  break;
+              }
+              // Serial.print("The number is correct\n");
+              break;
+            }
+          }
+          if(pinInserted) {
+            break; 
+          } 
+          delay(1000);
+        }
+        allNumberCorrect = allNumberCorrect && oneNumberCorrect;
+        if(!oneNumberCorrect){
+          menu=MENU0;
+          break;
+        }
+        delay(1000);
+      }
+      if(allNumberCorrect){
+        menu=MENU2;
+      }
+      break; 
+    case MENU5:
+      num1=random(1,9);
+      for (int a=0;a<num1;a++){
+        blink(&red);
+      }
+      menu=MENU6;
+      break;
+    case MENU6:
+      digitalWrite(blue, HIGH);
+      for(int i=startPin;i<=endPin;i++){  
+        if (digitalRead(i) == LOW){
+          if (num==i){
+            menu=MENU7;
+            delay(1000);
+          } 
+        }
+        else {
+          menu=MENU0;
+          delay(1000);
+        }
+      }
+      break;
+    case MENU7:
+      for(int i=startPin;i<=endPin;i++){  
+        if (digitalRead(i) == LOW){
+          if (num1==i){
+            menu=MENU3;
+            delay(1000);
+          } 
+          else{
+            menu=MENU0;
+            delay(1000);
+          }
+         }
+      }
+      break;
+    default:
+      break;
+  } 
+    resetAllPins();    
+}