Newbie’s guide into programming - 00000100 - First program and its structure – Arduino

Teapot.jpg

Now it is Arduino’s turn to open the secret of “Blink a LED” to us. Arduino is heavily supported by a huge community, and is well documented. So, no doubt, there is a “Blink” sample code, which is accessible from Arduino IDE’s main menu File > Examples > 1. Basics > Blink. Below is the code of this example:

 

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
void setup() {                
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);     
}
 
void loop() {
  digitalWrite(13, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  delay(1000);              // wait for a second
}

 

Just like with Picaxe, I will ask you to go and familiarize yourself with Arduino documentation which is available online at arduino.cc.

Now, before we proceed, I’d like to have a few words on C, the programming language for Arduino. It is a high level language, just like Basic, but much more powerful. Its power mostly comes from two elements:

  •     It is closer to lower level assembler language.
  •    The syntax of C is stricter.

Also, please, remember that C is case sensitive, that means “arduino” and “Arduino” are not the same!

 

So, let’s move on. The comments which are in grey will help you a lot as you read through the code. By the way, the next chapter will be dedicated to commenting your code.  The first actual code you will see here is:

void setup()

That is the name of function. And the name “setup” is predefined. This function will be called once on microcontroller power up or reset. Word “void” tells us that function does not return a result. And it takes no parameters. You can tell that from empty brackets. We will have a whole chapter for functions later, but for now, think of them like of instructions we have discussed earlier in previous chapter. The body of the function is enclosed in curly braces and in our case consists of just one line:

                pinMode(13, OUTPUT);    

You have read what pinMode function (yes, it is a function!) does, haven’t you? Good! Now, a little note: every instruction in C has to be terminated by a semicolon, remember that. What we see next?

                void loop()

Yes, that is another function, and it is predefined too. “loop” is being called continuously during microcontroller operation. You should investigate what do functions in loop body do. Now you can see that functionally it is absolutely the same as Picaxe analogue, but note all the differences between implementations! See that C has nicely distinguished structure. In Basic it is rather indistinct. Now, having all this information we can build ourselves an operational cycle diagram to have better understanding:

arduinocycle.png


And an empty, I’d say “template” program for Arduino would look like this:

void setup() {                
  // Initialization instructions
}
 
void loop() {
  // operating cycle instructions
}

 

To summarize: Arduino’s operating cycle is controlled by two predefined functions:

  • setup() is executed once, just in the beginning. Initializations are usually put in the body of this function.
  • Code in loop() is executed continuously while microcontroller is powered up.

See other chapters of this series here.