D-3 LED and Button Code

Next, you'll use your Smart Light device app as starter code for your Smart Security device app. The LED and button code in the two apps will be nearly identical – you'll just make a few minor changes. After that, you'll be ready to start adding new code for the motion sensor and speaker.

Log In to Particle Build

One person on your team should log in to Particle Build using your team's Particle account login.

Once you're logged in, you should see a new app template in the code editor. (If not, just click the "Create New App" button in the Code menu panel.)

Title New App

Enter smart-security as your app's title.

Copy Smart Light Code

You'll use your smart-light app as starter code for your smart-security app:

  1. The Code menu panel lists all your saved apps under "My apps." Click on your smart-light app to load its code into the code editor panel. (If the Code menu panel is not visible, just click the Code icon in the left navigation bar to show the Code menu panel.)

  2. In the code editor panel, select all the existing code in your smart-light app, and copy it.

  3. In the Code menu panel, click on your smart-security app listed under "My apps" to load it into the code editor panel. (If the Code menu panel is not visible, just click the Code icon in the left navigation bar to show the Code menu panel.)

  4. Select all the existing code in your smart-security app, and then replace it by pasting in the copied code from your other app.

Save your smart-security app code by clicking the Save icon in the left navigation bar.

Modify App Code

The LED and button code in the Photon app will remain essentially the same. You'll just make some minor changes to the lightStatus variable and toggleLight() function:

  1. Change the name of lightStatus to deviceMode. There should be 6 places in the code where this change needs to be made.

  2. The possible values for lightStatus were "off" or "on". Find the 3 places in the code where "off" is listed, and change each one to "disarmed" instead. There is 1 place in the code where "on" needs to be changed to "armed" instead.

  3. Change the name of toggleLight to toggleMode. There should be 4 places in the code where this change needs to be made.

  4. In the comments at the top of the code, change Smart Light Device to Smart Security Device.

These modifications do not change how the code functions – it just makes the code make more sense when you read it, since this app will control a Smart Security device (instead of a Smart Light).

Flash App to Device

Be sure your Photon device is connected to Wi-Fi and Particle Cloud.

Flash your app code to your Photon device by clicking the Flash icon in the left navigation bar. (Particle Build will first save and verify your code before flashing it to your device.)

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

  • Confirm that the device still functions like the Smart Light did: the LED light should toggle between on and off each time the button is pressed. When the LED is turned on, it indicates the security system is in "armed" mode. When the LED is turned off, the security system is "disarmed."

In the next steps, you'll add code for the motion sensor and speaker to made the security system fully functional when it's "armed."

Last updated