Main > Everything Else
Mini Slot Machine Project with M5StickC Plus2
(1/1)
OpenELAB:
full text:
https://openelab.io/blogs/learn/m5stack-projects-mini-slot-machine-project-with-m5stickc-plus2
Introduction
I recently came across a slot machine project using M5StickC Plus and it piqued my interest. However, I only had the M5StickC Plus2 at hand. This got me thinking - could I create the same slot machine effect with the Plus2 and maybe even add some unique variations and optimizations? This seemed like the perfect opportunity to dive into this project!
The project involved an extensive exploration of the M5StickC Plus2, focusing on hardware control and graphic display. The decision was made to proceed with the mini slot project in a step-by-step manner, starting with an experiment.
Project Features
The objective of the project is to develop a mini version of a slot machine. Players will be able to activate the reels with a button and stop each column individually. The 1.14-inch TFT display included with Plus2 will be used to show five columns of scrolling icons, each containing 10 unique symbols. This setup aims to provide a visually rich and dynamic experience for the players.
Hardware Advantages of M5StickC Plus2
The M5StickC Plus2 offers significant performance enhancements in graphic display and hardware controls compared to the Plus version. Its controller enables users to initiate and terminate the spinning of reels, replicating the traditional slot machine experience. Hence, I have chosen to integrate it as the central hardware for the project. Leveraging its robust button interaction, players can effortlessly manage the game's start and stop functions. Furthermore, the display contributes to the project by enhancing image refresh speed and color performance.
Installation and Operation
Precondition
Software dependency: Arduino IDE, VScode or text, etc.
Hardware requirements:
USB-C cable:https://openelab.io/products/usb-a-to-usb-type-c-cable-white
M5StickC Plus2:https://openelab.io/products/m5stickcplus2
Dependencies: M5StickC Plus2 library, Arduino library, etc.
Arduino IDE Installation Steps
Installation of Dependencies
1. After installing the Arduino IDE, open the Arduino settings, copy the M5 development board link [https://static-cdn.m5stack.com/resource/arduino/package_m5stack_index.json] to the arrow shown and click OK to save it.
image:https://openelab.io/blogs/learn/m5stack-projects-mini-slot-machine-project-with-m5stickc-plus2
2. Open Tools->Board->Boards Manager.
image:https://openelab.io/blogs/learn/m5stack-projects-mini-slot-machine-project-with-m5stickc-plus2
3. Search for M5Stack and choose to install it. It's already installed on this host, so I won't repeat the installation.
image:https://openelab.io/blogs/learn/m5stack-projects-mini-slot-machine-project-with-m5stickc-plus2
4. Select development version, Tools->Board->M5Stack Arduino->M5StickCPlus2.
image:https://openelab.io/blogs/learn/m5stack-projects-mini-slot-machine-project-with-m5stickc-plus
5. Next, install the M5StickCPlus2 library, select Tools->Manage Libraries, search for M5StickCPlus2, and then select Install, which will not be repeated if it is already installed.
image:https://openelab.io/blogs/learn/m5stack-projects-mini-slot-machine-project-with-m5stickc-plus2
Compile and Run
1. After completing the installation of the dependencies, open the good downloaded zip archive.
image:https://openelab.io/blogs/learn/m5stack-projects-mini-slot-machine-project-with-m5stickc-plus2
2. Connect the Plus2 to your computer using USB-C, select Tools->Port to choose your own port.
image:https://openelab.io/blogs/learn/m5stack-projects-mini-slot-machine-project-with-m5stickc-plus2
3. Click on compile and then click on upload when the compilation is complete.
image:https://openelab.io/blogs/learn/m5stack-projects-mini-slot-machine-project-with-m5stickc-plus2
Instructions for Use
Order and Number of Pictures
The slot machine has five columns, each of which can hold up to 10 icons, and you can adjust their order at will!At the moment, we have prepared six 48x48 pixel material icons, their RGB565 hexadecimal data is already in the code, corresponding to elements 0 to 5 in the slot_symbols array. If you want to change the order and number of icons in each column, you can easily change the number in the symbolIndices array to change the display of the icons in each column!
image:https://openelab.io/blogs/learn/m5stack-projects-mini-slot-machine-project-with-m5stickc-plus2
Column-to-Column and Figure-to-Figure Spacing
By changing PAD_X and PAD_Y, you can change the spacing between columns and graphs, usually the default is 2 and 0.
image:https://openelab.io/blogs/learn/m5stack-projects-mini-slot-machine-project-with-m5stickc-plus2
Turntable Rotation Speed, Stop Reduction Speed
#define Speed_MAX 800 //Maximum speed of slot machine rotation
#define Speed_MIN 50 //Slot machine rotation minimum speed
#define Acceleration_MAX 12 //Acceleration when the slot machine is accelerating
#define Acceleration_MIN -20 //The acceleration when the slot machine is slowing down.
image:https://openelab.io/blogs/learn/m5stack-projects-mini-slot-machine-project-with-m5stickc-plus2
Next Issue Preview
In the next issue, we will explain in detail how to change the picture of the slot machine, we will get the hexadecimal parameters of the picture by taking the model of the picture and adjusting it to the format we want, and then present the picture we want on the slot machine Stay tuned!!!
More Details
Project Specific Code: https://github.com/OpenELAB/OpenELAB-M5StickCPlus2-Slot
M5StickC_Plus_Slot_Machine: https://github.com/Sarah-C/M5StickC_Plus_Slot_Machine
M5Stack Projects: Mini Slot Machine Project with M5StickC Plus2 -2
OpenELAB:
Welcome back to the Mini Slot Machine Project with M5StickC Plus2. In the previous article, we covered some basic operations. In this article, we're going to have some fun - change the picture of the slot machine! The whole process is still very simple and doesn't require advanced programming knowledge. You can add as many pictures as you like to your slot machine as you like. How exciting! Let's dive in and get hands-on together!
full text:
https://openelab.io/blogs/learn/m5stack-projects-mini-slot-machine-project-with-m5stickc-plus2
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、First, let's pick out 1-10 images with a clean, white or transparent background, all perfectly sized at 150x150 pixels.
2、To convert the image into a hexadecimal array in RGB565 format (where each 16-bit unit is 0X0000), we have two options for you! The first method uses Windows and the handy tool Lcdimg2. Here's the process:
Start by downloading Lcdimg2 (you can find plenty of tutorials online if needed).Set the image resolution to 48x48 (this gives you 4608 hex units in the 0X00 format).We've written a small program that takes those 4608 units and converts them into 2304 hex units in the 0X0000 format, giving you the RGB565 format we need!
3、The second option is even cooler—just use ChatGPT! Here's the plan: Upload your image, and ChatGPT will resize it to 150x150 pixels for you.Then, it will work its magic and output the image as a .C file in RGB565 format, with each pixel in 0X0000 hexadecimal form.Fast, easy, and you get your image converted into the format you need without lifting a finger!
4、Once we have the .C file generated using either method, we’ll have a neatly structured hexadecimal binary dataset—just like the example shown. All we need to do is copy down this data, and we’re all set for the next step!
5、We create a new .h file in the image folder, name it as the name of the image (e.g. Ghostface.h), write the following code in the file, and save it.
6、Then open our slot_symbols.h file, and enter the following code.
7、Next, open our M5StickCPlus2_slot.ino project file. We've already covered the initial setup, and now we’ll move on to changing the picture. The number represents the *slot_symbols pointer array’s hexadecimal data for each image. For example, if we use the Ghostface image and place it in the first position, then in the symbolIndices array, you would write the number 0. Why 0? Because the starting index is 0. So, if you have two images, you'd write 0 and 1. If three, then 0, 1, 2. Keep in mind, it’s not recommended to place the same numbers next to each other, and the values in the symbolIndices array must stay within a reasonable range.
8、If you need to add more than one picture, just follow the same steps as before. You can add up to 10 pictures in total.
Compile and Run
1、After completing the installation of the dependencies, open the good downloaded zip archive.
2、Connect the Plus2 to your computer using USB-C, select Tools->Port to choose your own port.
3、Click on compile and then click on upload when the compilation is complete.
Next Issue Preview
In the next update, we’ll make the final improvements to the slot machine by switching from manual to automatic stopping. With just one press, the five columns will stop in sequence, randomly, making the project more complete and much closer to the real-life slot machine experience!
More details
M5Stack Projects: [Mini Slot Machine Project with M5StickC Plus2](https://openelab.io/blogs/learn/m5stack-projects-mini-slot-machine-project-with-m5stickc-plus2)
Project Specific Code: [https://github.com/OpenELAB/OpenELAB-M5StickCPlus2-Slot-2](https://github.com/OpenELAB/OpenELAB-M5StickCPlus2-Slot-2)
M5StickC_Plus_Slot_Machine: [https://github.com/Sarah-C/M5StickC_Plus_Slot_Machine](https://github.com/Sarah-C/M5StickC_Plus_Slot_Machine)
OpenELAB:
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.
full text:
https://openelab.io/blogs/learn/m5stack-projects-mini-slot-machine-project-with-m5stickc-plus2
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:https://openelab.io/products/usb-a-to-usb-type-c-cable-white
M5StickCPlus2:https://openelab.io/products/m5stickcplus2
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.
--- Code: ---// Initialize M5StickC Plus2
#include "time.h"
#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++;
}
}
}
}
--- End code ---
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.
--- Code: ---// 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
}
}
--- End code ---
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.
Navigation
[0] Message Index
Go to full version