Serial.begin(9600) in setup() opens a line to the computer; Serial.println(level) sends a value and starts a new line, and Serial.print stays on the same line. The serial monitor shows what the board sees, which is how you test and debug a sketch. In C++, "words" + a number does not join them.
Your sketch decides from a number you can't see. When the LED does something odd, how do you find out what the board actually read?
The serial monitor shows text the board sends over the USB cable. On this page it is under the board; in Tinkercad and the Arduino IDE it is a panel you open.
void setup() {
Serial.begin(9600); // open the line to the computer (9600 bits a second)
}
void loop() {
Serial.print("Level: "); // words, and stay on this line
Serial.println(level); // the number, then a new line
}
Predict: Read the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.
Try: Try it on the board and compare the result with your prediction.
Investigate: Work out why it happens, one step at a time.
Modify: Change the problem on purpose and see what happens.
Practise: A few on your own, to check you have it.
Make: Make your own problem using what you have learned. Expect a few tries; that is normal.
WidthMore contexts, same idea.
Make your prediction first. Help opens once you've locked it in.
PredictRead the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.
What appears?
TryTry it on the board and compare the result with your prediction.
Try: watch the monitor
Try it yourselfYour edits are saved in this browser.
Upload and run, then turn the knob and watch the monitor.
Use the board. This ticks when you get there.
InvestigateWork out why it happens, one step at a time.
Worked example: print and println
Worked exampleBefore each step, say what you think comes next.
Serial.begin(9600);In setup(): open the line. Nothing prints without it.
Serial.print("Level: ");Send the words. The cursor stays on this line.
Serial.println(level);Send the value of level (512), then a new line.
monitor: Level: 512One line per loop().
Now pick the pair that does this.
Sam's monitor shows bits of Level: and then nonsense instead of Level: 512. Click the line where Sam's sketch goes wrong.
ModifyChange the problem on purpose and see what happens.
Vary: change one thing
Try it yourselfYour edits are saved in this browser.
Change the sketch so each line reads Level: and then the number.
Use the board. This ticks when you get there.
This sketch prints nothing. Fix it.
Use the board. This ticks when you get there.
Modify TypicalChange the problem on purpose and see what happens.
Build it in Tinkercad
PractiseA few on your own, to check you have it.
Printing
MakeMake your own problem using what you have learned. Expect a few tries; that is normal.
Print a percentage
Try it yourselfYour edits are saved in this browser.
Need a hint?
map(value, fromLow, fromHigh, toLow, toHigh) rescales a number.
Another hint
int percent = map(level, 0, 1023, 0, 100);
Another hint
Serial.println(percent); and test at both ends of the knob.
Print the knob as a percentage, 0 to 100, one number per line.
Use the board. This ticks when you get there.
For teachers
Students open the serial line in setup(), print a knob's reading every loop and watch it change, build lines with print and println, meet C++'s "words" + number trap (it moves along the text instead of joining), fix a missing Serial.begin, and print a percentage with map. About 40 minutes.
I can use printing to find out what my sketch sees.
Before class
The board behaves like the real one: nothing prints without Serial.begin, and "words" + a number moves along the text (pointer arithmetic), printing the end of the words or nonsense. A real board would print whatever happens to be in memory; ours prints a few shaded blocks.
The baud rate (9600) must match the monitor's setting on real hardware; the board here ignores it.
Running the lesson
Predict: ask why it is not 'level'.
Find the mistake: show that Python would join them; C++ doesn't.
Practise, debugging: link back to the night light.
The hard part (on purpose)
Python habits: + to join text. Show what the board really prints, then the two-line fix.
When students get stuck
Nothing prints. Ask: Which line opens the line to the computer? Fix: Serial.begin(9600); in setup().
All the numbers run together on one line. Ask: Which call starts a new line? Fix: println.
Stretch and support
Stretch: Print Level: 512 (dark) or Level: 512 (light) depending on a threshold.
Support: A card: print = same line, println = new line, begin first.
Where it leads
Next: Outputs that vary (analogWrite and PWM).
Code: micro:bit and the computer uses the same serial idea from MakeCode.
what appears?: Committing to what println(level) shows tests the new move: the value is sent, not the variable's name.
Try: watch the monitor: Running it and turning the knob settles the prediction: the monitor tracks the reading.
Worked example: print and println: Tracing print and println line by line models how lines are built; Sam's + is planted after the method, with what the real board does.
Vary: change one thing: Adding a label shows print and println together; removing Serial.begin shows why it is in setup().
Build it in Tinkercad: Tinkercad's serial monitor is the one students will use for the real board, so they practise opening and reading it.
printing: Predicting monitor output from print and println, and using printing to debug.
print a percentage: Printing a calculated value (with map) combines a variable, a calculation and println: the monitor then shows the knob as people think of it.