1

I'm trying to making a traffic light for my school project but my button for the traffic light doesn't work. The traffic lights should light up normally but when the button is pressed all LEDs should go to LOW but that does not happen. Did I connect my buttons incorrectly?

Code:

int Pins[] {2,3,4,5,6}; 
int red =2;
int yellow=3;
int green = 4;
int GruenF =6;
int RotF=5;

int blaulichtTaster = 12;
int blaulichtTasterStellung = 0;
  
  
void setup() {
  for(int i=0;i<9; i++) {
  pinMode(Pins[i], OUTPUT);
}
  pinMode(blaulichtTaster, INPUT);
  
}
void loop() {
  
  blaulichtTasterStellung = digitalRead(blaulichtTaster);
  
  if(blaulichtTasterStellung == 0){
    digitalWrite(red, HIGH); 
  delay(3000);
  digitalWrite(GruenF, HIGH);
  digitalWrite(RotF,LOW);
  delay(4000);
  for(int i =0;i<4;i++) {
    digitalWrite(GruenF,LOW);
    delay(500);
    digitalWrite(GruenF,HIGH);
    delay(500);
  }
  digitalWrite(GruenF,LOW);
  digitalWrite(RotF, HIGH);
  delay(3000);
  digitalWrite(yellow,HIGH);
  delay(4000);
  digitalWrite(red,LOW);
  digitalWrite(yellow,LOW);
  digitalWrite(green, HIGH);
  delay(10000);
  digitalWrite(green, LOW);
  delay(500);
  for(int i = 0; i<3; i++) {
    digitalWrite(green, HIGH);
    delay(500);
    digitalWrite(green, LOW);
    delay(500);
  }
  digitalWrite(green, HIGH);
  delay(500);
  digitalWrite(green, LOW);
  digitalWrite(yellow, HIGH);
  delay(4000);
  digitalWrite(yellow, LOW);
  }
  
  else{
    digitalWrite(red,LOW);
    digitalWrite(yellow,LOW);
    digitalWrite(green,LOW);
    digitalWrite(RotF,LOW);
    digitalWrite(GruenF,LOW);
  }

}

My circuit: enter image description here

5
  • 3
    Assuming that the power rails in your breadboard are continuous, and you hold the button long enough for the entire light sequence to execute from start to finish (33 seconds?), then yes it looks like it should work. Are you expecting it to react instantly to you pressing the button or something? – Majenko Mar 13 at 12:29
  • Yes it schould react instantly. – fatihakan21 Mar 13 at 12:38
  • 1
    Then you're going to have to re-think your program and get rid of all those delay() functions. – Majenko Mar 13 at 12:42
  • @fatihakan21, can you address the "Assuming that the power rails in your breadboard are continuous" part of Majenko's comment. Have you in fact confirmed that they are continuous? – timemage Mar 13 at 14:50
  • 1
    write a test sketch that turns on all the LEDs when the button is pressed and turns off the LEDs when the button is released ... after all, the question is about the button, not about the rest of the sketch ... you are assuming that the button is failing, so you need to test your assumption – jsotola Mar 13 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.