Skip to content

A classic Cyclone LED chasing game implemented on an STM32F031K6 microcontroller using WS2812 addressable LEDs. Press the button to stop the light on the target!

Notifications You must be signed in to change notification settings

ekrem-bas/STM32-Cyclone-LED-Game

Repository files navigation

STM32 Cyclone LED Game (WS2812B)

This project is a Cyclone game implementation running on an STM32F031K6 microcontroller, using a WS2812B addressable LED strip. The player's goal is to stop the green light rotating on the LED strip precisely on the central red target LED by pressing a button at the right time.

🎯 Game Objective

The player attempts to stop the green light, which moves circularly on the LED strip, on the fixed red target LED (defined as CENTER_LED 29 in game.c).

  • Successful Stop: The player advances to the next level, and the game difficulty (speed) increases. Upon completing the hardest level (MISSION_IMPOSSIBLE), a special winning animation is played, and the game resets.
  • Failed Stop: The player loses a life. When all lives are depleted, the game resets. The player's remaining lives are indicated by 3 separate LEDs connected to the GPIOB port.

🛠️ Hardware and Configuration

  • Microcontroller: STM32F031K6
  • LED Strip: WS2812B Addressable RGB LED Strip.
    • Number of LEDs Used: 59 (Core/Inc/ws2812.h -> noOfLEDs).
    • Target LED Index: 29 (Core/Src/game.c -> CENTER_LED).
  • Button: 1 momentary push-button for player input.
    • Connection: PA0 (Core/Src/game.c -> BUTTON_Pin GPIO_PIN_0, BUTTON_GPIO_Port GPIOA).
    • In main.c, within the MX_GPIO_Init() function, the PA0 pin is configured for external interrupt (EXTI0_1_IRQn), Rising Edge triggered, and with a Pulldown resistor. Therefore, one terminal of the button should be connected to PA0 and the other to 3.3V.
  • WS2812 Data Pin: Driven via TIM3_CH1 PWM output (Core/Inc/ws2812.h -> extern TIM_HandleTypeDef htim3).
    • PWM and DMA settings are configured in MX_TIM3_Init() within main.c. For STM32F031K6, default pins for TIM3_CH1 can be PA6, PB4, or PC6. The GPIO setting for this pin is done in HAL_TIM_MspPostInit(&htim3) (usually within Core/Src/stm32f0xx_hal_msp.c).
  • Life Indicator LEDs: 3 standard LEDs (pins defined in Core/Src/game.c):
    • 1st Life: PB7 (LIFE_LED_3_PIN)
    • 2nd Life: PB1 (LIFE_LED_2_PIN)
    • 3rd Life: PB0 (LIFE_LED_1_PIN)
  • Brightness: LED brightness is set to BRIGHTNESS 100 (on a scale of 0-255) in Core/Src/game.c.

🧩 Core Libraries and Functions

Core/Inc/ws2812.h & Core/Src/ws2812.c

Responsible for driving the WS2812B LED strip.

  • noOfLEDs: Defines the number of LEDs (59).
  • pwmData[]: PWM buffer containing the LED data.
  • WS2812_SetLED(): Sets the color of a single LED.
  • WS2812_Send(): Sends the LED data to the strip.
  • WS2812_ResetAllLED(), WS2812_SetAllLED(): Functions to control all LEDs.

Core/Inc/game.h & Core/Src/game.c

Contains the game logic and flow.

  • Game_Setup(): Initializes the game settings.
  • Game_Loop(): Runs the main game loop.
  • Game_ButtonPressCallback(): Interrupt handler called when the button is pressed.
  • Defines various difficulty levels (from EASY to MISSION_IMPOSSIBLE).
  • Includes functions for in-game animations (play_cylon_animation, play_flash_animation, play_won_animation).

Core/Inc/main.h & Core/Src/main.c

Includes the main program flow, hardware initializations, and interrupt dispatches.

  • SystemClock_Config(): Configures the system clock to 48MHz.
  • MX_GPIO_Init(), MX_DMA_Init(), MX_TIM3_Init(): Initializes the necessary peripherals.
  • HAL_GPIO_EXTI_Callback(): Dispatches the button interrupt to Game_ButtonPressCallback().

🚀 Build and Run

  1. Get the Project: Download or clone the project files to your computer.
  2. Open in IDE: Open the project in STM32CubeIDE.
  3. Check LED Count: Ensure the noOfLEDs definition (currently 59) in Core/Inc/ws2812.h matches the LED strip you are using. CENTER_LED (29) must be less than this value.
  4. Hardware Connections:
    • Connect the DIN (Data Input) pin of the WS2812B LED strip to the STM32's TIM3_CH1 PWM output pin (e.g., PA6, PB4, or PC6 for STM32F031K6).
    • Connect the button between PA0 and 3.3V (as PA0 is configured with a pulldown resistor).
    • Connect the life LEDs (PB0, PB1, PB7) to their respective pins with appropriate current-limiting resistors and to GND.
    • Provide a suitable power supply for the LED strip and the STM32 (usually 5V for the strip, regulated 3.3V for the STM32). Ensure all GND lines are common.
  5. Build the Project: Compile the project from the STM32CubeIDE menu (e.g., "Project" -> "Build All").
  6. Flash the Microcontroller: Upload the resulting .hex or .bin file to your STM32F031K6 microcontroller using an ST-Link programmer and suitable software (e.g., STM32CubeProgrammer).
  7. Run: Power up the hardware and enjoy the game!

🖼️ GIFs

gif_1

gif_2

gif_3

About

A classic Cyclone LED chasing game implemented on an STM32F031K6 microcontroller using WS2812 addressable LEDs. Press the button to stop the light on the target!

Topics

Resources

Stars

Watchers

Forks

Languages