Was hab ich bei dieser Arduinoprogrammierung falsch gemacht?

2 Antworten

Zunächst mal, das sieht der einen Aufgabe aus der Intel Leibniz challenge sehr ähnlich. Aber wie lange hast du basic programmiert, MANN BENUTZT KEINE GOTOs IN CPP. Die Taster bouncen, du brauchst noch ein debounce (google ist dein freund ;))

Das heißt dann so, oder was?:

#include <IOpins.h>

  const int S2 = IO27;      //Taster S2
  const int S3 = IO4;     //Taster S3
  const int S4 = IO5;     //Taster S4
  const int rot =  IO15;      // Rote LED
  const int gruen =  IO16;    //Grüne LED

  // Variablen für die Überprüfung, ob der Taster gedrückt("pushed") wurde:
  int S2State;                //Taster S2
  int S3State;                //Taster S3
  int S4State;                //Taster S4
  int rotState = HIGH;    // the current state of the output pin
  int gruenState = HIGH;  // the current state of the output pin
  int lastS2P=LOW;        //vorheriger Status des Tasters S2
  int lastS3P=LOW;        //vorheriger Status des Tasters S3
  int lastS4P=LOW;        //vorheriger Status des Tasters S4
  long lastDebounceTime = 0;  // the last time the output pin was toggled
  long debounceDelay = 50;    // the debounce time; increase if the output flickers
  long lastDebounceTime1 = 0;  // the last time the output pin was toggled
  long debounceDelay1 = 50;    // the debounce time; increase if the output flickers
  long lastDebounceTime2 = 0;  // the last time the output pin was toggled
  long debounceDelay2 = 50;    // the debounce time; increase if the output flickers

  void setup()
  {
  pinMode(rot, OUTPUT);  
  pinMode(gruen, OUTPUT);  
  pinMode(S2, INPUT);    
  pinMode(S3, INPUT); 
  pinMode(S4, INPUT); 
  }

  void loop()
  {
  // read the state of the switch into a local variable:
  int reading = digitalRead(S2);
  int reading1 = digitalRead(S3);
  int reading2 = digitalRead(S4);
  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:  
  // If the switch changed, due to noise or pressing:
  // set the LED using the state of the button:
  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  //LEDs ausschalten
  digitalWrite(rot, HIGH);
  digitalWrite(gruen, HIGH);
  digitalWrite(rot, LOW);
  digitalWrite(gruen, LOW);
  // Schauen ob der Taster S2 gedrückt ist
  // Wenn ja, dann ist buttonState = HIGH   
    if (reading != lastS2P) 
    {
        // reset the debouncing timer
        lastDebounceTime = millis();
          if ((millis() - lastDebounceTime) > debounceDelay) {
          // whatever the reading is at, it's been there for longer
          // than the debounce delay, so take it as the actual current state:
          S2State = reading;
            lastS2P = reading;
          }

           digitalWrite(rot, S2State); //Rote LED einschalten

             if (reading2 != lastS3P) 
                  {
                  // reset the debouncing timer
                  lastDebounceTime1 = millis();

                  if ((millis() - lastDebounceTime1) > debounceDelay1) {
                  // whatever the reading is at, it's been there for longer
                  // than the debounce delay, so take it as the actual current state:
                  S3State = reading1;
                  lastS3P = reading1;
                  }
                  digitalWrite(gruen, S3State); //Grüne LED einschalten
                  // Schauen ob der Taster S2 gedrückt ist
                  // Wenn ja, dann ist buttonState = HIGH

                       if (reading2 != lastS4P) 
                       {
                        // reset the debouncing timer
                        lastDebounceTime2 = millis();
                          if ((millis() - lastDebounceTime2) > debounceDelay2) {
                           // whatever the reading is at, it's been there for longer
                           // than the debounce delay, so take it as the actual current state:
                        S4State = reading2;
                        lastS4P = reading2;
                          }
                       }

                 }
           }
  }
0

Ich hab es übrigens noch geschafft, und zwar so:

  #include <IOpins.h>
   #define rot IO15
   #define gruen IO16
   #define taster IO27
   #define gruentaster IO4
   #define zurueck IO5
   int pushed = 0;
   int pushed1 =0;
   int pushed2 =0;
   int rotan=0;
   int gruenan=0;

  void setup()
  {
   pinMode(rot, OUTPUT) ;
   pinMode(gruen, OUTPUT) ;
   pinMode(taster, INPUT) ;
   pinMode(gruentaster, INPUT) ;
   pinMode(zurueck, INPUT) ;
   digitalWrite(rot, HIGH);
   digitalWrite(gruen, HIGH);
   digitalWrite(taster, HIGH);
   digitalWrite(gruentaster, HIGH);
   digitalWrite(zurueck, HIGH);
  }


  void loop()
  {
   pushed = digitalRead(taster);
   pushed1 = digitalRead(gruentaster);
   pushed2 = digitalRead(zurueck);

  if(pushed==LOW)
  {
    rotan=1; 
     if((pushed1==LOW)&&(rotan==1))
     {
         gruenan=1;
         if(pushed2==LOW)
         {
           rotan=0;
           gruenan=0; 
         }
     }
     else
     {
       gruenan=0;
     }
  }
  else
  {
   rotan=0;
   gruenan=0; 
  }
  if(rotan==1)
  {
     digitalWrite(rot, LOW);   
  }
  else
  {
    digitalWrite(rot, HIGH);
  }



  if(gruenan==1)
  {
     digitalWrite(gruen, LOW);   
  }
  else
  {
    digitalWrite(gruen, HIGH);
  }

  }

Was möchtest Du wissen?