Wiring Programming Language
Apps that run on Photon devices are coded in a programming language called Wiring, which was specifically created for microcontrollers.
Logo for Wiring Programming Language
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).

PARTICLE BUILD (Code Editor)

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.
Logo for Particle

PROGRAMMING REFERENCES

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.

APP STRUCTURE

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:
  1. 1.
    libraries (if needed for certain inputs or outputs)
  2. 2.
    global variables
  3. 3.
    setup() function (required - can only have one)
  4. 4.
    loop() function (required - can only have one)
  5. 5.
    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.
1
// comments are notes to yourself or others reading your code
2
// comments are ignored by the device when the app runs
3
// comments can be added as their own separate line in your code
4
5
int led = D0; // comments can also added directly after a line of code
6
7
/*
8
This is a multi-line comment. It starts with a forward slash and asterisk.
9
It's useful when you have a lot of information to list or explain.
10
It is also useful to temporarily comment out a large section of code.
11
The multi-line comment ends with an asterisk and forward slash.
12
*/
Copied!

LIBRARIES

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.
1
// This #include statement was automatically added by the Particle IDE.
2
#include <SparkFunMicroOLED.h>
Copied!

GLOBAL VARIABLES

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.
1
// some examples of data types for variables
2
// there are other data types as well...
3
int led = D7;
4
int timeLimit = 10;
5
int roomTemperature; // variable declared but no value assigned yet
6
float bodyTemp = 98.6;
7
boolean motionStatus = false;
8
String deviceName = "Photon";
9
10
timeLimit = 20; // assign new value to this variable
11
timeLimit = timeLimit + 10; // change value again - now it equals 30
12
int timeLeft = timeLimit; // assign value of timeLimit to new variable
Copied!

Local Variables

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.

Local Variable vs. Global Variable

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.
1
// global variables because they are declared outside of a function
2
int led = D7;
3
int button = D2;
4
5
// setup() function
6
void setup() {
7
pinMode(led, OUTPUT); // uses global variable "led"
8
pinMode(button, INPUT_PULLUP); // uses global variable "button"
9
10
// blink led 5 times at start
11
for (int count = 0; count < 5; count = count + 1) { // "count" is local variable because it is declared inside this statement
12
digitalWrite(led, HIGH); // uses global variable "led"
13
delay(200);
14
digitalWrite(led, LOW); // uses global variable "led"
15
}
16
}
17
18
// loop() function
19
void loop() {
20
int buttonState; // local variable because it is declared inside this function
21
buttonState = digitalRead(button); // uses global variable "button" for digitalRead() and assigns value to local variable "buttonState"
22
23
// LOW means button is being pushed
24
if (buttonState == LOW) { // compares value of local variable "buttonState" to see if it is equivalent to LOW
25
digitalWrite(led, HIGH); // uses global variable "led"
26
}
27
}
Copied!

SETUP( ) FUNCTION

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.
1
// example of setup function
2
void setup() {
3
pinMode(button, INPUT_PULLUP);
4
pinMode(led, OUTPUT);
5
}
Copied!

LOOP( ) FUNCTION

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.
1
// example of loop function
2
void loop() {
3
// read button and assign value to local variable buttonStatus
4
int buttonStatus = digitalRead(button);
5
6
// LOW means button is pressed
7
if(buttonStatus == LOW) {
8
digitalWrite(led, HIGH);
9
} else {
10
digitalWrite(led, LOW);
11
}
12
}
Copied!

CUSTOM FUNCTIONS

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.
1
void loop() {
2
// call custom function
3
checkButton();
4
}
5
6
// custom function
7
void checkButton() {
8
// read button and assign value to local variable "buttonStatus"
9
int buttonStatus = digitalRead(button);
10
11
// LOW means button is pressed
12
if(buttonStatus == LOW) {
13
digitalWrite(led, HIGH);
14
} else {
15
digitalWrite(led, LOW);
16
}
17
}
Copied!
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.
1
void loop() {
2
// call custom function and pass values into it
3
// assign return value to local variable "surfaceArea"
4
int surfaceArea = calculateArea(7,8);
5
6
if(surfaceArea > 50) {
7
digitalWrite(led, HIGH);
8
} else {
9
digitalWrite(led, LOW);
10
}
11
}
12
13
// custom function that receives parameters and returns a value
14
int calculateArea(int width, int length) {
15
int area = width * length;
16
return area;
17
}
Copied!

PUTTING IT ALTOGETHER

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?
1
// example of app coded in Wiring programming language
2
3
// global variables
4
int button = D2;
5
int greenLed = D1;
6
int redLed = D0;
7
8
void setup() {
9
pinMode(button, INPUT_PULLUP);
10
pinMode(greenLed, OUTPUT);
11
pinMode(redLed, OUTPUT);
12
}
13
14
void loop() {
15
checkButton();
16
}
17
18
// custom function
19
void checkButton() {
20
int buttonStatus = digitalRead(button);
21
22
// LOW means button is pressed
23
if(buttonStatus == LOW) {
24
digitalWrite(greenLed, HIGH);
25
digitalWrite(redLed, LOW);
26
} else {
27
digitalWrite(greenled, LOW);
28
digitalWrite(redLed, HIGH);
29
}
30
}
Copied!