> For the complete documentation index, see [llms.txt](https://reacoda.gitbook.io/molemi-iot/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://reacoda.gitbook.io/molemi-iot/controlling-leds-with-an-arduino/adding-a-button-to-control-the-leds/the-code.md).

# 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:<br>

```
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:
  
  if (digitalRead(button) == HIGH){
        delay(15); // software debounce
        if (digitalRead(button) == HIGH) {
            // if the switch is HIGH, ie. pushed down - change the lights!
            changeLights();
            delay(15000); // wait for 15 seconds
        }
    }
}
```
