Saturday, May 07, 2011

Arduino's IDE: And it begins...

Even if you're programming in hex, there are applications that the micro-controller companies release that allow you to write and program your chip. The IDE program provided for the Arduino utilizes C with some added functions built specifically for the board. If you know how to program in C you know how to write programs for the Arduino.

Let us take a closer look at the Arduino's IDE:

The bare minimum that a program needs to avoid a compiler error are the setup() and loop() functions. Even if you aren't using them, they still need to be placed in the sketch for structure's sake.

 Found by going to File>Examples>Basics>BareMinimum inside of the Arduino IDE

The setup() function is used to initialize pins, registers, variables, and peripherals. It is the first function that is called when the Arduino resets or is powered up, and is only run once. Oddly enough, in all of the example code that I've seen no one actually initializes variables inside of the setup() function even though the Arduino reference suggests it. Most variables are normally declared at the beginning of the program to assure that they are global.

The loop() function replaces the main() function that one would normally see when programming in C. More specifically, it would replace the while loop inside of the main function. As you probably can infer from the name, the loop() function continuously loops until you either place the Arduino into sleep mode or shut it off.

You can still create other functions that would be called from inside of loop(), just place them outside of the loop function as you would when programming in C.


When you are finished with your program or just want to check for any syntax errors, click on the Play (Verify) button to run the compiler.


If your program is error free, you will see a message at the bottom of the IDE letting you know when the compiler is done.


If there are any errors, the program is pretty specific as to what they are and will even highlight the line where the compiler crashed for you. This is a great relief for anyone who has tried to decipher the cryptic error messages in other IDE programs. I'm looking at you MPLAB!



Now you can program your Arduino, assuming you have the USB cable (or a printer to steal it from) by clicking on the upload button.

The best way to learn how to use a new program is to try it out for yourself, so up next... Blinking an LED

No comments:

Post a Comment