Go Down

Topic: Fehler im Code? (Read 800 times) previous topic - next topic

buiya

Hallo ich habe ein Problem mit meinem Code. Er wird immer wieder wiederholt obwohl er erst laufen soll wenn man den button drückt. Könntet ihr mir sagen wo ich in meinem code ein Fehler habe? :)

Gruß Yann


int carRed = 12;                                              // LED connected to digital pin 12
int carYellow = 11;                                           // LED connected to digital pin 11
int carGreen = 10;                                            // LED connected to digital pin 10
int pedRed = 9;                                               // LED connected to digital pin 9
int pedGreen = 8;                                             // LED connected to digital pin 8
int button = 2;                                               // Button connected to digital pin 2
int crossTime = 5000;
unsigned long changeTime;

int x;

void setup(){
  pinMode(carRed, OUTPUT);                                    // sets the digital pin as output
  pinMode(carYellow, OUTPUT);                                 // sets the digital pin as output
  pinMode(carGreen, OUTPUT);                                  // sets the digital pin as output
  pinMode(pedRed, OUTPUT);                                    // sets the digital pin as output
  pinMode(pedGreen, OUTPUT);                                  // sets the digital pin as output
  pinMode(button, INPUT);                                     // sets the Button as input
  digitalWrite(carGreen, HIGH);                               // set the ledpin to HIGH (5 volts)
  digitalWrite(pedRed, HIGH);                                 // set the ledpin to HIGH (5 volts)

}

void loop(){                                                  // creates the loop that your Arduino will repeat over & over
  int state = digitalRead(button);
  if (state == HIGH && (millis() - changeTime) > 5000){
      changeLights();
  }
}
void changeLights(){                                          // the commands that make the leds light up in a specific order

  digitalWrite(carGreen, LOW);                                // set the ledpin to LOW (0 volts)
  digitalWrite(carYellow, HIGH);                              // set the ledpin to HIGH (5 volts)
  delay(2000);                                                // causes the Arduino to wait for 2000 milliseconds before continuing on to the next line
  digitalWrite(carYellow, LOW);                               // set the ledpin to LOW (0 volts)
  digitalWrite(carRed, HIGH);                                 // set the ledpin to HIGH (5 volts)
  delay(1000);                                                // causes the Arduino to wait for 1000 milliseconds before continuing on to the next line
  digitalWrite(pedRed, LOW);                                  // set the ledpin to LOW (0 volts)
  digitalWrite(pedGreen, HIGH);                               // set the ledpin to HIGH (5 volts)
  delay(crossTime);                                           // causes the Arduino to wait for the specified number of milliseconds before continuing on to the next line
  for(int x=0; x<10; x++){
    digitalWrite(pedGreen, HIGH);                             // set the ledpin to HIGH (5 volts)
    delay(250);                                               // causes the Arduino to wait for 250 milliseconds before continuing on to the next line
    digitalWrite(pedGreen, LOW);                              // set the ledpin to LOW (0 volts)
    delay(250);                                               // causes the Arduino to wait for 250 milliseconds before continuing on to the next line
}
  digitalWrite(pedRed, HIGH);                                 // set the ledpin to HIGH (5 volts)
  delay(500);                                                 // causes the Arduino to wait for 500 milliseconds before continuing on to the next line
  digitalWrite(carYellow, HIGH);                              // set the ledpin to HIGH (5 volts)
  digitalWrite(carRed, LOW);
  delay(1000);                                                // causes the Arduino to wait for 1000 milliseconds before continuing on to the next line
  digitalWrite(carGreen, HIGH);                               // set the ledpin to HIGH (5 volts)
  digitalWrite(carYellow, LOW);
  changeTime = millis();

}

Serenifly

#1
Jul 03, 2013, 07:50 pm Last Edit: Jul 03, 2013, 07:59 pm by Serenifly Reason: 1
Versuch mal changeTime mit millis() zu initialisieren.

unsigned long changeTime = millis();

Oder mach es im setup am Ende

Sicher dass der Taster high-aktiv ist? Das kann man auch anders herum aufbauen.

buiya

Vielen Dank :) er funktoniert jetzt

Go Up