Fehler im Code?

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? :slight_smile:

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();

}

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.

Vielen Dank :slight_smile: er funktoniert jetzt