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:

String lightStatus = "off";

This code statement declares a variable named lightStatus which has a data type of String, which is the data type used for storing text. The initial value of this variable is made equal to "off".

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';

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):

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):

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:

    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.

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.

Last updated