Python microbit basics: buttons and touch pins

Extend it with code
How can you improve it?

If you’ve made it this far then you should be up for a challenge. You’ve got the hang of buttons and touch pins so why not attempt the following challenges:

  • Write a program that shows a happy face if you push one button and a sad face if you push another.

    Hint: Use the documentation to remind you how to show an image. You might want to put your code in a while True  loop so that it keeps repeating.

  • Make a countdown timer where you use the buttons to set the time then touch pin0 to count down to 0.

    Hint: Use sleep(1000)  to make your program pause for a second. You’ll need a variable that stores the countdown value.

  • Adapt the micro:vote counter from the previous page so that you can choose any of 4 options with one button then select your choice with the other button.

    Hint: You’ll need a variable that stores the option that the user has chosen and you’ll need to store the number of votes for each option separately. You could do this with separate values or a list.

5 thoughts on “Python microbit basics: buttons and touch pins

  • December 13, 2016 at 8:50 pm
    Permalink

    Wow – love this simulator. Haven’t managed to get into coding the micro:bit with anything other than the block editor up til now – this is LOADS better!

    Here’s my first program, part of a Christmas display I’ve stuck on my classroom door here in Cambodia:
    https://create.withcode.uk/python/NP

    Press ‘A’ to announce the scroll the words of the angel.

    Press ‘B’ to play ‘Away in a manger’ one note at a time (had to add the durations to every note, doesn’t seem to ‘hold’ the previous value whilst playing single notes otherwise – bug or feature?)

    Q. How do I press A+B in the simulator?

    Reply
    • December 15, 2016 at 1:47 pm
      Permalink

      I love your Christmas door display – thanks for sharing.

      I’ve added a button to let you press both A and B at the same time to the simulator – thanks for the suggestion.

      I don’t know about the bug / feature for playing notes – I’m in the process of updating the simulator to attempt to keep up with the new features of the micropython code (e.g. speech) so I’ll look into it.

      Merry Christmas 🙂

      Reply
  • December 15, 2016 at 4:40 pm
    Permalink

    Speech would be great. I did try the speech library but it said not found. I then tried copying it across to the ‘official’ microbit editor – the code compiled but then the micro:bit said it was out of memory 🙁

    Reply

Leave a Reply