Monday, 11 May 2020

Hello World and Dice

So - the first project was the standard "Hello World" program - the program that every programmer always writes when they start a new language or framework. 

We went to the make code site and wrote the program. 

Hello World Program

I showed them how to save the .hex file, and then drag it to the micro:bit window's drive to install it onto the micro:bit. 

I then showed them how to pair the micro:bit, so they could download the program directly onto the micro:bit. 

So far so good. 

The next project was to write a dice program, so we can use the micro:bit as a dice. 

When the 'A' button is pressed, we would pick a random number from 1 to 6 and display it on the screen. 

Simple Dice Program

Now - to introduce them to variables and get them used to displaying some pictures on the screen I asked them to design the faces of the dice to get the following code: 

Dice Program with LED graphics

At this point, I asked them to change the code so that we could roll the dice using both the 'A' and 'B' buttons. 

My eldest immediately created an 'On button B pressed' block and copied the code from the 'On button A pressed' block. 
All good - but I tried to get him to see what was wrong with this approach.
What if we wanted to change the LED graphics? We'd need to change it for both the A and the B button every time. 
What if we also wanted to be able to shake the micro:bit to roll the dice. We'd have another copy. 

So I introduced them to functions. Now I've looked at a lot of the tutorials on the microbit.org site, and I don't see much use of functions. 
Which I think is a shame, since putting code in a function is a great habit to pick up early on.  

So I wanted to get into functions as soon as possible, and the dice program was a good place to start. 

We created 2 functions - one to throw the dice, and the other to display the value. This now turned our program into the following: 

Dice Program with Functions


They both agreed that this code looked a lot nicer than the copied code from before. 

The last thing I wanted to do was add a little animation to simulate the dice being rolled. This is where I think I should have tried it out first to see how it looked.  

The idea was to quickly show 10 random dice faces to simulate a rolling dice, and the last one displayed is the one we'll take. However I was expecting the led screen to change quickly in a blur - but it didn't, so the dice throws were a bit slow. 
Maybe a different animation to simulate the dice throw would have been better. However it did get them into loops, so another new structure learnt. 

Repeat 10 times function


The lesson lasted just over an hour, and my youngest was starting to get a bit distracted towards the end, but they both seemed to enjoy it. My eldest exclaimed "Wow I think my Dad actually managed to teach me something!" - so I couldn't complain. 

Next lesson is to move onto the sensors ... 



 











No comments:

Post a Comment