← Teacher guide

Talking to the computer

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.

Arduino · Writing C++ · about 40 minutes · Open the student lesson

Classroom resources

Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Try 5 min · Worked example 9 min · Vary 8 min · Tinkercad 8 min · Practise and Make 10 min

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

  1. Predict: ask why it is not 'level'.
  2. Find the mistake: show that Python would join them; C++ doesn't.
  3. 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

What you will seeA question to askThe fix
Nothing prints.Which line opens the line to the computer?Serial.begin(9600); in setup().
All the numbers run together on one line.Which call starts a new line?println.

Answers to the checks

  1. what appears?: The knob is halfway. loop() runs Serial.println(level); after level = analogRead(A0);. What does the serial monitor show?
    512 (halfway on 0 to 1023), one line every time loop() runs. println prints the variable's value, not its name.
  2. Try: watch the monitor: Upload and run, then turn the knob and watch the monitor.

    Each line is the reading at that moment: the monitor shows what the board sees.
  3. Build a line: Which pair of lines prints Level: 512 on one line, then starts a new line?
    Serial.print("Level: "); Serial.println(level);
    print stays on the line; println ends it.
  4. Find the mistake: Sam's monitor shows bits of Level: and then nonsense instead of Level: 512. Click the line where Sam's sketch goes wrong.
    line 9
    Line 9: in C++, "Level: " + level doesn't join them (as Python would); it moves level characters along the text and prints what is there. Use Serial.print("Level: "); then Serial.println(level);
  5. Add a label: Change the sketch so each line reads Level: and then the number.

    Serial.print("Level: "); then Serial.println(level);
  6. Nothing printed: This sketch prints nothing. Fix it.

    Serial.begin(9600); was missing from setup(). The line to the computer must be opened first.
  7. Print and println: loop() is Serial.print(1); Serial.print(2); Serial.println(3);. What does the monitor show after two loops?
    123 on one line, then 123 on the next
    print, print, println: one line of 123 per loop.
  8. Debugging: A night light comes on in daylight. What is the most useful first step?
    Print the reading and compare it with the threshold
    Printing the value shows whether the reading or the threshold is the problem.
  9. print a percentage: Print the knob as a percentage, 0 to 100, one number per line.

    map(level, 0, 1023, 0, 100) turns 0–1023 into 0–100.

Solutions

One possible solution for each task. Other correct programs are fine.

Vary: change one thing › Add a label

Open task: any program that does what the step asks.

Vary: change one thing › Nothing printed

Open task: any program that does what the step asks.

print a percentage

Open task: any program that does what the step asks.

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.

Exit ticket

Write the two lines in loop() that print Temp: and then the value of temp on one line.

Serial.print("Temp: "); Serial.println(temp);

What each step is for

Where it leads

Next: Outputs that vary (analogWrite and PWM).

Code: micro:bit and the computer uses the same serial idea from MakeCode.

Sources