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.
setup(). Anything that
has to keep happening goes in loop().The sketch
Blink, and what every line of it says
// 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.voidmeans 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 tosetup. 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
13instead. The name is clearer and survives moving to a different board. void loop() {- The other function the board calls for you. When
loopreaches 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.
HIGHputs about 5 volts on the pin,LOWputs 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
ledPinholding 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.
| Rule | Example | What goes wrong |
|---|---|---|
| Statements end with a semicolon | delay(500); | expected ';' before '}' token |
| Capital letters matter | digitalWrite, 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 spaces | ledPin, not led Pin | The compiler reads it as two separate things |
| Text goes in double quotes | "Hello", not 'Hello' | Single quotes mean one single character |
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.
| From | To | Why |
|---|---|---|
| Uno pin 8 | One end of the 220 Ω resistor | The pin that switches on and off |
| Other end of the resistor | Long leg of the LED (the anode) | The resistor limits the current so the LED survives |
| Short leg of the LED (the cathode) | Uno GND | Completes the circuit back to the board |
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.
// 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. 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.
- Make one LED stay on for two seconds and off for a tenth of a second.
- Delete the
pinModeline and upload. Does the LED still work? Does it look the same brightness? What do you think the pin is doing now? - Move
digitalWrite(ledPin, HIGH);fromloop()intosetup()and delete the rest ofloop(). Explain the result in one sentence. - In the two LED sketch, delete one of the
digitalWritelines so only three remain. Predict what happens before you upload, then check. - Drop both delays in the two LED sketch to
1. Look at the LEDs. Are they both on? What is actually happening? - Add a third LED on another pin and make the three of them run as a pattern of your choosing.
- 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?