Programming Terms and Programming Environments
Programming Terms and Programming Environments
Programming Environments
Introduction
If you’ve never programmed before, there are a few terms and concepts that
may throw you off when you first start. There are many helpful guides to
understanding programming on the web, and there are many for Arduino,
p5.js, and Processing in particular. What follows are some specific concepts
that will come up in these labs and notes that you may not be familiar with.
To get the most our of these notes, you may want to download Arduino and try
things as they’re mentioned.
• A text editor
• A compiler
• Controls to run and stop the program
• A debugger
The text editor of any IDE is the main thing you should see. It’s where you
write your code. Programming text editors can come with all kinds of features.
The Arduino editor, the p5js editor, and the Processing editor are all pretty
basic so as not to overwhelm you with features, but there are a couple
features you might notice:
Line numbers are displayed at the bottom of the editor as shown in Figure 1. In
later versions of Arduino (1.6.5 and after) you can click on File -> Preferences
and turn on line numbering in the editing window itself.
Figure 2. Note
the “Done Compiling” message at the top of the console pane. At the bottom,
Arduino indicates the board type and the serial port to which it uploaded your
code. Figure 3.
When an error message occurs, the “Copy error messages” copies the error to
the clipboard so you can paste it into a search engine or help forum message
easily.
Block completion indicators Many programming languages use braces {},
parentheses () and brackets [] as punctuation for parts of a program.
Programming editors usually include an indicator that shows you the opening
or closing bracket, parenthesis, or brace that goes with the one on which the
cursor is currently positioned. In Figure 4 you can see the rectangle around the
opening bracket that is paired with the closing bracket where the cursor is. Put
your cursor on different brackets, braces, or parentheses, and you should see
this completion indicator move.
Figure 4. Block completion indicator
shows you the bracket, brace, or parenthesis corresponding to the one where
the cursor is
The Toolbar contains the control buttons that give you control of the
compilation and upload of your code. In Arduino (Figure 5), the first button to
the left compiles your code and the second compiles and uploads to the board
you have selected. The next few buttons are for opening and saving files. The
one on the right opens the Serial Monitor:
Figure 6. p5 Toolbar
Figure 9. Arduino 2.0 IDE window features the same Editor and Console panes,
but now there are buttons on the left of the window for the Sketchbook, the
Boards menu, the Libraries menu, the Debugger, and a Search tool. The bottom
of the Console now features a current line number and column number for the
cursor, a language and encoding indicator, and the board and serial port
indicator.
Figure 10. The Arduino 2.0 Serial Monitor now sits in the Console pane, and
stays with the Sketch window.
Figure 11. The Arduino 2.0 Serial Plotter (found in the Tools menu) now
features the ability to turn and off different values being plotted; a start/stop
button; a text input field; and an interpolator for smoothing data.
The Compiler
The compiler is a program that takes the text you write and translates it into
the binary instructions that the computer needs to run the program. You never
actually see the compiler in the Arduino or Processing IDEs, because it does all
the work in the background. The buttons in the Toolbar are your interface to
the compiler, and the message pane is where you’ll see status and error
messages from it.
The Arduino IDE, and any other microcontroller IDE, will also include
an uploader, which moves the compiled code from your personal computer to
the microcontroller itself. Other microcontroller environments may require a
hardware programmer which attaches to your computer via USB and then
connects to your controller in order to upload the code to it.
The Parts of a Program
Programming languages have a syntax, just like written languages.
Understanding the terms for that syntax will make it easier to program.
Arduino is based on a programming language called C, and Processing is based
on a programming language called Java. There are many programming
languages that share the same basic syntax as C, and they’re referred to as C-
style languages. Java happens to be a C-style language, as is JavaScript. The
notes below all describe C-style languages, so it all applies to C, Java, Arduino,
Processing, and JavaScript, among others.
Variables
1 buttonPushes = 0;
Variables can generally be any word you want, as long as they don’t start with a
number, and as long as they’re not an existing keyword in the language. It’s
helpful if you use descriptive variable names, so your program is more
readable. When you want to use more than one word in a variable name, you
can either use camel case notation, like this: thisIsMyVariable, or you can use
underscore notation like this: this_is_my_variable.
Functions
• name: addOne
• Data type: int
• return value: result
• parameter: number
Functions can take variables as input when you call them. These are called
the parameters of the function call. The parameters of a function go in the
parentheses after its name. In the function above, the function’s parameter is
int number.
Functions can also return values. What they return is called the return value.
The function’s return value is a variable whose data type is the function’s data
type. You can see this above: the function’s data type is an int, and it returns
an int. Functions don’t have to return values though. If a function doesn’t
return any value, its data type is void.
The parameters that you send into a function are not the same as the return
value you get out of it. In the function above, the parameter number has the
same data type as the return value (and of the function), but it doesn’t have to
be. Here’s another function where the parameters and the function’s data
types are different:
Keywords
Every language has certain keywords that have special meanings. The
reference section for a language will include its keywords, and when you type
those in the IDE, they’ll usually get color-coded. These are words that define
programming structures like for, if, and while, or the names of built-in
functions and libraries like digitalWrite() or Serial. Keywords also identify data
types like int, byte, and String. Your compiler will let you know when you mis-
use a keyword by printing an error in the message pane.
Operators
Addition: +
Subtraction: –
Multiplication: *
Division: /
Modulus: %
There are also some special math operators that combine other
operators. They’re shorthand for common math operations:
Increment value: ++
decrement value: —
add right value to left: +=
subtract right value from left: -=
multiply right value by left: *=
divide right value by left: /=
These operators update the value on the left side of the operator using the
value on the right side.
For example, to increment the variable buttonPushes without these operators,
you’d type:
1 buttonPushes = buttonPushes+1;
But with these operators, you can do this:
1 buttonPushes+= 1;
And since adding and subtracting one (also known as incrementing and
decrementing, respectively) are so common, you can even do this:
1 buttonPushes++;
Here are a few other examples using these operators:
1int buttonPushes = 2;
2buttonPushes += 2;
Result: buttonPushes = 4.
1int buttonPushes = 3;
2buttonPushes *= 2;
Result: buttonPushes = 6.
1int buttonPushes = 3;
2buttonPushes--;
Result: buttonPushes = 2.
1int buttonPushes = 3;
2int pointsScored = 6;
3pointsScored /= buttonPushes;
Result: pointsScored = 2.
Then there are comparison operators, that you use to compare two numbers:
Comparison:
1if (buttonPushes == 3) {
2
3}
Setting a variable equal to a number:
1 int headCount = 3;
Punctuation
In C-style languages, every line of code ends with a semicolon. You’ll get an
error if you don’t include it at the end of your lines.
1 int headCount = 3;
Blocks of code are surrounded by braces. Think of blocks like the paragraphs of
programming. For example, here’s a block of code that’s executed if a
particular condition is true:
1if (buttonCount == 4) {
2 digitalWrite(3, HIGH);
3 digitalWrite(4, LOW);
4}
Comments
You can write plain language comments in your program by putting two
slashes in front of them. When you do this, the compiler will ignore everything
from the slashes until the end of the line:
1if (buttonCount == 4) {
2 digitalWrite(3, HIGH); // turn on the blue LED
3 digitalWrite(4, LOW); // turn off red LED
4}
You can also make multi-line comments using the following notation:
1
/*
2 Everything between the star/slash combination
3 will be ignored by the compiler.
4*/
5if (buttonCount == 4) {
6 digitalWrite(3, HIGH); // turn on the blue LED
7 digitalWrite(4, LOW); // turn off red LED
8}
Program Flow
Your program will be executed one line at a time until it reaches the end.
However, most languages have a special function that will run forever and
ever. In Processing and p5.js, this function is called draw(), and in Arduino it’s
called loop(). in Java and C, it’s called main(). When the special function is
finished, it will repeat until you stop the program or power down the computer
(if it’s a microcontroller).
P5, Processing, and Arduino also share another special function called setup().
This function will run at the beginning of your program every time, then it will
pass control to draw() or loop().
Other functions will get executed when they are called. To call a function, you
call its name inside another function.
1void setup() {
2 Serial.begin(9600);
3 Serial.println("Program is starting");
}
4
void loop() {
5 if (digitalRead(3) == HIGH) {
6 saySomething();
7 }
8}
9
10void saySomething() {
11 Serial.println("I said something");
12}
13
The first function, setup(), will run once at the start. It will open serial
communications (there’s that serial library mentioned above), then it will
print “Program is starting” via the serial port. Then the program will move on
to loop(). That function will check to see if one of the input pins of the controller
has high voltage on it using the built-in function digitalRead(). If that is true,
then the function saySomething() will get called. The program will then return to
the end of the if statement. Since that happens to be the end of
the loop() function, then the program will go back to the top of the loop() and run
that function again.
Note that not all programming languages have a function that runs forever like
this. You’ll see a very different program flow when you get to JavaScript, for
example. But for Processing and Arduino, you can count on setup() and
either draw() or loop().
Conditional Statements
“if the sensor level is above a threshold, turn on the motor. Otherwise, turn the
motor off.”
1
int threshold = 45;
2int sensorlevel = analogRead(A0); // read an analog input
3if (sensorLevel < threshold) {
4 setMotor(1); // turn motor on
5} else {
6 setMotor(0); // turn motor off
7}
8// code flow continues here after the conditional
The conditional statement in the example above directs the program to jump
to the setMotor() function depending on whether the variable sensorLevel is
greater than the variable threshold or not. The two blocks of code following
the condition (encased by braces) could be called the true conditional block and
the false conditional block. Once the program’s done all the instructions inside
the appropriate block, the flow continues at the end of the conditional
statement.
You don’t have to use the else statement with every if statement. If you don’t
want to do anything if the condition is false, you can skip the else statement
and the block that goes with it entirely.
While Loops
For Loops
While loops are handy when you want to continue an action as long as some
condition is true, but sometimes you just want to repeat an action a particular
number of times. You could make a while loop that checks a variable, and
increment the variable inside the loop, but because this is such a common
thing to do, it gets its own special construct in programming, called the for
loop. It works like this:
If you wanted to loop ten times, your for loop would look like this:
Although the most common form of for loop is to increment a variable by one,
you can do fancy things, like decrementing, counting by two, starting with a
number other than zero, and so forth.
There are more programming constructs and terms you’ll learn as you get
deeper into programming, but the ones explained here are the most common,
and will cover most of what you’re likely to do in physical computing