Skip to content

Controlling a microprocessor

To read inputs from sensors, control actuators such as servo motors, or to do anything else with a microprocessor, you need to write some code and upload it.

If you are using the Arduino IDE, the piece of code you write is called a sketch and that is the term used in these notes. In other contexts, it may be called an app or firmware.

The process of uploading a sketch is known as flashing the microprocessor.

Sketch structure

When you write code for a PC, you package it in some way and run it using the operating system. A microprocessor is a much simpler environment - there is no operating system as such, and the only control over the device comes from your sketch.

Another difference is that in a program designed for a PC, an infinite loop is usually a bad thing. It causes the PC to enter a hanging state where it doesn't respond as you expect it to. In contrast, microprocessors are designed to run continuously and they depend on an infinite loop to do this.

There are three main parts to a sketch:

  • Globals: Here you import any required libraries and define global variables

  • Initialisation: Here, you initialise your variables

  • Main loop: This is where your main code goes - notice that it is a loop. There is no need to use any of the usual looping syntax like while or for. This loop works automatically.

Here is an example sketch from the Arduino IDE to illustrate these three parts:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  lcd.begin(16, 2);
  lcd.print("hello, world!");
}

void loop() {
  // Turn off the blinking cursor:
  lcd.noBlink();
  delay(3000);
   // Turn on the blinking cursor:
  lcd.blink();
  delay(3000);
}

Lines 1 - 2 are the global section.

The function setup() contains the initialisation code.

The function loop() contains operational code. In this example, it disables the blinking cursor on an LCD screen and waits for 3s. Then turns the cursor back on and waits for another 3s.

It is important to visualise this as an infinite loop to understand exactly how it will behave. As soon as the second delay(3000) statement terminates, processing continues from line 11. The overall effect is that the cursor disappears and reappears continuously for 3s at a time.

Loops

Many people who are not used to this make the mistake of trying to add their own loops. They do not realise that there is already an assumed loop built into the sketch.

In fact, loops should always be avoided in microprocessors sketches where possible. Another important insight into the way microprocessors work is that the speed of the main loop is determined by the amount of processing being done. Every operation adds to the amount of time that the loop will take, and an additional loop will have the effect of slowing the operation down even further. In the extreme case, it is like the unintended infinite loop in the PC application. Because a microprocessor is a single-threaded environment, a loop will block all other operation until it terminates. If you are wondering why your sketch is not responding to input in the way you expect, it could be that it is busy running a loop, or waiting for some other piece of processing to complete.

The trick is to learn to rely as much as possible on the embedded loop.