> For the complete documentation index, see [llms.txt](https://docs.idew.org/code-internet-of-things/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.idew.org/code-internet-of-things/tutorials/create-smart-light/3.5-modify-button-code.md).

# C-5 Modify Button Code

Next, you'll modify your app code to make the button toggle the LED on or off with each button press – similar to how a light switch normally works.

## Global Variable for LED Status

You'll add a global variable that track the LED's current status ("off" or "on"). Whenever the button is pressed, this variable will be checked, in order to toggle the LED to the opposite status.

Declare a global variable to store the LED's status by adding this code statement **before** the `setup()` function:

```cpp
String lightStatus = "off";
```

This code statement declares a variable named `lightStatus` which has a data type of [`String`](http://www.wiring.org.co/reference/String.html), which is the data type used for storing text.  The initial value of this variable is made equal to `"off"`.

{% hint style="success" %}
**USE DOUBLE QUOTES:**  Use double quotation marks to enclose your text when assigning the value of a String variable:

* **CORRECT (double quotes):**  `String myText = "Hello World";`
* **INCORRECT (single quotes):**  `String myText = 'Hello World';`
  {% endhint %}

## Turn Off LED in Setup

Since the `lightStatus` was initially set to `"off"`, let's be sure the LED is turned off when the app starts by adding this code statement **within** the `setup()` function (after setting the LED pin mode):

```cpp
digitalWrite(LED, LOW);
```

## Custom Function to Toggle LED

Let's create a custom function named `toggleLight()` that will be "called" (performed) whenever the button is pressed.

The `toggleLight()` custom function will check the current value of `lightStatus` ("off" or "on") and then toggle the LED and `lightStatus` to the opposite status.

Add the `toggleLight()` function **after** the `loop()` function (after its closing curly brace):

```cpp
void toggleLight(){
    if (lightStatus == "off") {
        digitalWrite(LED, HIGH);
        lightStatus = "on";
    }
    else {
        digitalWrite(LED, LOW);
        lightStatus = "off";
    }
}
```

Now you can "call" (perform) this custom function whenever the button is pushed. A function is "called" by simply listing the function's name as a code statement.

**Replace** all the existing code within the `loop()` function (everything **inside** its curly braces) with this code instead:

```cpp
    int buttonState = digitalRead(button);
    
    if (buttonState == LOW) {
        toggleLight();
        delay(175); // wait before trying to read button again
    }
```

Now when the button is pressed, the `toggleLight()` function will be called, performing the code within that custom function.

Notice that a brief `delay()` of 175 milliseconds was included. This is necessary to give people just enough time to release the button after pressing it:

* If there is no delay (or the delay is too brief), the device will seem unpredictable because the `loop()` will repeat so quickly that it toggles the light multiple times for the same button press.
* If the delay is too long, the device will seem unresponsive because the button has to be held down longer before the button press is detected.

So this `delay()` value shouldn't be too brief or too long – it needs to be just right. A value between 150-200 ms should work well for most people. You can test your button's behavior, and then adjust the specific value higher or lower to make your button seem more responsive.

## Flash App to Device

Flash your app code to your Photon device by clicking the **Flash** icon in the left navigation bar.&#x20;

Once your Photon has downloaded the app and restarted, the updated app will start running:

* Confirm that the LED light toggles between "on" and "off" each time the button is pressed. (If you hold the button down, you'll see the LED continuously toggles between on and off.)

You may notice that occasionally a button press isn't detected immediately.  This is normal because there will be occasions when you press the button while the Photon happens to be in the middle of the `delay()` period. However, if the button seems very unresponsive, you can adjust the `delay()` value in your app code, and then flash the updated app to your device to test the new value.

At this point, your Photon device should be operating similar to a normal light that can be switched on and off.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.idew.org/code-internet-of-things/tutorials/create-smart-light/3.5-modify-button-code.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
