6
Unit A · From blocks to text
From an empty file
What we are making: whatever you choose — but you open an empty file
and write it from nothing. No block column, no starter code.
The idea
For five sessions the block you knew has sat next to the line you were learning. Today the
left column goes away.
You are not missing anything. Everything you need is in the five sessions behind you, and
this page is all of it on one page.
| In blocks | In C++ | Worth knowing |
|---|
| when green flag clicked | void setup() { } | runs once at power-on |
| forever | void loop() { } | runs over and over |
| set pin 9 to output | pinMode(9, OUTPUT); | declare it before you use it |
| turn on pin 9 | digitalWrite(9, HIGH); | HIGH is on, LOW is off |
| read pin 2 | digitalRead(2) | gives back HIGH or LOW |
| wait 1 seconds | delay(1000); | milliseconds, so 1000 is one second |
| set x to 5 | int x = 5; | int only the first time |
| if ... else ... | if (...) { } else { } | == compares, = assigns |
| repeat 5 | for (int i = 0; i < 5; i++) { } | counts from 0 |
| repeat until | while (...) { } | when you do not know how many |
| define myThing | void myThing() { } | call it with myThing(); |
| say hello | Serial.println("hello"); | needs Serial.begin(9600) in setup |
Everything from Unit A. Fold this corner — you will come back to it all year.
Pick one
All of these use only what is on the page opposite. Pick the one you can describe in a single
sentence.
- Reaction timer — the LED lights after a random wait; time how long until the button is pressed, and print the milliseconds.
- Traffic light with a crossing — your Session 5 light, but a button request gives pedestrians their turn.
- Morse code — blink a word in dots and dashes. Write a
dot() and a dash() function. - Press counter — count button presses, then blink that many times when you hold the button down.
- Dice — press the button, get a number from 1 to 6, blink it out.
- Your own — take it to your teacher first.
💡 Tip
Two things you have not been given: random(1, 7) gives a whole number from 1 to 6, and millis() gives the milliseconds since the board started. Look them up if your choice needs them. Looking things up is the skill, not a shortcut.
How to start from nothing
- Write the sentence first. On paper: it does ____ when ____.
- Open a new file. Type the empty setup() and loop() skeleton from memory.
- Add your variables at the top. Pins first.
- Fill in setup(): every pinMode, and Serial.begin if you are going to print.
- Write the smallest loop() that proves one thing works. Compile. Upload.
- Add one thing. Compile. Upload. Repeat until it is finished.
⚠ Careful
Compile every three or four lines. You are writing from nothing for the first time and you will make more mistakes than usual. Compiling often means every error has one obvious cause.
When you are stuck
Being stuck is normal, and this is the session where it happens most. In order:
- Read the error. The first one only. Line number, then the line above it.
- Check the page opposite. Nearly every mistake today is a missing semicolon, a capital letter, or a bracket.
- Print something. A println tells you whether the program reaches a line at all.
- Make it smaller. Comment out half with //, get the other half working, then bring it back.
- Then ask. And when you ask, say what you expected, what happened, and what you have already tried.
Printing 'got here' is the oldest debugging trick there is
got here 1
got here 2
(nothing after this, so the program never reached line 3)
💡 Tip
Printing to find out how far your program gets is not a beginner's trick. It is what professionals do first, every time, before anything cleverer.
Show it
Last fifteen minutes. Swap with another team. They run your program and try to break it; you
do the same to theirs.
- Hand over. Say nothing.
- Watch what they press, and in what order.
- Write down anything that behaved unexpectedly.
- Fix the worst one before you pack away.
End of Unit A
- You can read and write C++ for everything you could build in blocks.
- You can read an error and fix it without help.
- You use named variables instead of the same number in six places.
- You can write a function and call it.
- You started from an empty file and finished something.
Unit B takes all of that and puts wheels on it.
Challenge optional — only if you finish early- From an empty file, make an LED blink in a pattern nobody else in the room has used.