CanSat Tutorials

Tutorial 2 of 9 · The basics

Your first sketch: Blink, line by line

Every Arduino program has the same two part shape. Here is what each line of Blink actually tells the board to do.

Tutorials 1 2 3 4 5 6 7 8 9

Open Blink again. It is thirteen lines long and almost all of them do something you will use in every sketch you ever write. Read it with the diagram below next to it.

A sketch runs setup once, then loops forever Power on or reset button setup() runs once loop() runs to the end, then starts again FOREVER, UNTIL POWER IS LOST
Every Arduino sketch has this shape. Anything that happens once, such as setting up a pin or starting the serial link, goes in setup(). Anything that has to keep happening goes in loop().

The sketch

Blink, and what every line of it says

Blink, with the comments spelled out
// Blink: one second on, one second off, forever.
// Anything after two slashes is a comment. The board ignores it.
// Comments are for the humans who read your code, including you in March.

void setup() {
  // Runs once, the moment the board powers up or is reset.
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  // Runs over and over, from top to bottom, forever.
  digitalWrite(LED_BUILTIN, HIGH);   // LED on
  delay(1000);                       // wait 1000 milliseconds
  digitalWrite(LED_BUILTIN, LOW);    // LED off
  delay(1000);                       // wait again
}

Upload it. It should behave exactly as the original did, because comments change nothing. Now the important bit: what each line means.

Line by line

// like this
A comment. Everything from the two slashes to the end of the line is ignored by the board. Use them to say why, not what. /* ... */ comments out several lines at once.
void setup() {
Declares a function called setup. The board calls it once, automatically, at power up. void means it hands nothing back when it finishes. The empty brackets mean you give it nothing to work on.
{ and }
Curly brackets fence off a block of code. Everything between setup's brackets belongs to setup. Every { needs a matching }, and that is the mistake you will make most often.
pinMode(LED_BUILTIN, OUTPUT);
Sets up a pin. The Uno's pins can be inputs or outputs and you have to say which. Here pin 13 becomes an output, which means the board will push voltage out of it. The two things in the brackets are called arguments: which pin, and which mode.
LED_BUILTIN
A built-in name for whichever pin the on-board LED is wired to. On an Uno that is 13, so you could write 13 instead. The name is clearer and survives moving to a different board.
void loop() {
The other function the board calls for you. When loop reaches its closing bracket, the board immediately runs it again. There is no way out and there does not need to be one.
digitalWrite(ledPin, HIGH);
Turns an output pin on. HIGH puts about 5 volts on the pin, LOW puts it at 0 volts. Current flows through the LED and it lights.
delay(1000);
Stop and do nothing for 1000 milliseconds, which is one second. The LED stays as it was because nothing changes it. Remember this line, it causes trouble later.
;
Ends a statement, like a full stop. Miss one and the compiler complains about the line after the one you broke, which is why error messages can point at innocent code.
int ledPin = 8;
Not in Blink, but in the next sketch. It makes a labelled box called ledPin holding the whole number 8. Change the number in one place and every line that uses the name follows.

Syntax

Five rules the compiler will not forgive

C++, the language the Arduino IDE uses, is fussy in a few specific ways. Learn these five and most of your errors disappear.

RuleExampleWhat goes wrong
Statements end with a semicolondelay(500);expected ';' before '}' token
Capital letters matterdigitalWrite, not digitalwrite'digitalwrite' was not declared in this scope
Every bracket needs its pair{ }, ( )Errors that point at the very last line of the file
Names have no spacesledPin, not led PinThe compiler reads it as two separate things
Text goes in double quotes"Hello", not 'Hello'Single quotes mean one single character
A habit to start now

Press Verify after every two or three lines you write, rather than at the end. One new error is easy to find. Fifteen at once, in code you wrote twenty minutes ago, takes far longer to untangle.

Your own circuit

Driving an LED you wired yourself

The on-board LED is convenient but it is soldered to the board. A CanSat needs you to control your own parts, so wire up an LED of your own. You need a breadboard, two jumper wires, an LED and a resistor of about 220 ohms.

FromToWhy
Uno pin 8One end of the 220 Ω resistorThe pin that switches on and off
Other end of the resistorLong leg of the LED (the anode)The resistor limits the current so the LED survives
Short leg of the LED (the cathode)Uno GNDCompletes the circuit back to the board
Always use the resistor

An LED straight across 5 volts and ground draws as much current as it can and dies, usually taking a bit of the pin with it. The flat edge on the plastic rim marks the short leg. Any resistor from 150 to 1000 ohms will work, higher values just look dimmer. Each Uno pin can supply about 20 mA, and the whole board about 200 mA, a limit to remember once you start adding parts.

Blink an LED you wired yourself
// The same idea, but driving an LED you wired yourself on pin 8.

int ledPin = 8;        // one place to change the pin number

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  digitalWrite(ledPin, HIGH);
  delay(1000);
  digitalWrite(ledPin, LOW);
  delay(1000);
}

Two LEDs is barely more work, and it is the first sketch where the order of the lines really matters.

Two LEDs taking turns
// Two LEDs taking turns. Pin 8 and pin 9.

int redPin = 8;
int greenPin = 9;

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
}

void loop() {
  digitalWrite(redPin, HIGH);
  digitalWrite(greenPin, LOW);
  delay(500);

  digitalWrite(redPin, LOW);
  digitalWrite(greenPin, HIGH);
  delay(500);
}

One thing to remember

Why delay() will have to go

Now a warning about delay(). While it is counting, the board does nothing else at all. It cannot read a sensor, check a button, or send a radio packet. For a blinking LED, that is fine. Now picture a CanSat falling at 15 metres per second. It has to read two values, build a packet and transmit it, every second. A board that freezes for a whole second at a time would miss its own mission.

There is a standard way round it using millis(), and that is tutorial 4. For now, keep using delay() and know that you will replace it.

Now change it

Change one thing at a time, upload, and watch what the board does.

  1. Make one LED stay on for two seconds and off for a tenth of a second.
  2. Delete the pinMode line and upload. Does the LED still work? Does it look the same brightness? What do you think the pin is doing now?
  3. Move digitalWrite(ledPin, HIGH); from loop() into setup() and delete the rest of loop(). Explain the result in one sentence.
  4. In the two LED sketch, delete one of the digitalWrite lines so only three remain. Predict what happens before you upload, then check.
  5. Drop both delays in the two LED sketch to 1. Look at the LEDs. Are they both on? What is actually happening?
  6. Add a third LED on another pin and make the three of them run as a pattern of your choosing.
  7. Change int ledPin = 8; to a pin you have not wired anything to, and move your wire to match. How many lines did you have to edit?