Overview

Making difficult decisions can be tricky and stressful. This project will help you take off some of the pressure, instead letting your HalloWing-based Magic 9 Ball do the heavy lifting of your decision making!

By harnessing the HalloWing's accelerometer to detect when the ball is turned upside down and right side up (or shaken), we'll then run a highly mysterious algorithm (OK, it's just a random number generator, sorry) to choose an image file to show on the TFT display.

A stylish housing filled with swirly mystery fluid completes the effect. Will you love building this project?

INDICATIONS SAY YES

1 x Adafruit HalloWing M0 Express
Skull-shaped ATSAMD21 board w 1.44" 128x128 TFT display
1 x DIY Snow Globe Kit
108mm Diameter

Materials & Tools

In addition to the above parts, you will want the following:

  • Food dye
  • Water
  • Glycerin (optional)
  • Super Pearl Dust (used in baking to add shimmer, thanks to Epbot for the tip!)
  • E6000 glue (optional)
  • Double stick foam tape
  • Optional: 3D printed cap, mounting plate, and HalloWing case (if you don't have a 3D printer, you can send the files to be printed, or simply double-stick tape your HalloWing to the regular snow globe cap)

Build the Magic 9 Ball

Mix the Mystery Fluid

The Magic 9 Ball derives much of its power from the mysterious roiling fluid contained within the orb. Here, for the very first time, we shall reveal the secrets of the unfathomable fluid!

Get ready for the secrets to be revealed! -- We'll mix water with super pearl dust (also called shimmer dust), food coloring, and optional glycerin. The dye gives the mix its color, while the pearl dust provides the moving, glittering view into the complex fluid dynamics going on in there while it is thinking.

Add a very small amount of the pearl dust to the empty globe.

 

Fill halfway with water, then add dye and swirl it around. I used five drops of yellow to two drops of red here.

 

Fill the rest of the way with water, up to the level of the stopper cap.

 

Press the stopper cap into place. You can glue this in with silicone sealant or E6000 glue if you like, for extra safety.

This is a fun time to test it out -- screw on the original cap and swirl the orb to see the intricate fluid motion!

HalloWing Prep

The HalloWing will be the brains and display of our Magic 9 Ball.

Ready the Battery

 

Plug your Lipoly battery's JST plug into the battery port on the HalloWing.

 

Use double-stick foam tape to affix the battery to the board as shown.

Prepare the Cap and Mount

The cap, spacer and mount are all 3D printed. I modified these parts that were created by the Ruiz Bros. More info on the HalloWing M0 Express Wearable Case here, and the threaded ring came from their excellent Automatic Dice Roller project.

Below are the model files you'll need. These were printed in PLA plastic with 0.2mm layer height.

You'll also need two 10mm long M2.5 nylon screws and nuts to affix the HalloWing case bottom to the spacer.

Assemble

Snap the HalloWing into place in the bottom mount, then place the spacer into the cap as shown.

sensors_IMG_4969_2k.jpg

sensors_IMG_4970_2k.jpg

sensors_IMG_4975_2k.jpg

Screw the mount into place, thread on the nuts and tighten them. Then trim the screws flush with diagonal cutters. This prevents the screws from interfering with the fluid cap.

sensors_IMG_4977_2k.jpg

sensors_IMG_4979_2k.jpg

sensors_IMG_4980_2k.jpg

sensors_IMG_4983_2k.jpg

sensors_IMG_4988_2k.jpg

sensors_IMG_4990_2k.jpg

Screw Cap onto Globe

Now, we can screw the cap onto the globe -- just make sure the stopper is securely in place!

sensors_IMG_4992_2k.jpg

sensors_IMG_4994_2k.jpg

sensors_IMG_4995_2k.jpg

Finally, snap the HalloWing case cover into place.

Plug in a USB cable and next we'll program it!

Code with CircuitPython

Image Player Firmware

You can download the firmware file below that is a specific Alpha cut of CircuitPython that can display graphics on the TFT display easily.

To use it, plug in the HalloWing over USB to your computer and then double-click the reset button on the HalloWing. This will put it into bootloader mode, allowing you to change the firmware. You'll see a USB drive appear called HALLOWBOOT.

Drag the .uf2 file you downloaded onto the HALLOWBOOT drive. Once it copies over, it will automatically restart, ready for you to load on a CircuitPython program that can take advantage of the display capabilities.

File Format

To prepare your graphics for the HalloWing, you'll need to size them to 128 x 128 pixels. You can do this in nearly any graphics program, such as Photoshop, GIMP, Preview on osx, ImageMagik, MS Paint, you name it!

Save your file as a 24-bit .bmp file. Here you can see some example graphics.

sensors_lookslikeyes.bmp

sensors_nodoubtaboutit.bmp

sensors_verylikely.bmp

Here is the full set of answers for your Magic 9 Ball. Download the file and then unzip it.

Drag the .bmp files onto your CIRCUITPY drive as shown.

Tip: Images inside of a folder on your HalloWing won't be seen by this image player code below, only files at the root level of the HalloWing are seen. So, you can swap files in and out of folders to quickly change your image set!

Magic 9 Ball Code

This program is pretty simple in what it does -- it loads a random image onto the screen while fading the backlight up, then, whenever the HalloWing's accelerometer detects the board has been turned upside down or shaken, it dims the backlight, selects and displays a new image, and brings the backlight up again.

Adafruit really likes using the Mu editor to edit the CircuitPython code. See this guide on loading and using Mu.

# Magic 9 Ball
# Turn HalloWing face down and then face up to change images at random
# place 128 x 128 pixel 24-bit .bmp images at root level of HalloWing

import time
import os
import random
import board
import displayio
import pulseio
import busio
import adafruit_lis3dh

backlight = pulseio.PWMOut(board.TFT_BACKLIGHT)
splash = displayio.Group()
board.DISPLAY.show(splash)

max_brightness = 2 ** 15
SENSITIVITY = 5   # reading in Z direction to trigger, adjustable

images = list(filter(lambda x: x.endswith("bmp"), os.listdir("/")))

i = random.randint(0, (len(images)-1))  # initial image is randomly selected

# Set up accelerometer on I2C bus, 4G range:
I2C = busio.I2C(board.SCL, board.SDA)

ACCEL = adafruit_lis3dh.LIS3DH_I2C(I2C, address=0x18)

ACCEL.range = adafruit_lis3dh.RANGE_4_G

while True:
    shaken = False
    with open(images[i], "rb") as f:
        print("Image load {}".format(images[i]))
        try:
            odb = displayio.OnDiskBitmap(f)
        except ValueError:
            print("Image unsupported {}".format(images[i]))
            del images[i]
            continue
        face = displayio.Sprite(odb, pixel_shader=displayio.ColorConverter(),
                                position=(0, 0))
        splash.append(face)
        # Wait for the image to load.
        board.DISPLAY.wait_for_frame()

        # Fade up the backlight
        for b in range(100):
            backlight.duty_cycle = b * max_brightness // 100
            time.sleep(0.01)  # default (0.01)

        # Wait forever
        while not shaken:
            try:
                ACCEL_Z = ACCEL.acceleration[2]  # Read Z axis acceleration
            except IOError:
                pass
            # print(ACCEL_Z)  # uncomment to see the accelerometer z reading
            if ACCEL_Z > SENSITIVITY:
                shaken = True

        # Fade down the backlight
        for b in range(50, -1, -1):
            backlight.duty_cycle = b * max_brightness // 100
            time.sleep(0.005)  # default (0.005)

        splash.pop()

        i = random.randint(0, (len(images)-1))  # pick a new random image
        # print("shaken")
        faceup = False
        i %= len(images) - 1

Copy the code and then paste it into Mu. Save the file to your CIRCUITPY board as code.py.

The board will restart and then play an image. Try turning the Magic 9 Ball upside down and then back up -- you'll see a new image appear!

Answer Any Question

You can now unplug the USB cable (plug it back in any time to charge the battery) and use the Magic 9 Ball!

Ask it a yes or no question, such as "Have we reached the mother ship yet?", then turn the ball over (or give it a shake), then turn it right side up and the answer will present itself in all of its wisdom!

Microcontroller Selection Oracle

Here's another interesting use of the Magic 9 Ball code -- you can create a random microcontroller selector for all those times you just aren't sure which amazing CircuitPython-capable Adafruit board to get from Digi-Key, who are going all-in on CircuitPython!

This will display a randomly selected board and show the product name, image, and Digi-Key product number!

Simply load these images linked below onto the board (you can place the Magic 9 Ball images into a folder you make on the HalloWing and they won't be selected for viewing).

sensors_itsybitsy_m0_express.bmp

sensors_trinket_m0.bmp

sensors_metro_m0_express.bmp

sensors_gemma_m0.bmp

sensors_feather_huzzah.bmp

sensors_cpx.bmp

This is just an example we created for our friends at Digi-Key, but we hope it also serves as a leaping off point for you to come up with your own creative uses for the image randomization of the Magic 9 Ball!

This guide was first published on Sep 22, 2018. It was last updated on Sep 22, 2018.