Wrapping One's Brain Around the Arduino, Part 2

In this blog, we move on to consider the Arduino IDE, along with resources like books and kits that are available to help you learn how to program this little rascal.

In my previous column on the Arduino, we considered the hardware platform itself. Now it's time to consider how we create programs for this little rascal.

There are several programming environments one can use with the Arduino. If you are a beginner, perhaps the best option is to use the official Arduino IDE (integrated development environment), which you can download for free from the Download page on the Arduino website. Have your Arduino board and USB cable near your computer, but don't plug them in just yet.

As you will see, there are Windows, Mac OS X, and Linux versions of this IDE available. Just follow the instructions on the Arduino website. When you do come to connect your Arduino to your computer, one of the first things you must do in the Arduino IDE is use the "Tools > Board" pull-down menu to select the type of Arduino platform you are using. For the purposes of these discussions we will assume an Arduino Uno, so this is the option you would select.

Introducing the Arduino programming language
The Arduino programming language is an implementation of the open-source electronics prototyping platform called Wiring, which itself is based on an open-source electronics prototyping platform called Processing. For our purposes here, however, the simplest way to think of this is that the Arduino programming language is a simplified version of the C and C++ programming languages.

If you are already familiar with C/C++, then you will also be familiar with the concept of the "main" function. The idea here is that a standard C/C++ program consists of one or more functions, and that one of these functions must have the name "main":

A standard C/C++ program is executed when the higher-level operating system hands control over to the "main" function in the program. The "main" function differs from other functions in two ways:

It may not be called from within the program.

Any parameters to "main," if they exist, are provided (passed-in) by the operating system.

A program written for the Arduino is called a "sketch." The main point to note here is that sketches do not contain a "main" function. Instead, every Arduino program includes two primary functions (along with any functions you add of your own) called "setup" and "loop":

Any code you write within the curly brackets associated with a function will be executed when that function is called. The "setup" function runs only one time when the Arduino is first powered-up or when it is reset. The "loop" function runs continuously after the "setup" function has finished performing its tasks.

As an aside, for the C/C++ purists amongst us, my understanding is that when we initiate a compilation, the IDE slips in a "main" function while we aren't looking. The thing to remember is that the creators of the Arduino are trying to keep things as simple as possible for beginners.

Learning the Arduino programming language
First of all, there are a bunch of useful tutorials and other resources available for free on the main Aduino.cc website. If you are an absolute newcomer to C/C++, then one book that I would personally recommend is Programming Arduino Getting Started with Sketches by Simon Monk, which is available for a very reasonable $10.99 from Amazon.

It's worth knowing that an Arduino board typically arrives with a default program called "Blink" preinstalled. As soon as you connect the Arduino to your host computer with the USB cable, this program will cause a LED on the board to start blinking. The first half of Simon's book introduces you to various aspects of the C/C++ programming language via changes you make to this "Blink" sketch. Later experiments in the second half of the book will require you to use some very basic components and tools, like a resistor, a switch, a couple of pieces of wire, and a multimeter.

As you become more advanced, you will discover that there are all sorts of Arduino-specific resources available on the Internet to answer your questions. Also, a lot of general-purpose C/C++ resources are out there, such as this 'const' and 'static' keywords tutorial and this pointer tutorial.

Actually, the "main" function is defined among the other files in the hardware specific directory of the arduino ide installation. I just discovered this myself fairly recently while reading a book on programmin the arduino as an AVR platform.

Alex - Right now, the Arduino doesn't really compete with the mbed in the comercial world. I think that's changing though. I think that the Arduino will end up as a decent alternative; especially in the cross over from maker to commercial community.

I'm a big fan of the mbed too. It's nice to see it showing up on more platforms as well.

Even if people accept the open license terms of the arduino for some projects, wouldn't it just make sense to learn the mbed, and due to commercial friendly licensing terms , use it it in more projects , and get a better return on learning?

Also, yes there are plenty more content/code around the arduino than the mbed, but if we're talking about professionally usable content/code , how do they really compete ?

Alex - Yes, the Due is 32 bit. There are more and more 32 bit Arduino compatibles popping up too. My theory is that with the more powerful procesors, the Arduino language will start to fnd acceptance in some commercial applications.

Although I prefer the BeagleBone, I'm getting an Arduino for my nephew, because of the size of the ecosystem & amount of tutorials (he has NO electronics experience). However, instead of getting a kit with discrete parts, I'm buying a Seeeduino plus the Seeeduino Grove shield, some Grove devices, and (I hope I can get them to work at the same time) an Arduino motor shield R3 (which actually has Tinkerkit connectors).

The Grove & Tinkerkit systems are similar: there are standard connectors and then you can get various prewired devices. Devices can communicate via analog input, digital input, digital output, or I2C.

The Grove system is currently cheaper than Tinkerkit & has a wider variety of components, such as analog thumb joystick, button, motor controller, OLEDs, LEDs, current sensors, PIR, WiFi, Bluetooth, and much more. You can also get Grove kits, but I much prefer picking out exactly what I want, for substantially less than a kit (or the kits you showed).

blinking and hello world are no problems. THey usually have that code ready to go with the board. However, where I find the most benefit to the Arduino community is that there are THOUSANDS of tutorials for things beyond that.

AD - I have few negative things to say about the Arduino when used for it's intended purposes. The biggest issue I ran into originally was the limited amount of RAM and program storage FLASH available.

I was able to set up a wired Arduino web server amazingly quickly. However, when I tried to make it wireless, I ran out of space.

I got my Arduino working with a Ti CC3000 WiFi breakout board, but when I tried to add in a small OLED, I ran out of RAM.

Since that time, I've discovered 32 bit Arduino compatible boards. The ChipKIT PIC32-based Arduino I have has plenty of both RAM and FLASH. It's pretty compatible, but not quite 100%, so I've run into a few issues there.

AD - I can confirm that some of the error messages are rather cryptic. I've had a few that don't seem to give any actual information relative to what the error ended up being. That's kind of frustrating.

I've developed (in my head) a standard checklist of sorts to look at first when there's an unclear error message.