Skip to content

+49 1626571232

info@openelab.io

🚀 Free Shipping from 50€ in EU / 80€ Worldwide

M5StickC Plus2: Create a Mini Slot Machine Project-3

17 Oct 2024 0 Comments

Congratulations on reaching the final step of your slot machine project! In this step, we’ll guide you through implementing a super cool feature: changing from manual stop to automatic stop. But that’s not all! We’ll also show you how to use macro definitions to create a one-button mode switch. With this feature, you can effortlessly switch between auto and manual modes just by modifying a single value.

It’s like magic, adding more flexibility and fun to your project! So, are you ready to take your slot machine to the next level? Let’s dive in together and make it happen!

  

Installation and Operation

Precondition

Software dependency: Arduino IDE, VScode or text, etc.

Hardware requirements: USB-C cable, M5StickCPlus2, etc.

Dependencies: M5StickCPlus2 library, Arduino library, etc.

Installation of dependencies

1. This change involves two main parts. Let's first walk through the first part, building on the modifications introduced in the previous section. We’ll analyze each change step-by-step, starting from the top.

  • Automation Definition: We defined the automation switch, which allows you to toggle between automatic and non-automatic modes.

  • Interval Settings:

    • time_MAX and time_MIN: These variables represent the range for the stopping time interval.

    • initTime_MAX and initTime_MIN: These indicate the initial stop time interval during the early stages of the game.

  • Stop Count Limits:

    • startCount_MAX: This defines the maximum number of stops from the beginning of the game.

    • initTime_MAX and initTime_MIN: (Reiteration) These represent the initial interval settings for stop time selection.

    • startCount_MAX: This variable also controls how many stops can occur from the start.

With these variables, you have more control over timing and automation, allowing for smoother transitions between manual and automatic modes. In the next part, we’ll explore how to implement these intervals and switches in the code, bringing flexibility to your project.

2. Next, let’s look at the setup() function. We’ve added srand(time(0)) to initialize the random function with the current time, ensuring unique random values in each run.

Now, let’s examine the loop() function. First, we use Automation as a switch to determine whether the system operates in automatic or manual mode. If manual mode is selected, no changes are required. Our focus, however, is on the automatic mode.

In automatic mode, the primary task is to decide whether the spindles should stop. The spindles stop gradually based on a randomly generated stopping time (Count_Num), so there’s no need for the user to press the button multiple times.

When Button A (BtnA) is pressed, or the counter reaches the stopping condition, the spindles will stop one by one automatically.

  • State: SLOTS_INIT In this state, all spindles start spinning simultaneously, and a random count determines when they stop.

  • The spindles stop automatically under two conditions:

    • Button A is pressed.

    • The random counter reaches the designated stop time.

This logic ensures smooth operation in automatic mode, making the spindles stop without manual intervention, enhancing the slot machine’s gameplay experience.

// Initialize M5StickC Plus2
#define Automation 1 #define Time_MAX 80 #define Time_MIN 45 #define InitTime_MAX 100 #define InitTime_MIN 80 #define StartCount_MAX 6 void setup() { srand(time(0));//added on top of the original few } void loop() { static int Slot_Start = 0; // Control the number of spindle starts static int Slot_Stop = 0; // Whether to stop the spindle static int Count_Stop = 0; //Count the heartbeats of the stop spindle static int Count_Num; // Randomly generated stop times in automatic mode M5.update(); //Update button status if (Automation) { // If automatic mode is enabled if (M5.BtnA.wasPressed() || Slot_Stop == 1) { // button press or auto mode trigger stop if (state == SLOTS_INIT) { // If in the initialization state for (int i = 0; i < SLOT_COUNT; i++) { slots[i].start(); // Activate each spindle } state++; Slot_Start = 1; // Record startup status Count_Num = rand() % (InitTime_MAX - InitTime_MIN - 1) + InitTime_MIN;// Randomly generated stop times } else if (state < SLOTS_STOP) { // If the spindle is running Slot_Start++; slots[state - 1].stop(); //Stop the current axis state++; } } Slot_Stop = 0; // Reset stop sign } else { // manual mode if (M5.BtnA.wasPressed()) { //Press the button to start or stop the spindle if (state == SLOTS_INIT) { for (int i = 0; i < SLOT_COUNT; i++) { slots[i].start(); // Activate each spindle } state++; } else if (state < SLOTS_STOP) { slots[state - 1].stop(); // stop spindle state++; } } } }
✔ Copied!

 

3. Next, let’s look at the second part, which focuses on making each column stop randomly. This section is placed at the end of the loop function and cannot be moved. Our focus here is only on the automatic mode.

In this part, automatic mode is enabled when the macro Automation is defined as 1. In automatic mode, the user does not need to press a button to start or stop each spindle. Instead, the code automatically manages the start and stop process for each spindle.

When Auto Mode is enabled:

  • The system automatically starts the spindles and uses the counter Count_Stop to keep track of the elapsed time.
  • Each time the counter reaches a pre-generated random stop time (Count_Num), the system triggers the stopping of one spindle.
  • Once all spindles have been stopped, the system resets its state and prepares for the next round of operation.
This implementation ensures smooth, hands-free gameplay by automating the starting and stopping of the spindles, enhancing the slot machine's performance and user experience.
// Initialize M5StickC Plus2
 if (Automation) { // If in automatic mode
  if (Slot_Start && Slot_Start < StartCount_MAX) { //When at least one shaft has been started and the maximum number of starts has not yet been reached
    Count_Stop++; // Counter increments to record time progress
    if (Count_Stop == Count_Num && state < SLOTS_STOP) { //If the counter reaches a randomly generated stop time
      Slot_Stop = 1; // Trigger auto-stop spindle
      Count_Stop = 0; //Counter resets to zero in preparation for the next stop
      Count_Num = rand() % (Time_MAX - Time_MIN - 1) + Time_MIN; //Randomly generate the next stop interval
    }
  } else if (Slot_Start == StartCount_MAX) { // If all spindles have been activated
    Slot_Start = 0; // Zero the startup counter in preparation for the next startup
  }
}


        
✔ Copied!

 

Compile and Run

 1. After completing the installation of the dependencies, open the downloaded zip archive.

2. Connect the M5StickC Plus 2 to your computer using a USB-C cable, then go to Tools -> Port and select the appropriate port.

3. Click Compile, and once the compilation is complete, click Upload.

Prev Post
Next Post

Leave a comment

All blog comments are checked prior to publishing

Someone recently bought a

Thanks for subscribing!

This email has been registered!

Shop the look

Choose Options

Edit Option
Back In Stock Notification
this is just a warning
Login
Shopping Cart
0 items
RuffRuff App RuffRuff App by Tsun