MakeCode Extension

Click here for English please. Nu volgt eerst de Nederlandstalige versie.






Scratch heeft handige "wacht tot"-blokken die in MakeCode ontbreken. Met een speciale extensie kun je
nu toch zes "wacht tot"-blokken gebruiken in MakeCode (zie plaatje hierboven). Die kunnen handig zijn
bij allerlei spellen, om beveiligingscodes te maken, en om de micro:bit naar Morsecode te laten luisteren.





Wil je deze nieuwe codeblokken zelf uitproberen?

Hier is een link naar een MakeCode-file met daarin de Nederlandstalige codeblokken toegevoegd als extensie en een voorbeeldje dat je kunt testen in de simulator. Daarmee zie je snel hoe de blokken werken:

https://makecode.microbit.org/_PaDJdx89vF06

Je kunt de extensie ook in MakeCode toevoegen aan een nieuw of bestaand project. Dat doe je door in MakeCode op uitbreiding te klikken, en dan "wait until" in te typen of door deze link naar het zoekveld te kopiëren:

https://teacherpinky.github.io/Wait-Until-Blocks/



Wacht tot pin wordt losgelaten

MakeCode heeft een handig codeblok om te detecteren dat een stroomkring kort gesloten wordt. Dat blok, dat hiernaast is afgebeeld, werkt echter alleen als de stroomkring minder dan 1 seconde gesloten blijft, zoals te lezen valt op deze webpagina https://makecode.microbit.org/reference/input/on-pin-pressed.

Als je een flipperkastje maakt, waarbij een bal de stroomkring sluit en dan weer snel terugrolt, werkt dat roze "wanneer pin wordt aangeraakt"-blok prima.

Je kunt echter niet goed uit de voeten met dit codeblok als je een spel hebt waarbij de stroomkring langer gesloten wordt, bijvoorbeeld bij een basketbalspel waarbij je een geleidende bal in een bakje gooit en de bal in dat bakje blijft liggen totdat je hem er weer uitpakt. Dat kost meer dan 1 seconde.

Ook bij spellen waarbij de bal niet meteen stil ligt, maar eerst een beetje stuitert en de stroomkring dus wel gesloten wordt, maar tussendoor ook nog enkele keren niet gesloten is, is het blok "wanneer pin wordt aangeraakt" niet zo handig om te gebruiken. Dan loop je namelijk het risico dat een speler met één worp meer punten verdient dan de bedoeling was.

In het filmpje hieronder zie je enkele voorbeelden van zulke spellen:



Bij deze spellen kun je wel detecteren dat de stroomkring gesloten wordt met "als pin wordt ingedrukt dan" (zie code hieronder).

Bij een spel waarbij de speler een punt krijgt als er een pin wordt ingedrukt, stuit je dan echter op een nieuwe lastigheid. Je wilt niet dat een speler door één keer een bal in een bakje te gooien meteen een heleboel punten verdient. Daarom is het handig om het spel zo te coderen dat je pas weer een nieuw punt kunt verdienen als de bal eerst uit het bakje is gehaald.

En daar komt dit nieuwe codeblok om de hoek kijken. Daarmee kun je de micro:bit laten wachten totdat de stroomkring weer onderbroken wordt. Dan wordt bijvoorbeeld Pin1 weer losgelaten. En zo voorkom je dat een speler veel te veel punten krijgt.



In de code hierboven zie je dat het nieuwe blok een ingewikkelder stukje code kan vervangen, wat handig is voor kinderen (en volwassenen) met weinig programmeerervaring. Het nieuwe paarse blok doet hetzelfde als de terwijl-lus die ernaast staat.

De wachttijd na het nieuwe "wacht tot"-blok zorgt er voor dat de speler geen punt krijgt als hij/zij per ongeluk de stroomkring kort nog even opnieuw sluit tijdens het uit het bakje halen van het balletje.

Met deze code is op een behoorlijk betrouwbare manier de score bij te houden in een spelletje. Voorwaarde is wel dat de bal zwaar genoeg is om de stroomkring goed te sluiten. Een lichtere bal kan korte periodes het contact verbreken, wat de betrouwbaarheid van de puntentelling niet ten goede komt.



Wacht tot knop is ingedrukt

Een misschien nog wel simpelere oplossing is om het nieuwe "wacht tot knop is ingedrukt"-blok te gebruiken. Ook met onderstaande code kun je voorkomen dat een speler te veel punten krijgt.

In dit geval moet de speler zelf op knop A drukken nadat hij de bal uit een bakje of uit een doel heeft gehaald. Zolang dit niet gebeurd is, zal de micro:bit geen nieuwe punten toekennen als de stroomkring bij pin 1 gesloten is.



Het "wacht tot knop is ingedrukt"-blok en het "wacht tot knop is losgelaten"-blok zijn samen ook heel handig als je een alarm wilt maken, waarbij de eigenaar een geheime code heeft om het alarm uit te zetten. De micro:bit kan met de code hieronder controleren of bij een rijtje van bijvoorbeeld vier A's en B's de A's en B's in de goede volgorde staan.

Je kunt dit idee bijvoorbeeld ook gebruiken voor een geheime code die een speler nodig heeft om toegang te krijgen tot een spel.






Wacht tot geluidsniveau hoger/lager is dan



Met deze blokken kun je onder andere eenvoudige code maken om de micro:bit V2 te laten schrikken als je hard boe roept.

Deze codeblokken werken alleen met de luidspreker van de nieuwe micro:bit.


Het laatste paarse blok is verder bijvoorbeeld ook te gebruiken als je Morsecode in de vorm van geluid in Morsecode in de vorm van streepjes om wil zetten.

Om dat te kunnen doen, moet de micro:bit eerst de lengte (tijdsduur) van een geluid bepalen. Dat kan met de microfoon van de nieuwe micro:bit (versie 2) en de code die hiernaast staat.

Bij deze code kijkt de micro:bit als het ware op de klok als het geluid boven een bepaald niveau uitkomt. In dit geval is 80 gekozen als drempelwaarde.

Het blok "looptijd (ms)" houdt bij hoe lang de micro:bit al aanstaat. Door de looptijd waarop het geluid begint te vergelijken met de looptijd wanneer het geluid eindigt, kun je de lengte van het geluid bepalen. En om te bepalen wanneer het geluid eindigt, kun je het nieuwe paarse "wacht tot"-blok gebruiken.

Zodra je de lengte van het geluid weet, kun je onderstaande code gebruiken om te bepalen of het een kort geluid was (kort streepje) of een lang geluid (lang streepje).






































Het meten van de lengte van een geluid kun je ook gebruiken als je hele eenvoudige stemcontrole voor een micro:bit-robot wilt maken. Je kunt dan een tweede micro:bit (met microfoon) als afstandsbediening gebruiken (door radiosignalen naar de micro:bit in de robot te sturen). Dan kun je een programmaatje maken waarbij je bij een kort geluid het signaal voor linksaf naar de robot stuurt, bij een iets langer geluid het signaal voor rechtsaf en bij een nog langer geluid het signaal voor rechtdoor.




Status van de extensie

De "wacht tot"-blokken zijn voor mij gemaakt door een aardige Scratcher die uit de voeten kan met typescript. Sinds 30 september is het een offiële extensie, goedgekeurd door de Micro:bit Foundation en MakeCode. Dat betekent dat je hem kunt vinden door de trefwoorden "wait until" in de zoekbalk in te typen nadat je op "geavanceerd" en daarna op "uitbreidingen" hebt geklikt in MakeCode.

Lukt dat niet? Dan kun je hem ook vinden door deze link in de zoekbalk te plakken:

https://teacherpinky.github.io/Wait-Until-Blocks/

TIP: het kan zijn dat je in eerste instantie de Engelstalige blokken te zien krijgt. Dit is te verhelpen door de taal even te veranderen naar een andere taal dan Nederlands of Engels en daarna weer terug naar het Nederlands te gaan.





ENGLISH







Scratch has useful "wait until" coding blocks that MakeCode doesn't have. With a special extension, however, you can use six "wait until" blocks in MakeCode (see picture above this text). These can be useful when coding games made out of cardboard, Lego or wood, to make an alarm and to make a Morse code listener.





Want to play around with the new coding block?

This is the direct link to a MakeCode file with the extension and an example code that you can easily test in the simulator and gives you a sense of how the blocks work:

https://makecode.microbit.org/_8yU4J9f8w3Wv

You can also type "wait until" in the search field that you get after clicking on "advanced" and "extensions" in MakeCode to add the "wait until" blocks to a new or an existing project.

If that doesn't work, please copy this link in the search field.

https://teacherpinky.github.io/Wait-Until-Blocks/




Wait until pin released

In a lot of my microbit games the player scores a point when a conductive ball closes an electrical circuit connected to P1 or P2 and the GND pin. When the circuit stays closed for more than 1 second, you can't use the regular "On pin pressed" block (see picture on the right), because in order to get this block activated the pin needs to be released within 1 second, as you can read on this webpage https://makecode.microbit.org/reference/input/on-pin-pressed.

The "On pin pressed" block works fine for a pinball game in which a ball closes the circuit and quickly bounces back again or if you push a button that closes the circuit for a short while. However, if you make a basketball game e.g. in which the ball stays in the basket until you get it out, you need other coding blocks.

Also when the ball bounces in the basket for a little while the "On pin pressed" block can cause trouble, because then the circuit opens and closes a few times. This way the player can get more points than intended.

This video shows you some examples of games in which the circuit is closed for more than one second:



In these games you can detect if the circuit is closed by using "if pin is pressed then" (see code beneath).

In a game that gives a player a point when a pin is pressed, for example because a conductive ball closes the circuit at Pin 1, you encounter a new problem. You have to prevent that the player gets a lot of points by throwing the ball in a basket just one time. Therefore it is useful to add coding blocks that make sure that the player is only able to gain new points after he/she has taken the ball out of the basket.

The new coding block can make this happen. You can use it to tell the micro:bit to wait with executing the next lines of code until the circuit is broken again, because the player took the ball out of the basket. Then the pin is released. This way a player can't get too many points at once.


In the code example above you can see that the new coding block can replace a more complicated part of the code, which is useful for kids (and adults) with little coding experience.

The pause block beneath the new "wait until" block makes sure that the player doesn't get an extra point while getting the ball out of the basket. When doing that you can accidentally close the circuit again for a short while. If this happens, no extra point is gained because of the extra waiting time.

This code is pretty reliable for keeping the score. However, the ball you use must be heavy enough to close the circuit all the time. When a ball is too light, it doesn't make contact with both pins all the time, which makes the score less reliable.



Wait until button is pressed


Using the new "wait until button is pressed" block gives another option that might be even more easy to understand for kids. The following code gives another option to make sure the player doesn't get too many points at once.

In this case the player has to press button A themselves after getting the ball out of a basket. As long as that hasn't happened, the micro:bit won't add points to the score when the circuit between Pin 1 and GND is closed.



Without the "wait until button is pressed" block you would need to use code that's way more complicated for people who're just starting to learn to code. You can for example use a variable that's only 1 when the basket is empty. If this variable doesn't have the value 1, you can't score a point. And pressing button A, after getting the ball out of the basket, sets the variable to 1 again.



The "wait until button is pressed" and the "wait until button is released" blocks are also perfect for making an alarm.

You can turn the alarm off by pressing button A and B in the right order. You can also use it as a secret code that the user needs to give before being able to start a game.

Here's a simple example of how this block can be used. Coding it this way makes it understandable for kids in my opinion.




Wait until sound level above/below


These coding blocks can be used to give kids an easy option for coding a micro:bit V2 that gets scared when you yell boo.

These coding blocks can only be used with the microphone of a new micro:bit.



The last purple custom blocks can also be used for a Morse code listener that can transform sounds into the characters . and -

To do this the micro:bit needs to measure the length of a sound (of a tone). This can be done with the microphone on the new micro:bit (version 2) and the code on the right. You can use one or two of the new "wait until" blocks.

While running this code the micro:bit is sort of telling the time at the moment it hears a loud sound, by using its internal clock.

The "running time" code block is a variable that stores the time elapsed in microseconds since the power of the micro:bit was turned on. By comparing the running time at the start of a loud sound with the running time at the end, you can calculate the length of the sound. And to make the micro:bit notice when the sound stops, you can use the new purple costum block.

And when the micro:bit knows the length of the sound, this code can be used to transform a sound into the characters . and -





































Measuring the lenght of a sound can also be used to make a simple voice control for a micro:bit robot. In this case you can code your remote control (a micro:bit that controls the robot by sending radio signals) in such a way that it tells the robot to turn left when you make a short loud sound, to right when you make a longer loud sound and go forward when you make a long loud sound.



Status of the extension

The "wait until" costum blocks are made for me by a kind Scratcher who knows typescript. It's an official extension approved by the Micro:bit Foundation and MakeCode since September 30th. So you can find it when you click extensions in MakeCode and search for "wait until".

If this doesn't work, you can also add the extension by copying this link in the extension search field:

https://teacherpinky.github.io/Wait-Until-Blocks/




Dreams for the future

It would be cool if MakeCode could provide a general "wait until" block like is available in Scratch, a block that allows you to make other options too, for example with the analog read pin blocks.






Want to share ideas about this topic?

You can find me on Twitter or mail me at PinkyPepper.Scratch at gmail.com.