MI-14micro:bitBeginner

micro:bit Magic Ball: Yes or No

Think of a question and shake the board: the ball thinks for a moment, then says yes, no or maybe. random.randrange picks the answer and each answer has its own tone (music.pitch). No extra parts are needed.

0.0(0)
micro:bit Magic Ball: Yes or No
Make a fortune ball that answers your questions. You learn to detect a shake, play an animation from built-in images, pick a random position and take the item at that position from two lists, and play tones of different pitch on the speaker.

Video

Circuit Diagram

micro:bit Magic Ball: Yes or No - Circuit Diagram

Source Code

MicroPython
1# ─────────────────────────────────────────
2#   BlueGrays · bluegrays.com
3#   micro:bit Magic Ball: Yes or No
4# ─────────────────────────────────────────
5# Think of a yes or no question and shake the board. The ball thinks for a
6# moment and then answers: a tick means yes, a cross means no and a
7# confused face means maybe. Each answer has its own tone. No extra parts
8# are needed.
9#
10# Pins:
11#   LED -> Onboard 5x5 LED display
12#   ACCEL -> Onboard accelerometer
13#   SPEAKER -> Onboard speaker
14
15from microbit import *
16import music
17import random
18
19# ============ SETTINGS ============
20VOLUME = 150         # Volume, 0-255
21THINK_MS = 100       # Time for each frame while thinking (ms)
22BEEP_MS = 300        # Length of the answer tone (ms)
23LOOP_MS = 50         # Loop pause (ms)
24
25# The answers and their tones are in the same order: yes is high, no is low and maybe is in between.
26ANSWERS = [Image.YES, Image.NO, Image.CONFUSED]
27TONES = [880, 220, 440]
28
29set_volume(VOLUME)
30display.show(8)
31
32while True:
33    if accelerometer.was_gesture("shake"):
34        display.show(Image.ALL_CLOCKS, delay=THINK_MS)
35        pick = random.randrange(3)
36        display.show(ANSWERS[pick])
37        music.pitch(TONES[pick], BEEP_MS)
38    sleep(LOOP_MS)
39

FAQ

Does the ball really know the future?

No, it is a game. random.randrange picks the answer at random; it does not hear the question or remember earlier answers. The three answers are equally likely: we ran the code 300 times on a computer and each answer came up about 100 times.

What do the signs on the display mean?

While it waits you see an 8, because the game is inspired by the number eight pool ball. A tick means yes, a cross means no and a confused face means maybe. Yes comes with a high tone, no with a low tone and maybe with a tone in between.

How do I put this code on the micro:bit?

The code is MicroPython. Open the micro:bit Python Editor (python.microbit.org) on a computer, delete the code that is there and paste this one. Connect the board with a USB cable and press Send to micro:bit. If that button does not work, use Save to download the .hex file and copy it to the drive called MICROBIT. You can rebuild the same project with blocks in MakeCode, but this code cannot be pasted there.

Does it work on the older micro:bit (V1) too?

The code is written for the micro:bit V2. Projects that only use the LED display, buttons A and B and the accelerometer also run on a V1. The speaker, the microphone and the touch logo exist only on the V2, so code that uses them gives an error on a V1. If your board has a speaker on the back and a gold logo on the front, it is a V2.