How a Toy Slot Machine Picks Symbols on an ESP32

How a Toy Slot Machine Picks Symbols on an ESP32

This ESP32 slot machine does not take money. It is a desk toy that teaches what “random” means on a computer. Three reels spin, the symbols stop, and the program checks whether they match. You can watch it, change the art, and read the few lines that decide a win.

The board is the Waveshare ESP32-C6-LCD-1.47 (Amazon B0DK5J6LX3). This sketch stays in landscape320×172, the whole time. There is no touch and no motion sensorBOOT on GPIO9 is the lever. The RGB LED is GPIO8. Backlight stays at 50%. The case and flash file are on the lucky slab project.

Random is a number generator

random() does not peek into the future. It walks a formula that looks scrambled. Give it a range and you get a whole number inside that range. Each reel asks for a symbol index. There are 7 symbols: 7, BAR, STAR, GEM, CHERRY, BELL, LEMON.

The toy starts with 24 credits. A spin costs 1. Two matching reels pay 3. Three of a kind pay 24. Three 7s pay 77. Those are numbers in the sketch (START_CREDITSSPIN_COSTPAIR_PAYTRIPLE_PAYSEVEN_PAY), not coins. When the credits hit zero, the phase is PH_BROKE until you reset. Serial can also type credits or jackpot if you want to poke the toy while you learn it.

short BOOT press pulls the lever. A hold of about 0.7 seconds cycles the cabinet: VegasDiner, then Cyber. The reels stay the same. Only the colors and the title change. Serial 115200 accepts spinthemecreditsjackpothelp, and status.

A win is a comparison

After the reels stop, the program looks at three variables. If the first equals the second, that is a pair. If all three are equal, that is three of a kind. If all three are the 7 symbol, that is the jackpot line. Nothing else is special. The flashing words (WINNERSO CLOSE, and the rest) are just strings chosen after the comparison.

The reels are drawn into a small off-screen picture, then copied onto the panel in one blit. Drawing circles straight onto this ST7789 tears. Composing first is the same trick the GIF player uses.

Flash it

Chrome or Edge, data USB-C, Flash to board on the project page. No port: hold BOOT, tap RESET, release BOOT. Arduino IDE: ESP32C6 Dev Module, USB CDC On Boot.

Print the landscape shell in PLA or PETG, 0.2 mm, with the USB-C and button cutouts. The cavity matches this slab only. It will not fit the round Desk Pal puck.

Change a payout, flash again, and spin. You will see that “luck” on this board is three numbers and an if.