
我想制作一个有趣的东西,让人们真正可以动手玩耍,而不是又一个摆在我桌上的电路板。起初,反应时间游戏看起来很简单,但越想越多,我反而对电子元件、控制方式和外壳设计有了更多想法。 于是,我决定将这个想法变成一款小巧的桌面游戏,包含两个按钮、RGB LED灯、 OLED显示屏 和一个 蜂鸣器 。游戏目标很简单:等待信号,尽可能快速地做出反应,击败对手! 在本项目中,我将带你一步步了解如何将这个创意变为现实,从电子与机械设计,到固件编写和最终组装。 •我的OLED使用SH1106控制器。所连接的Daraz显示屏指定为SSD1306,因此应将其视为一种替代方案,而非完全替换。SSD1306显示屏需要对固件进行一些修改。 •请确保您的RGB LED为共阴极类型。共阳极LED则需对固件和电路进行调整。 •在原型中,共享的RGB颜色通道使用了220Ω电阻。对于每个LED颜色通道,单独使用限流电阻是更可靠的方法。 •在连接蜂鸣器前,请先检查其电压和电流要求。可能需要使用晶体管或MOSFET驱动器。 我决定不在一块板上全部集成,而是将三个小型电路分别制作在Vero板上:两个用于RGB LED,一个用于三个菜单按钮。这样能保持结构清晰,并便于后续将所有组件安装到外壳内。
我制作了两块完全相同的电路板,每块对应玩家的三颗RGB LED。每块电路板尺寸约为16.4 × 49.5毫米。 红色通道相互连接,绿色通道相互连接,蓝色通道相互连接。每个共享颜色通道都配有一个220Ω的限流电阻。 每个LED的公共引脚通过跳线单独引出,这样每块电路板就拥有三个独立的使能连接,使得ESP32可以分别控制每个LED的公共引脚。 这个原型的一个局限是,每个共享颜色通道只使用一个电阻,而不是为每个LED单独使用一个电阻。这可能导致并联LED之间的电流分配不均,从而造成亮度差异。为了更可靠的设计,建议为每个LED的红、绿、蓝通道分别使用独立的限流电阻。 第二个电路负责处理三个小型菜单按钮:模式、选择和开始。电路板尺寸约为34 × 26毫米。 每个按钮都有独立的信号连接,而开关的另一端则连接到共用的接地线上。当按下某个按钮时,其信号会被拉至地线,ESP32可通过上拉电阻检测该输入信号。 我使用焊接的导线连接件在电路板背面建立了连接。三根信号线和共地线分别引出,以便在布线阶段将电路板连接到ESP32。 这就是小型电路模块的全部内容!接下来,我们进入用于固定所有部件的外壳部分。
我在Fusion 360中设计了外壳,保持布局简洁,并确保所有电子元件都有足够的空间。 整体外壳尺寸为 233.75 × 83 × 56.5 毫米(长 × 宽 × 高)。 主外壳分为三个部分:两端各有一个小区域,用于安装 22 毫米的玩家按钮;中央较大的区域则用于容纳 ESP32 和面包板。 外壳上有两个开孔:一个用于连接 ESP32 的 Micro-USB 接口以供电,另一个用于让蜂鸣器发出的声音传出。 盖子上设有开孔,分别用于安装两个玩家按钮、三个菜单按钮以及 RGB LED。OLED 显示屏位于盖子顶部,其下方有一个开孔,以便引出其引脚并实现电气连接。 我用朋友的Anycubic Kobra 3打印了两个部件。使用PLA线材和打印机默认设置,总打印时间约为四小时。 打印效果对原型来说已经足够良好,而PLA线材对于这类项目来说是个合适的选择。由于外壳相对较小,且不需要承受重载,因此我的打印设置完全满足了需求。 在机箱印刷完成、小型电路板准备就绪后,是时候将所有部件组装起来了。我首先将元件安装到盖板上,然后将所有内容连接到ESP32。 我先将两个RGB LED板、三键电路板和OLED显示屏放置在盖板上。使用热熔胶枪将这些板固定到位,确保LED和按钮与各自的开口对齐。
OLED显示屏位于盖板顶部,其引脚通过一个小开孔穿过,以便在下方进行电气连接。 接下来,我将ESP32开发板放在两个小型面包板上。这样可以方便地连接GPIO引脚,并实现电源和地线的分配,而无需直接将所有元件焊接到ESP32上。 我按照下图所示的电路原理图和引脚分配进行连接。我使用的是一个30针的ESP32开发板。 每个玩家的三个RGB LED共享红色、绿色和蓝色的控制通道。每个LED的公共引脚都有独立的使能连接,使ESP32能够单独控制六个LED。 前四个输入端使用外部10 kΩ上拉电阻,连接至3.3 V电源。当按钮被按下时,会将对应的信号线拉至地。开始/复位按钮则使用ESP32内部的上拉电阻。 显示屏采用SH1106控制器,并通过I²C与ESP32通信。 在需要时,我通过合适的驱动器连接蜂鸣器,而不是假设ESP32的GPIO可以直接提供其完整的工作电流。 在为设备供电前,请对照原理图检查连接情况,确认电源和地线是否正确,并确保没有焊锡桥接或松动的线路。同时请核实LED共阳极引脚的接线与LED类型及电路设计相匹配。 重要提示:经典ESP32上的GPIO 34、35、36和39为仅用于输入的引脚,无内置上拉电阻,因此需要使用外部10 kΩ的上拉电阻。此外,GPIO 5也是一个屏蔽引脚,在启动时可能会因驱动电路不同而产生短暂的蜂鸣声。
硬件准备就绪后,现在是时候让游戏开始运行了!我已附上完整的固件文件,因此您无需再从文章中复制粘贴全部代码。 - Adafruit SH110X — 用于与SH1106 OLED显示屏通信。 - Adafruit GFX Library — 适用于Adafruit SH110X的图形库。 - ESP32 Arduino Core 3.x — 提供ESP32板支持,并包含本固件中使用的LED PWM功能。 您可以通过Arduino IDE的库管理器安装Adafruit库。要安装ESP32板支持,请打开“工具”→“管理板”,搜索Espressif Systems中的ESP32,并安装兼容的3.x版本。 请确保固件中包含的types.h文件与所有其他提供的源文件一起存放在同一个sketch文件夹中。固件使用ESP32 Arduino Core 3.x的LED PWM API,因此较旧的2.x版本可能需要修改代码。 安装完成后,打开提供的固件文件,通过USB连接ESP32,并上传代码。 我的显示屏使用的是SH1106控制器。如果您使用的是SSD1306显示屏,请安装Adafruit SSD1306库,并对固件进行如下修改。
最后,将固件中所有 SH110X_WHITE 替换为 SSD1306_WHITE,所有 SH110X_BLACK 替换为 SSD1306_BLACK。 当前固件是针对共阴RGB LED进行配置的,其使能引脚为低电平激活。而共阳LED需要反向的RGB PWM值以及相反的使能引脚极性。 •在 setRGB() 函数中,将每个 ledcWrite() 的颜色值分别替换为 255 - c.r、255 - c.g 和 255 - c.b。 •在 setPlayerLEDs() 中,将激活状态从 LOW 改为 HIGH,非激活状态从 HIGH 改为 LOW。 •在 clearAllLEDs() 中,将使能引脚的状态从 HIGH 改为 LOW。 •在 setup() 中,将 LED 使能引脚初始化为 LOW 而不是 HIGH。 这些修改假设固件使用的是 8 位 PWM 值,范围为 0 到 255。 •我首先将两个玩家按钮安装到外壳的相应位置,然后安装蜂鸣器。 •接着,我把面包板放在中央隔层内,把ESP32放进去,并调整线路,确保所有部件都能正确对齐。 •当所有组件都安装到位后,我小心翼翼地盖上外壳盖子,确保没有电线被夹住或卡在两部分之间。
我最初设计的盖子采用卡扣式结构,但没充分考虑打印机的公差,导致合拢时稍显松动。为了解决这个问题,我在边缘处涂了一点热熔胶,以固定盖子。 由于使用了热熔胶而非更牢固的粘合剂,这样之后仍可打开外壳进行维修或改装。 硬件组装并上传固件后,就可以开始游戏了!该设备提供四种游戏模式、可配置的倒计时功能,以及用于导航菜单和查看结果的OLED界面。 设备开机后,会进行短暂的LED颜色扫描和启动程序。按下“START”键进入主菜单。 选定模式后,按下“START”键进入准备画面,再次按下“START”键即可开始回合。 •倒计时:显示屏从设定的起始数值开始倒数,若开启声音,则伴有短促蜂鸣声。LED灯指示倒计时进度。 •等待:显示屏显示等待信息,同时设备生成随机延迟时间。在此期间,玩家必须避免按压按钮。 •发令信号:LED灯闪烁白色,蜂鸣器发出声响,提示双方尽快做出反应。 •结果:设备判定胜者并显示反应时间。获胜者的LED灯变为绿色,失败者的变为红色。 反应时间对决最初只是一个简单的构想:利用现成的电子元件和一个自制3D打印的外壳,开发一款互动式反应时间游戏。最终原型集成了ESP32、RGB LED灯、按钮、 蜂鸣器 和 OLED显示屏 ,实现了一款支持多人对战、多种模式及可配置设置的游戏。
项目开发过程中包括设计外壳、布线并测试各个组件、编写固件,以及将所有部分整合为一个功能完整的设备。尽管在组装外壳时需要进行一些调整,但最终原型成功实现了硬件与软件的完美结合。 未来版本仍有改进空间,例如可以使用定制PCB板替代面包板,优化外壳公差以实现更整洁的卡扣式装配,或增加更多游戏模式和性能统计功能。
本文来源:https://www.21ic.com/a/1017566.html
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。

I wanted to build something fun that people could actually play with by hand, not just another circuit board sitting on my desk. At first, a reaction time game seemed simple, but the more I thought about it, the more ideas I had about the electronic components, the control methods, and the enclosure design. So I decided to turn this idea into a compact desktop game featuring two buttons, RGB LEDs, an OLED display, and a buzzer. The goal of the game is simple: wait for the signal, react as quickly as possible, and beat your opponent! In this project, I will take you step by step through how to bring this idea to life, from the electronic and mechanical design, to writing the firmware and the final assembly. • My OLED uses an SH1106 controller. The connected display from Daraz is specified as an SSD1306, so it should be treated as an alternative rather than a complete replacement. An SSD1306 display requires some firmware modifications. • Please make sure your RGB LED is a common cathode type. Common anode LEDs require adjustments to the firmware and circuit. • In the prototype, 220Ω resistors were used for the shared RGB color channels. Using a separate current-limiting resistor for each LED color channel is a more reliable approach. • Before connecting the buzzer, check its voltage and current requirements. You may need to use a transistor or MOSFET driver. I decided not to integrate everything on a single board, but instead built three small circuits separately on Vero boards: two for the RGB LEDs and one for the three menu buttons. This keeps the structure clear and makes it easier to install all the components into the enclosure later.
I made two identical circuit boards, one for each player's three RGB LEDs. Each board measures about 16.4 × 49.5 mm. The red channels are connected together, the green channels together, and the blue channels together. Each shared color channel has a 220Ω current-limiting resistor. The common pin of each LED is routed out individually via jumper wires, so each board has three separate enable connections, allowing the ESP32 to control each LED's common pin individually. One limitation of this prototype is that only one resistor is used per shared color channel instead of one for each LED. This can lead to uneven current distribution among the parallel LEDs, causing brightness differences. For a more reliable design, it is recommended to use separate current-limiting resistors for the red, green, and blue channels of each LED. The second circuit handles three small menu buttons: Mode, Select, and Start. The board measures about 34 × 26 mm. Each button has its own signal connection, while the other side of the switch is connected to a shared ground line. When a button is pressed, its signal is pulled to ground, and the ESP32 detects this input through a pull-up resistor. I used soldered wire connections on the back of the board to make the connections. The three signal wires and the common ground wire are routed out separately so the board can be connected to the ESP32 during the wiring stage. That is all for the small circuit modules! Next, we move on to the enclosure that holds everything in place.
I designed the enclosure in Fusion 360, keeping the layout simple and making sure there was enough space for all the electronic components. The overall enclosure measures 233.75 × 83 × 56.5 mm (length × width × height). The main enclosure is divided into three sections: a small area at each end for mounting the 22 mm player buttons, and a larger central area for housing the ESP32 and the breadboard. There are two openings in the enclosure: one for connecting the ESP32's Micro-USB port for power, and another for letting the sound from the buzzer out. The lid has cutouts for the two player buttons, the three menu buttons, and the RGB LEDs. The OLED display sits at the top of the lid, with an opening beneath it for routing its pins out and making the electrical connections. I printed both parts on a friend's Anycubic Kobra 3. Using PLA filament and the printer's default settings, the total print time was about four hours. The print quality was more than good enough for a prototype, and PLA filament is a suitable choice for this kind of project. Since the enclosure is relatively small and does not need to bear heavy loads, my print settings were perfectly adequate. With the enclosure printed and the small circuit boards ready, it was time to assemble everything. I first mounted the components to the lid, then connected everything to the ESP32. I began by placing the two RGB LED boards, the three-button circuit board, and the OLED display on the lid. I used a hot glue gun to secure the boards in place, making sure the LEDs and buttons aligned with their respective openings.
The OLED display sits at the top of the lid, with its pins passing through a small opening for electrical connections underneath. Next, I placed the ESP32 development board on two small breadboards. This makes it easy to connect the GPIO pins and distribute power and ground without having to solder all the components directly to the ESP32. I connected everything according to the circuit schematic and pin assignments shown in the figure below. I used a 30-pin ESP32 development board. Each player's three RGB LEDs share the red, green, and blue control channels. Each LED's common pin has its own enable connection, allowing the ESP32 to control all six LEDs individually. The first four inputs use external 10 kΩ pull-up resistors connected to the 3.3 V supply. When a button is pressed, it pulls the corresponding signal line to ground. The Start/Reset button uses the ESP32's internal pull-up resistor. The display uses an SH1106 controller and communicates with the ESP32 via I²C. Where needed, I connected the buzzer through a suitable driver rather than assuming the ESP32's GPIO could directly supply its full operating current. Before powering the device, check the connections against the schematic, confirm that power and ground are correct, and make sure there are no solder bridges or loose wires. Also verify that the wiring of the LEDs' common anode pins matches the LED type and circuit design. Important note: On the classic ESP32, GPIO 34, 35, 36, and 39 are input-only pins with no internal pull-up resistors, so external 10 kΩ pull-up resistors are required. In addition, GPIO 5 is a strapping pin and may produce a brief beep at startup, depending on the driver circuit.
With the hardware ready, it is time to get the game running! I have attached the complete firmware files, so you do not need to copy and paste all the code from the article. - Adafruit SH110X — for communicating with the SH1106 OLED display. - Adafruit GFX Library — the graphics library used with Adafruit SH110X. - ESP32 Arduino Core 3.x — provides ESP32 board support and includes the LED PWM functionality used in this firmware. You can install the Adafruit libraries through the Arduino IDE's Library Manager. To install ESP32 board support, open Tools → Boards Manager, search for ESP32 by Espressif Systems, and install a compatible 3.x version. Please make sure the types.h file included in the firmware is stored in the same sketch folder as all the other provided source files. The firmware uses the LED PWM API from ESP32 Arduino Core 3.x, so older 2.x versions may require code modifications. Once everything is installed, open the provided firmware file, connect the ESP32 via USB, and upload the code. My display uses an SH1106 controller. If you are using an SSD1306 display, install the Adafruit SSD1306 library and make the following changes to the firmware.
Finally, replace all SH110X_WHITE with SSD1306_WHITE and all SH110X_BLACK with SSD1306_BLACK in the firmware. The current firmware is configured for common cathode RGB LEDs, whose enable pins are active low. Common anode LEDs require inverted RGB PWM values and the opposite enable pin polarity. • In the setRGB() function, replace the color value in each ledcWrite() with 255 - c.r, 255 - c.g, and 255 - c.b respectively. • In setPlayerLEDs(), change the active state from LOW to HIGH and the inactive state from HIGH to LOW. • In clearAllLEDs(), change the enable pin state from HIGH to LOW. • In setup(), initialize the LED enable pins to LOW instead of HIGH. These modifications assume the firmware uses 8-bit PWM values ranging from 0 to 255. • I first installed the two player buttons into their corresponding positions in the enclosure, then installed the buzzer. • Next, I placed the breadboard in the central compartment, put the ESP32 in, and adjusted the wiring to make sure all components aligned properly. • Once all the components were in place, I carefully closed the enclosure lid, making sure no wires were pinched or caught between the two parts.
I initially designed the lid with a snap-fit structure, but did not fully account for the printer's tolerances, so it was slightly loose when closed. To fix this, I applied a little hot glue along the edges to secure the lid. Because hot glue was used instead of a stronger adhesive, the enclosure can still be opened later for repairs or modifications. With the hardware assembled and the firmware uploaded, the game is ready to play! The device offers four game modes, a configurable countdown, and an OLED interface for navigating menus and viewing results. When the device powers on, it performs a brief LED color scan and startup sequence. Press START to enter the main menu. After selecting a mode, press START to go to the ready screen, then press START again to begin the round. • Countdown: The display counts down from the set starting value, accompanied by short beeps if sound is enabled. The LEDs indicate the countdown progress. • Wait: The display shows a waiting message while the device generates a random delay. During this time, players must avoid pressing their buttons. • Go signal: The LEDs flash white and the buzzer sounds, prompting both players to react as quickly as possible. • Result: The device determines the winner and displays the reaction times. The winner's LEDs turn green and the loser's turn red. Reaction Time Duel began as a simple idea: develop an interactive reaction time game using off-the-shelf electronic components and a custom 3D-printed enclosure. The final prototype integrates an ESP32, RGB LEDs, buttons, a buzzer, and an OLED display, resulting in a game that supports multiplayer battles, multiple modes, and configurable settings.
The development process included designing the enclosure, wiring and testing each component, writing the firmware, and integrating everything into a fully functional device. Although some adjustments were needed when assembling the enclosure, the final prototype successfully achieved a good combination of hardware and software. There is still room for improvement in future versions, such as using a custom PCB instead of a breadboard, optimizing the enclosure tolerances for a neater snap-fit assembly, or adding more game modes and performance statistics features.
本文来源:https://www.21ic.com/a/1017566.html
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。