Good morning everyone and welcome to part 2 of our Christmas project!
A quick note for parents
We had a very large email newsletter list (over 500 addresses!) and very few people reading them so we decided to clean out our list so that only people who wanted to hear from us would get our emails. We didn’t want to be spamming anyone! So if you previously signed up to our newsletter, you’ll need to resubscribe. You can do this here: http://eepurl.com/2wj0D
Secondly, we have launched our Annual Survey in order to get some feedback on our sessions this year, but particularly in the last term. We would much appreciate if you could take a few minutes to complete this at some stage before the next session. The survey is available here: https://docs.google.com/forms/d/e/1FAIpQLSeNviaNSEtIJMn69Mm_zsPZ-LHRHT352jTSJ7gSkJjJi6EmyQ/viewform
Micro:Bit Christmas Tree
Well done to everyone who got their Micro:Bits to light up at the same time during the last session. Now it’s over to the hard part – getting them all to play music at the same time!
So I know a lot of you got your Micro:Bit to play Jingle Bells but just couldn’t get it to play at the same time as your friend’s one. After a lot of headscratching, brainstorming and help from Elliot we found the cause and started working on a solution.
When a Micro:Bit started, it would immediately start to play Jingle Bells and send signals to other Micro:Bits. When another Micro:Bit started, it would receive the signals and also start to play Jingle Bells. When the third Micro:Bit started, it would also receive the signals and start the play Jingle Bells. The problem was that the Micro:Bits all received the signals at different times and when they did, they always started at the beginning of the song, even though any other connected Micro:Bit had already started and might be one the second or third line of the song.
Micro:Bit 1: STARTS Jingle bells, Jingle bells, Jingle all the way! Oh what fun it is to…
Micro:Bit 2: STARTS Jingle bells, Jingle bells, Jingle all the way! Oh…
Micro:Bit 3: STARTS Jingle bells, Jingle bells, Jingle
After some discussion we decided that we needed a conductor to make sure that all the Micro:Bits were playing the same part of the song at the same time.
What we have done is created two programs – one which will go on just one Micro:Bit and one which will go on the other 19.
The first program is the conductor. It controls what line of Jingle Bells should be currently played by broadcasting a signal to the other Micro:Bits what line it wants them to play.
The other program is for the other 19 Micro:Bits. This program waits for a signal from the conductor and plays the correct line of song based on the number it has received. This means that even if a Micro:Bit joins in late, it will join in at the same part of the song that the other Micro:Bits are playing.
We’ve been working on these all week and haven’t quite got it right yet so we’re hoping you awesome coders will be able to help us figure it out!
First of all, here is a quick refresher of what functions are and why we use them:
“As you all know, computer programs are a set of instructions that tell a computer what to do. Instructions can get very long and complicated. If we had to give instructions to each other the way we need to tell a computer,we would never get anything done!
For example, an instruction your mum might give you is “Please go to the shop and get a pint of milk for me.” That is probably all you would need to know. But if your mum was to tell a computer this she would have to say “Walk to the front door (and tell the computer where the door is) and walk to the shop (and give exact directions to the shop). Enter the shop (It needs to know how to open the door!) Get a pint of milk – it needs to know what is milk? Where will it find it? How will it pick it up? Go to the till (Where is the till?) Pay for the milk – how does it do this? etc. So you can see that very quickly computer programs can get very complicated. To deal with this, we use functions.
We create a function called GoToTheShop and code it with all the instructions needed for the computer to go to the shop. We can also create a function called GetMilk which has the instructions for finding the milk, PayForItems which has the instructions for how to pay and ComeHome which has the instructions for finding your house again.
This makes it simpler because now we can say to the computer – GoToTheShop, GetMilk, PayForItems, ComeHome.
This is much easier than listing out all the instructions individually every single time. We can also reuse the functions so maybe the next day our instructions could look like GoToTheShop, GetBread, PayForItems, ComeHome.” – https://derrycoderdojo.wordpress.com/2017/11/03/saturday-4th-november-2017/
We use functions when we are going to to the same thing lots of times in the one program. In this program we have created a function called sendSignal. Now instead of having to create 4 new blocks every time we want to send a signal to the other Micro:Bits we just call the sendSignal function.
This on start block is similar to the one we have been using for our lights. It makes sure that all the Micro:Bits are communicating on the same channel.
This is our sendSignal function. Everytime sendSignal is called, it does the same thing. It sends “Start” to the other Micro:Bits then it adds one to the Bar counter variable. The Bar counter is used to say which line of the song should be played. It is sent to the other Micro:Bits and also to the LEDs of the Conductor so that we can watch what it is sending out.
This very long block of code is the Conductor following the song. It doesn’t make any sound itself, it just controls what part of the song the other Micro:Bits should be at and sends the signals by calling the sendSignal function.
****HANDY HINT ****
If you want to work on your next program without closing your Conductor program. Work on your next program in a different browser. e.g. Microsoft Edge or Chrome.
The program for the 19 musicians is a little bit more complicated.
First we need to make 14 functions – one for each line of the song. Start with line0 and create one for every number up to line13:
Our next step is to code each function with the notes for that line. Each function plays its own line of the song then calls the function for the next line.
We also need our on start block which tells the Micro:Bit what channel to operate on.
We have one more block to add now, and it’s a big one, but an easy one! This block tells the Micro:Bit what to do every time it receives a signal from the conductor.
In theory, this should all be working. (This is said a lot when we are coding!) It’s nearly there, but it’s just not quite in sync yet. Have you any ideas how we can get it together?
Get together in groups of 3-4 ninjas and put the conductor code on one Micro:Bit and the musician on the others. You might need to spread out across the room a bit to make sure other groups aren’t communicating with yours. Or you can change the Set group block to something other than 12 that only your group will use.
For a bonus activity today, use TinkerCAD to design your own Micro:Bit Christmas Tree.
If you are under the age of 13, ask your parent/guardian to create an account on https://www.tinkercad.com/
Complete the short tutorials and get stuck into designing your tree.
Some things to consider:
- There are 20 Micro:Bits that need added to the tree
- How will the tree stand?
- How will the Micro:Bits be attached to the tree?
- Will there be other decorations on the tree?
You never know – we might use some of your ideas when building our tree!
That’s all from us today. Our next session on Saturday 16th December will be our final session of the term. We will be celebrating Computer Science Education Week by hosting our annual #HourOfCode session. Some exciting tutorials have been launched by Code.org for this year’s activities! We can’t wait! We hope to see you there.