5. Programming in Arduino IDE!
Last updated
Was this helpful?
Last updated
Was this helpful?
Now that we’ve set up our Arduino IDE, installed the proper drivers, and successfully connected our Arduino to the computer, we can finally move on to the fun part… coding our FBX robot!
But… where do we even start?
Before we dive into programming our FBX, we should first go through a basic example program in the Arduino IDE…
Blink.ino
Let’s have a look at the example sketch called Blink.ino
.
Go to File > Examples > 01.Basics > Blink
We will break down each part of this example sketch in detail so you have a better idea of how programming in Arduino IDE works.
Anything in light-gray is what is known as a comment. They do not actually affect our program, but they allow a programmer to…
Organize thoughts about the code
Plan out their code before writing it
Communicate what a piece of code actually does (in simple terms). This is especially important when sharing code with others!
To make a single-line comment, we simply start our line with //
:
To make a multi-line comment, we surround our comment like /* this */
:
Setup()
FunctionThe setup()
function is called when a sketch starts.
We use it to...
initialize variables
initialize pin modes
start using libraries
The setup()
function will only run once, after each power up or reset of the Arduino board (any time we turn on the FBX!)
Let's take a closer look...
pinMode()
FunctionpinMode()
configures the specified pin to behave either as an input or an output.
In other words, we use pinMode()
to tell our Arduino board whether a certain pin is an INPUT (like a button or a switch), or an OUTPUT (like an LED or a motor on our FBX).
loop()
Functionthe loop()
function does precisely what its name suggests: It loops over and over again, allowing your program to change and respond in real time.
We can use it to actively control the Arduino board.
(And, by extension, our FBX!)
Let's take a closer look...
digitalWrite()
FunctiondigitalWrite()
allows us to write a HIGH or a LOW voltage value to a digital pin. If the pin has been configured as an OUTPUT with the pinMode()
function, its voltage will be set to the following values:
5V (or 3.3V) for HIGH
0V (ground) for LOW
delay()
Functiondelay()
pauses the program for the amount of time (in milliseconds) specified by the user in the parenthesis.
Since there are 1000 milliseconds in a second, a call to delay(1000)
should pause our program for 1 second!
Similarly…
delay(500)
= ½ of a second, delay(250)
= ¼ of a second