The Code

Start by adding a new variable to the start of the program:

int button = 12; // switch is on pin 12

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

Now, in the setup function, add a new line to declare the switch as an input. I’ve also added a single line to start the traffic lights in the green stage. Without this initial setting, they would be turned off.

int button = 12; // switch is on pin 12

void setup() {
  // put your setup code here, to run once:
  
  pinMode(button, INPUT);
  digitalWrite(green, HIGH);

}

void loop() {
  // put your main code here, to run repeatedly:

}

In the loop function add the following:

Last updated

Was this helpful?