Doorbell — Smart Home
Press the button on the board and it plays a doorbell: two notes of your own, then the ready-made doorbell tune.
This is one project for both firmware versions: when MoBot opens it, the blocks write the code for the firmware of your Smart Home.
The program
Section titled “The program”Read the program as text
- MoBot Start
- Print to serial
"Press the button" - repeat forever
- wait 2 seconds
- if is Button pressed ?
- Print to serial
"Ding dong!" - Play Note E5 Duration: 300
- Play Note C5 Duration: 500
- Play sequence : Doorbell
- Print to serial
What you need
Section titled “What you need”- A Smart Home board with the button and the buzzer. Nothing to plug in.
- A USB cable and the serial monitor.
How to run it
Section titled “How to run it”- Open 09-doorbell.mobot in MoBot.
- Plug in, pick the port, press Send to Device.
- Press and hold the button for a moment — about a second.
What you should hear
Section titled “What you should hear”In this order, every time:
[MOBOT] Ding dong!in the serial monitor.- A high note (E5), about a third of a second.
- A lower note (C5), about half a second.
- The doorbell tune — several notes, played by the board on its own.
Then a couple of seconds of quiet before it will ring again.
The test that matters is the order. All four things, every time, in that order. If the two notes play but the tune never does, or the tune cuts itself off half way, something is wrong with the buzzer’s background timer — say so, it is worth knowing.
Hold the button for a moment. The board looks at the button once every two seconds, so a very quick tap can fall in between two looks and be missed entirely. That is not the button failing.
If it rings without anyone touching the button, the button is stuck or
miswired. If nothing happens at all, check the serial monitor: no
Ding dong! means the button is never being seen.
Two kinds of sound block
Section titled “Two kinds of sound block”This is the thing worth teaching from this example.
- Play Note waits. The program stops at that block until the note is over. So the two notes play one after the other with no gap to arrange.
- Play sequence does not wait. It starts the tune in the background and
the program carries straight on. That is why the
wait 2matters: without it the loop would come round, see the button still held down, and start the tune again from its first note — so you would only ever hear the first note.
Why the wait is at the top
Section titled “Why the wait is at the top”Putting the wait at the start of the loop rather than after the ring looks
odd, and it is deliberate.
The two note blocks make the program wait while they sound, but the Play sequence block starts its tune and goes on at once. The two seconds at the top of the loop are what give that tune time to finish before the button is looked at again.
About “Duration”
Section titled “About “Duration””Both note blocks say Duration, and both are in milliseconds. 300 is
about a third of a second; 1000 is one second. The wait blocks next to them
are in seconds. Two different units on two neighbouring blocks — worth
saying out loud to a class before somebody types 1 and hears nothing.
The blocks
Section titled “The blocks”| block | what it does |
|---|---|
iot_button |
True while the button is held down. |
controls_if |
Rings only when the button is down. |
iot_playnote |
One named note, from C4 up to B5. Waits while it plays. |
iot_playsequence |
One of 23 ready-made tunes. Does not wait. |
wait |
Two seconds — the gap between one ring and the next. |
The Python code
Section titled “The Python code”The same blocks write different Python for each firmware, and MoBot picks the one that fits the robot you use. The 2.x tab is for reference. This is a reading copy: editing it changes nothing, so change the blocks instead. The name map lists the names that changed.
# Doorbell — iot (firmware 3.0)## This is what MoBot puts on a robot running firmware 3.0 as main.py when you open# 09-doorbell.mobot and press Send to Device. It is GENERATED from the blocks;# editing this file changes nothing. Edit the blocks instead.# The firmware 2.x version is 09-doorbell.main.py.
from machine import Pinimport timefrom sounds import SEQUENCES
print("[MOBOT]", 'Press the button')while True: time.sleep(2) if home.button.is_pressed(): print("[MOBOT]", 'Ding dong!') home.buzzer.play_note('E5', 300) home.buzzer.play_note('C5', 500) home.buzzer.play_notes(SEQUENCES.get('doorbell'))# Doorbell — iot## This is what MoBot puts on the board as main.py when you open# 09-doorbell.mobot and press Send to Device. It is GENERATED from the blocks;# editing this file changes nothing. Edit the blocks instead.
from machine import Pinimport timefrom sounds import SEQUENCES
print("[MOBOT]", 'Press the button')while True: time.sleep(2) if mobotIOT.button.is_pressed(): print("[MOBOT]", 'Ding dong!') mobotIOT.buzzer.play_note('E5', 300) mobotIOT.buzzer.play_note('C5', 500) mobotIOT.buzzer.play_sequence(SEQUENCES.get('doorbell'))Try changing it
Section titled “Try changing it”- The tune dropdown has 23 choices.
victory,game_over,coinandlevel_upare the ones a class will ask for. - Write a real melody with four or five Play Note blocks in a row — E5, D5, C5, D5, E5 is the start of a song most children know. Keep Play sequence last, or delete it.
- Make the wait 0.5 and the doorbell answers almost instantly — but hold the button down and hear the tune stutter as the loop restarts it.