Getting Started with Programming on an Arduino
Introduction
As previously said with C++, it has a quite large learning curve that requires a lot of previous knowledge about how a computer works, how C works, how C++ works, etc., etc. But given one step at a time, it is significantly easier to learn with the Arduino framework than to go into the wild west of C++ programming.
If you're a total beginner stuck with an Arduino with no way to program it, no fear! This tutorial is for you. We here at Canine Rocket Technologies have had our own first experiences with Arduino. It isn't pretty, but it got us where we are now. Stick with us, we don't bite (too hard)!
Part 1: Understanding How Code Works
In a nutshell, all a computer does is follow a list of basic instructions. These instructions can be called machine code, the ones and zeros defining what the computer should do. A higher level implementation of machine code is an assembly language, or a human representation of those ones and zeros. Let's not jump too deep into that today, but try to keep that in mind as we go along this tutorial.
We will first be learning the basics of C, the precursor to the sister language C++. Both are very similar, but have very different features.
C is a high(ish) level programming language for general-purpose demands. It was created in the 1960s when the developers of the brand-new UNIX operating system got tired of writing assembly code and wanted something easier and more understandable to write in.
The basics of C and C-based programming languages follow these five main principles:
Functions: A way to call different parts of code in a single line.
Variables: A place to store all kinds of data in memory.
Conditions: A way to branch code given an expression.
Loops: A way to recursively run code.
Operations: A way to perform mathematical calculations and comparisons.
Let's examine each principle in context with some C code.
Did you see each principle in action?
If not, here's a commented version of the same code.
Of course, there's still a lot more to C than just this. This is a basic understanding of how C works.
This is essentially what we are doing in the code:
We set up the
main
function. It is the function to run at the start of the program.We create a floating-point variable
x
. That means it can accurately store a number that has a tenths place, hundredths place, etc.We check to see if
x
is greater than four. If it is, we print to the console it is. If not, we print to the console it isn't.We create a loop to increment the value of
x
by2.16
five times. We then print the current value ofx
.We finish off by returning
0
from themain
function. This tells the operating system that the program finished successfully.
By compiling this code with your favorite compiler and running it, the output should look similar to this:
Congratulations, you have learned the basic principles of how (most) programming languages work. Good job.
Part 2: Implementing Our Own Arduino Code
Now that we've seen how basic C code is, let's try making our own C++ code to do some Arduino things. For now, we will keep it basic so we can easily understand how it works.
Just like with the C code, here is some C++ code to do some Arduino things:
Many, but not all, of the principles states in Part 1 were used. Can you spot them?
If not, here's a commented version of the same code.
If you couldn't tell already, this code turns on and off an LED on the Arduino really quickly.
If you would like a list of more functions to play with, here's a complete list that Arduino gives us to play with.
Last updated