Spirit Level
What you'll build¶
Tilt the micro:bit and a bright LED rolls to the lowest side, like a ball on a tray. When the board is flat, a target appears in the middle.
Code¶
How it works¶
accelerometer.get_x()andget_y()measure tilt in milli-g: about 0 when flat and ±1024 when that edge points straight down. Tilt right and x goes positive. Tilt the pin edge down and y goes positive.to_led()turns each reading into an LED position: every 300 milli-g of tilt moves the ball one LED from the middle, andmin/maxkeep it on the 5×5 grid.display.set_pixel(x, y, 9)lights one LED at full brightness. When the ball is centred, the customLEVELimage shows a dim ring around it.
Try this next¶
- Make it more sensitive by changing
300to150. - Turn it into a maze game: avoid "wall" pixels while you roll the ball to a goal.
- Use
accelerometer.was_gesture("face down")to switch the level off when the board is turned over.