Picture of Sparkfun RedBoard

Introduction to the RedBoard and Programming

The Sparkfun RedBoard is the device we will use to learn about microprocessors.  The RedBoard is programmed with Arduino and is a great device to learn on.

Our first program is to help us understand how an Arduino sketch works and the two default functions, setup and loop.  The setup function is called only one time when the board is powered up or the reset button is pressed.  The loop function is executed continuously.  When the loop function gets to the end or return is called, the function is called again.

To get started you must have a way to create an Arduino Sketch.  You can download the software at http://www.arduino.cc or use the web-based IDE.  See www.arduino.cc to decide the best option for you.  We will refer to the software you use to write your code the development environment.

Start up your development environment and create a new sketch.  The software will typically have the two functions (setup and loop) already prototyped with no code.  Create your first sketch with the following code:


/*These are global variables. The are known to every function and if any function changes
* the value it is changed throughout the code
*/
int LoopCount=0;
int FirstTime=1;

/*This is the setup function
*/
void setup() {


     Serial.begin(9600); // We will us the serial port to monitor what is happening in the serial monitor
     Serial.println("We're about to get started!!!");

}

/*After setup is called, the loop function is called. once loop gets to the bottom, starts over again.
*
*/
void loop() {
     int a=0;
     if (!FirstTime){
         a=a+1;
         LoopCount=LoopCount+1;
         Serial.print("Not the first time! a= ");
         Serial.print(a);
         Serial.print(" LoopCount= ");
         Serial.println(LoopCount);
     }else{
         FirstTime=0;
         LoopCount=LoopCount+1;
         a=a+1;
         Serial.print("It's the first time! a= ");
         Serial.print(a);
         Serial.print(" LoopCount= ");
         Serial.println(LoopCount);
     }
}

Compile and upload your sketch to the RebBoard then open the serial monitor under the Tools menu.  Unselect Autoscroll.  Now hold down the reset button on your RedBoard and while still holding it down click “Clear Output” on the serial monitor.  The Serial Monitor should be clear of any output.  Now release the reset button and the display inside the serial monitor should begin to fill.

 

Output from the serial monitor

Serial Monitor Display

Note the output:  The first line is “We’re about to get started!!!” which is displayed only once as the setup() function is only called one time per power or reset cycle.

Next, we see “It’s the first time” and local variable a has a value of 1.  Global variable LoopCount also has a value of 1.

On the following lines of output, we see “Not the first time!”.  a is always equal to 1 but LoopCount increments by 1 each time.  What does this tell us?

LoopCount is a global variable.  It is available to all functions throughout the code.  If one function changes the value of LoopCount – the value of LoopCount will be changed for all functions.

The display of “It’s the first time!” and “Not the first time!” is controlled by the FirstTime global variable.  We initialize it to 1.  Once the loop is called the first time we set it to 0.  Take a look at the conditional more closely to understand how this works.

The variable a is a local variable to the loop function.  Each time the function is called the value of a is initialized to 0, then in the conditional, the value of a is incremented by 1.  When the executing code gets to the bottom of the loop() function it is basically stopped and the loop() function is complete.   There is another piece of code that we don’t see that is calling loop() again and restarting it.  Behind the scenes there is a piece of code that looks something like this:


void main(void){
     setup()

     while(1){
          loop()
     }
}