Labs / Bipes + MicroPython / 10 Comprehensive Case

10.8 Motion-Controlled Fun Dice Rolling

Author: Li Zisheng · Source: FreakStudio open documentation · License: CC BY 4.0

Contents

Young makers, are you still shaking the dice manually when playing dice games?

Today we are going to make an extremely cool motion-sensing fun dice roller 🎲! Simply give the development board a gentle shake, and it will simulate the effect of rolling dice: the digital tube will scroll through numbers 1 to 6, and the vibration motor will give you different feedback, just like you are actually holding a vibrating dice in your hand – it's super fun and amazing!

1. Case Introduction

1.1 Modules Used

1.2 Creative Gameplay

This motion-sensing dice frees you from the limitations of manual dice rolling, and the gameplay is incredibly fun:

1.3 Core Learning Points (Essential for Beginners to Master)

  1. Learn to read the data of a 6-axis gyroscope (obtain changes in acceleration/angular acceleration to determine shaking motions);
  2. Master the shake detection threshold logic (which uses the absolute values of three-axis data to determine whether a shake is triggered);
  3. Understand random number generation and dynamic display (simulate the dice rolling effect, and finally lock in a number between 1 and 6);
  4. Implement multi-peripheral linkage feedback (synchronous interaction of digital tube display + vibration motor);
  5. Proficient in using loop counting and delay control (to achieve vibration rhythms with different beat counts).

2. Wiring Instructions

This case requires connecting 3 modules, which can be directly plugged into the standard interfaces of the development board as required:

2.1 Wiring of MPU6050 6-axis Gyroscope

We connect the MPU6050 six-axis gyroscope module to the I2C0 interface of the development board.

2.2 Wiring of 4-digit Nixie Tube Module

We connect the 4-digit digital tube module to the DIO0 interface of the development board.

2.3 Wiring of Vibration Motor Module

We connect the vibration motor module to the DIO1 interface on the development board.

📌 Wiring Tips

  • The GND of all modules must be connected to the common ground of the development board; otherwise, the MPU6050 will output erratic data, the digital tube will not display, and the motor will not vibrate.
  • The MPU6050 requires a warm-up period of 1 to 2 minutes; otherwise, the initial attitude data will be unstable, leading to inaccurate shake detection.

3. Program Description

The core of this case lies in the workflow of "shake detection → dynamic rolling → point locking → vibration feedback". We break it down in the order of building block assembly, so even beginners can follow along step by step:

10.8 Motion-Controlled Fun Dice Rolling
  1. Hardware Initialization Block (Pre-step, Critical!)
    • MPU6050 Initialization: Configure the I2C0 interface (SDA = Pin6, SCL = Pin7) to activate the 6-axis attitude detection function;
    • Digital tube initialization: use the Init TM1637 4 Digit Display block, set CLK= Pin10/GPIO7, DIO= Pin11/GPIO8;
    • Vibration motor initialization: Set the DIO1 interface (Pin12/GPIO9) to output mode to prepare for controlling the motor vibration.
  2. Infinite Loop Block (repeat while true)
    • Function: Continuously detect shaking movements to realize the circular interaction of "shake → stop → feedback";
    • Location: All function blocks must be placed inside this loop.
  3. Attitude Data Reading Block
    • Read gyroscope data / Gyro-X Axis / Gyro-Y Axis / Gyro-Z Axis blocks to obtain the triaxial angular acceleration values and store them in the x / y / z variables;
    • Logic description: When shaking, the triaxial angular acceleration will suddenly increase, and the dice rolling will be triggered once it exceeds the set threshold.
  4. Shake detection block (if multi-condition judgment)
    • Configuration: Judgment absolute (x) > 30 OR absolute (y) > 30 OR absolute (z) > 30 (if the angular acceleration in any of the three axes exceeds 30, it is determined as shaking);
    • Function: After a shaking motion is detected, the "dice rolling" process will be triggered.
  5. Dice rolling and display block (within the if branch)
    • Trigger prompt: print "Trigger", which displays a shake trigger signal in the terminal;
    • Generate a random dice roll value: use random integer from 1 to 6 to generate the final dice roll result and store it in the led variable;
    • Dynamic scrolling display: Use the count with i from 1 to 10 loop to generate a random number between 1 and 9999 each time and display it on the nixie tube, simulating the dynamic effect of dice rolling;
    • Lock display points: After scrolling ends, clear the digital tube and display the final points led.
  6. Vibration Feedback Building Blocks (Vibration Corresponding to the Number of Points)
    • 1 point of feedback: if led= 1, use the count with i from 1 to 3 loop to control the motor to vibrate briefly 3 times (each vibration lasts 200ms with a 200ms interval between vibrations);
    • 2-5 point feedback: if the led is 2-5, control the motor to vibrate briefly once;
    • 6-point feedback: If led= 6, control the motor to vibrate continuously once (with a longer vibration duration);
    • Logic description: By adjusting the number of cycles and the delay time, differentiated vibration prompts are realized to make the point feedback more intuitive.
  7. Debounce Delay Block
    • Add a delay of 200 milliseconds to prevent false triggers caused by minor jitter and make shake detection more accurate.

Program running effect:

Motion-sensing fun dice roll.mp4 59.7 MB

10.8 Motion-Controlled Fun Dice Rolling
10.8 Motion-Controlled Fun Dice Rolling

A Beginner's Guide to Avoiding Common Pitfalls

  1. Is the shake detection insensitive / triggered falsely?
    • Adjust the shake threshold: to increase the sensitivity, change the value "30" in the judgment condition to a smaller number (e. g. 20); to decrease the sensitivity, change it to a larger number (e. g. 40);
    • Ensure that the MPU6050 is firmly fixed to avoid data jitter caused by looseness.
  2. Garbled display or non-scrolling on the digital tube?
    • Check whether the CLK/DIO pins are connected correctly (Pin10/Pin11), and whether the pin configuration of the initialization block is consistent with the hardware;
    • Make sure the power supply of the digital tube is 3.3V; connecting it to 5V may damage the module.
  3. Vibration motor not vibrating / weak vibration?
    • Check whether the motor is connected to the DIO1 interface (Pin12) and whether the correct high-level trigger is set in the program;
    • Confirm that the motor is powered by 5V; 3.3V power supply will result in insufficient vibration intensity.

✨ Interactive Mini Quiz

  1. Think about it: If you want to make the shake detection more sensitive, which block in the program needs to be modified? (Answer: Modify the threshold "30" in the shake detection condition, for example, change it to "20", so that a smaller acceleration can trigger the shake)
  2. Try it out: Add a "sound effect feedback" to the project: when shaken, the buzzer makes a "clattering" sound, and after shaking stops, it plays different tones based on the rolled points. Can you add the buzzer block to the code?

Directly importable building blocks:

Motion-sensing fun dice roll.xml 24 KB