micro:bit – How to Write Code to Handle Movement

How to write code to recognize motion and react to it

Description

This project shows how to write code to recognize when a micro:bit is tilted toward the left, the right, the top, the bottom, and when it is shaken. It is a beginner project and requires no coding experience.

Video

Programming Language

MakeCode Blocks

Difficulty Level

Beginner

Materials

micro:bit and USB cable

Designing the Project

Develop the problem statement

The problem statement is how do I create a program to recognize some movements of the micro:bit while keeping it simple and engaging for beginners.

Write the Algorithm

This program recognizes events. There are five event types to respond to:

  • On tilt micro:bit left
    • draw left arrow
  • On tilt micro:bit right
    • draw rightarrow
  • On tilt micro:bit logo down
    • draw down arrow
  • On tilt micro:bit logo up
    • draw up arrow
  • On tilt micro:bit left
    • draw left arrow
  • On shake
    • Draw explosion

For a copy of the code, click here

Concepts Involved

Programming Concepts

Event Handling