IAN LANG ELECTRONICS

Doorbells are not glamorous. They do however provide an easy yet interesting project . So we are going to do one in both Microsoft Blocks and Python Editors, starting with a basic ding-dong and developing on the fly as we go.

First thing then - when you press button A it makes a ding-dong noise.

To do this you'll need a speaker on the Microbit. You can do it directly like this:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Or using the amp we looked at in the last article:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Here's how we started with BE, which at this point was the easiest way to go. The function of each block is marked up against the block.

Project Sheet 1

bbcmicrobit Go Back

The Doorbell

doorbell

The forever block makes the Microbit loop round the commands inside it. This means it's always looking for trigger events.

Look for button A being pressed and if it is do everything bracketed in here.

Play a note of G# constantly

Wait  for half a second  (500 milliseconds)

Stop playing G# and play E instead.

Wait for three quarters of a second (750 milliseconds)

Stop playing the note

speaker lobit bbcamp

GND OF YOUR

MICROBIT.

Here's how we translated that in Python:

 

from microbit import *

import music

import microbit as m

 

while True:

   if button_a.was_pressed():

         music.pitch(415, 500)

         music.pitch(330, 750)

 

The top three lines import the libraries to make it work. The forever loop in BE is here replicated by the while True: statement and the indentations underneath it, whilst the if button A is pressed is replaced in Python by if button_a.was_pressed():

The timings were kept but the notes were specified by frequency rather than notation.

 

It might be nice to have the LED display show the word "WELCOME" if the button is pressed. In BE we can do it like this:

 

 

 

welcome

You'll notice another block added at the bottom there which is show string "WELCOME" and what this does as an output is to scroll the word "WELCOME" across the LED display after the tones. I particularly want you to notice that it is inside the if block. Were it not then it would continue dong this forever.

 

We can do the same in Python like this:

 

 

from microbit import *

import music

import microbit as m

 

while True:

   if button_a.was_pressed():

         music.pitch(415, 500)

         music.pitch(330, 750)

         m.display.scroll("WELCOME",200)

 

There are two things that need pointing out here with regards to the bottom line which is

 

m.display.scroll("WELCOME",200)

 

The first is to note the indentation. I've banged on at length about what a grammar pedant Python is and the indentation here is to the if ....then statement (if button_a.was_pressed).

The second is that there is a number following the string we wish to display. That's the delay parameter. The bigger the number the slower the scroll speed because what it does is controls how fast the columns of the display iterate in microseconds. If that is as clear to you as mud, change the 200 to 2000. You will see how the columns light and dislight in very slow motion. This is what the scroll is; it takes on the light pattern of the previous column (with the exception of the far right column which is the frst iteration) on every cycle until the scroll is done.

 

If we double the frequencies the notes are playing at, what will we get? Change the lines

 

         music.pitch(415, 500)

         music.pitch(330, 750)

 

to

 

         music.pitch(830, 500)

         music.pitch(660, 750)

 

You end up getting the same thing but at a higher pitch; both notes have gone up one octave. Try halving the values as well.

 

 

Let's have one of those really annoying musical doorbells with two ways to annoy you. If you press one button then one tune pops out, if the other another. Here's Python:

 

 

from microbit import *

import music

import microbit as m

 

while True:

  if button_a.is_pressed():

      m.display.scroll("WELCOME",200,wait=False)

      music.play(music.NYAN)

  if button_b.is_pressed():

      m.display.scroll("WELCOME",200,wait=False)

      music.play(music.ODE)

There are two things to notice about this line:

 

     m.display.scroll("WELCOME",200,wait=False)

 

Firstly, it appears under both if statements. That's not good practice and it's something we can address later. Secondly it has another appendage on it now, which is wait=False.

 

In the scroll function of the display, you can set a number of parameters. We already looked at the delay which is set to 200 milliseconds, but the wait parameter does something a bt different. If you set it to True, it waits until the animation is finished before proceeding. If you set it to False, it does the animation as a background process and carries on with the rest of the script. Note the capitalisations;  

True and False. I didn't. Ten minutes head-scratching and swearing at it before I remembered what a grammar pedant Python is and changed "false" to "False". Honestly. It's just like my old English teacher.

 

Over the page let's do some more electronics. You know you want to. After that we'll see if we cant get it to reflect our mood by changing "WELCOME" to "SOD OFF" and back again.

 

 

 

More >