Apps that run on Photon devices are coded in a programming language called Wiring, which was specifically created for microcontrollers.
Wiring is based on another programming language called Processing. An app written in Wiring actually gets compiled (converted) into another programming language called C++ in order to run on your device.
You may have heard of another popular platform for building and programming electronic devices called Arduino. Arduino is a programming language based on Wiring. In fact, the languages are so similar that many apps created for Arduino devices will also work on Photon devices (or can be modified to work).
Apps for your Photon device are created in the Particle Build online code editor. An app is loaded onto your Photon device by "flashing" (downloading) the app over Wi-Fi (similar to updating an app on a smartphone). Here's a brief overview of navigating and using Particle Build.
You can only store and run one app at a time on your Photon device. However, you can create and save multiple apps in your Particle Build account, making it easy to "flash" a different app onto your device when needed.
Here is the official reference for the Wiring programming language:
Particle also has some custom functions built into the Photon device's firmware (its Operating System). For example, Particle has custom functions for sending and receiving data through the Particle cloud service, etc.
The basic syntax for Wiring code is similar to certain other programming languages (such as Java, C++, etc.). For example, each statement typically ends with a semi-colon, comment lines start with two forward slashes, etc.
Every Wiring app must contain a setup()
function and aloop()
function (but only one of each). In addition, an app can have custom functions. Most apps have global variables.
Here is the basic structure (in order) for the code of a Wiring app:
libraries (if needed for certain inputs or outputs)
global variables
setup()
function (required - can only have one)
loop()
function (required - can only have one)
custom functions (if needed - can have as many as needed)
It can also be helpful if you include some comments in your code.
Comments are just notes to yourself (or to anyone else reading your code) that help explain parts of your code. A comment can be inserted in your code whenever you think it will help. Any comments in your code are ignored by the device when your app runs.
A comment starts with two forward slashes. Everything after the slashes (until the end of the line) will be treated as part of the comment.
Certain inputs or outputs (such as the Micro OLED display, etc.) require that a specific code library is included in your app, so you have access to special functions to control these parts more easily. This is similar to how a webpage will include a JavaScript library, such as jQuery, in order to use special functions.
Particle Build has numerous libraries available and will automatically generate an #include
statement at the top of your code for each library that you add to your app.
Computer programs use variables to store and process data. The data might be numbers, text, etc.
Similar to variables in algebra, variables in computer programs are given names. Each variable should have a unique name. As the person coding the program, you get to decide the unique name for each variable. A variable name could be as simple as x
or y
– but it is better to use names that help describe what the variable represents, such as pushButton
or roomTemperature
. This will help your code make more sense to you and to anyone else reviewing your code.
Variable names cannot contain spaces. A common format used for variable names is to type them in "camelCase": type them in lowercase, but use a capital letter to start any new "word" within the name. (The capital letters in the middle of the name are like the humps on a camel's back.) For example, you cannot name a variable as red button
because it has a space. Instead, any of these names (or other variations) would work: redbutton
or REDBUTTON
or redButton
. The last example is in camelCase, which is easier to read than the first two examples (all lowercase, all uppercase).
Variable names cannot be the same as a keyword in the programming language. These keywords are reserved for your programming instructions. For example, pinMode
is a keyword used to set an I/O pin as an input or output. Therefore, you cannot use pinMode
as the name of a variable. Here is a list of keywords in the Wiring programming language.
Global variables are variables that will be used by multiple functions within your code, such as the setup()
, loop()
, and custom functions. Global variables are typically declared (created) at the top of the code, before the setup()
function. A variable has to be declared before it can be used.
Wiring requires that each variable is declared as a specific data type, such as int (integer numbers), float (decimal numbers), boolean (true or false), String (text), etc.
NOTE: Wiring treats pin names (D7, etc.) for inputs and outputs as int variables.
A variable can be assigned a value when it is first declared by using the "equals" sign. The value of a variable can also be assigned or changed later in the program. You can do other things with variables, such as compare the values of variables, assign the value of one variable to another variable, etc.
Variables that are only used within one function or within one statement (such as: if
statement, for
loop, etc.) can be declared inside that function or statement as a local variable. A local variable only exists inside that particular function or statement and won't be recognized by any outside functions or statements.
There is no difference in how you declare (create) a local variable versus a global variable. The difference is where you declare the variable. The simple rule is that if you declare the variable inside a function (or inside a statement), then it will be treated as a local variable. If you declare the variable outside of a function, then it will be treated as a global variable.
The setup()
function runs one time at the start of program when the device is powered on. The setup()
function typically contains statements that identify the input and output pins being used by the device.
A setup()
function must be included (even if it is simply empty inside), but only one setup()
function is allowed in an app.
After the setup()
function is finished, the loop()
function runs over and over in a repeating loop. The loop()
function usually contains statements that perform the main tasks of the device, such as reading data from inputs, processing the data to make decisions, and writing data to outputs.
A loop()
function must be included (even if it is simply empty inside), but only one loop()
function is allowed in an app.
Custom functions are typically listed at the bottom of the code, after the loop()
function. Custom functions can be run by "calling" their name within another function, such as within the setup()
, loop()
, or a different custom function.
Custom functions are useful for long or complex programs because it allows you to break up your code into smaller modules that perform specific tasks.
Similar to variables, each function must be given a unique name and must have a return data type declared. Some custom functions return a value (such as int, float, String, etc.) when they are run. If a function doesn't return any value, then void
is declared for the return data type. For example, the setup()
and loop()
functions always have void
declared for their return data type.
Custom functions can have values passed into the function as parameters when the function is called. These parameters are declared as local variables inside the parentheses following the function name.
Here is a simple app written in Wiring that has global variables, a setup()
function, a loop()
function, and also a custom function. What will this app do when it runs?