Level 2Unit A · Session 3
3

Unit A · From blocks to text

Variables

What we are making: the button-and-LED circuit from Level 1, written so that changing a pin means changing one word, not six.

The idea

You used variables last year — the orange blocks, the score in your game. A variable is a box with a name that holds a value.

In C++ there is one extra step: you have to say what kind of thing the box holds before you use it. That is what int means — a whole number.

The block you already know
The line that does the same thing
set score to 0
int score = 0;
change score by 1
score = score + 1;
set ledPin to 9
int ledPin = 9;
turn on pin ledPin
digitalWrite(ledPin, HIGH);
Same idea. The new part is the word int, which says what kind of box it is.
In blocksIn C++Worth knowing
set x to 5, first timeint x = 5;int means it holds a whole number
set x to 5, after thatx = 5;no int — the box already exists
change x by 1x = x + 1;read it, add one, put it back
a box for decimalsfloat t = 21.5;float holds numbers with a point in them
a yes/no boxbool ready = true;bool holds only true or false
The kinds of box. You will use int nearly every time.
⚠ Careful
int ledPin = 9; the first time, and just ledPin = 9; after that. Saying int twice is like telling somebody your name twice in one sentence — the compiler will object.

Why it matters more in text than it did in blocks

Look at this program. The number 9 appears three times.

Three nines, and no way to know they mean the same thing
1void setup() {
2 pinMode(9, OUTPUT);
3}
4 
5void loop() {
6 digitalWrite(9, HIGH);
7 delay(500);
8 digitalWrite(9, LOW);
9 delay(500);
10}

Now move the LED to pin 6. You must find and change all three, and if you miss one the program still compiles perfectly and simply does not work. That bug is genuinely hard to find because nothing is wrong with the code — it is just about a different pin.

One place to change
1int ledPin = 9; // the name, declared once
2 
3void setup() {
4 pinMode(ledPin, OUTPUT);
5}
6 
7void loop() {
8 digitalWrite(ledPin, HIGH);
9 delay(500);
10 digitalWrite(ledPin, LOW);
11 delay(500);
12}
💡 Tip
A good variable name says what the thing is for. ledPin is good. x is bad. You will read this code again in March and x will mean nothing to you.

Wire it up

1
Your LED circuit
1
Pushbutton
1
Resistor 10kΩ
2
Jumper wires
+−+−jihgfedcba151015202530+92GND
The Level 1 Session 3 circuit: LED on pin 9, button on pin 2 with a pull-down.
Pins used925VGND

The code

Your program
1int ledPin = 9;
2int buttonPin = 2;
3int buttonState = 0;
4 
5void setup() {
6 pinMode(ledPin, OUTPUT);
7 pinMode(buttonPin, INPUT);
8}
9 
10void loop() {
11 buttonState = digitalRead(buttonPin);
12 
13 if (buttonState == HIGH) {
14 digitalWrite(ledPin, HIGH);
15 } else {
16 digitalWrite(ledPin, LOW);
17 }
18}
  • Line 11 — digitalRead asks the pin what it is seeing and puts the answer in the box.
  • Line 13 — == compares. = assigns. This catches everybody, including professionals.
⚠ Careful
= means 'put this in the box'. == means 'is this the same as that?' Writing if (x = 5) puts 5 in x and then always runs the if. It compiles without a single warning and behaves very strangely.

Try it

  1. Upload. Hold the button; the LED lights. Let go; it goes out.
  2. Move the LED to pin 6. Change one line. Confirm it still works.
  3. Add int count = 0; at the top and make it count presses. Print the count with Serial.println(count);.

What you learned

  • A variable is a named box, same as last year.
  • In C++ you say what kind of box it is: int, float, bool.
  • Declare it once with the type, then use the name.
  • One name in one place beats the same number in six places.
  • = assigns. == compares.
Challenge optional — only if you finish early
  • Use a variable to make the blink speed change every time round the loop.
2. Reading an error without panicking