|
STM32 Traffic Control
|
Traffic Light Controller. More...
#include <stdio.h>#include <stdint.h>#include <stdbool.h>#include "stm32f446xx.h"#include "uart.h"#include "queue.h"#include "lights.h"#include "systick.h"#include "controller.h"Functions | |
| void | SysTick_handleGreenPhaseTimeout (void) |
| Monitor and handle expiration of the active GREEN light phase. | |
| void | SysTick_handleYellowTransitionTimeout (void) |
| Handle expiration of the YELLOW light phase for a traffic light pair. | |
| void | changeLight (uint32_t lightA, uint32_t lightB) |
| Switch the active traffic light pair. | |
| void | SysTick_CheckFirstPressTimeout (void) |
| Check for first button press timeout and process queued requests. | |
| void | EXTI15_10_IRQHandler (void) |
| External Interrupt Handler for Button Presses. | |
Variables | |
| const uint32_t | BUTTON [BUTTONS] = {BUTTON1, BUTTON2, BUTTON3, BUTTON4} |
| bool | firstPress = false |
| uint32_t | firstPressTime = 0 |
| uint32_t | firstPair = -1 |
| uint32_t | secondPair = -1 |
| bool | timerActive = false |
| uint32_t | timerStartTime = 0 |
| uint32_t | allocatedTime = 0 |
| uint32_t | yellowStartTime = 0 |
| uint32_t | activeLightPair = -1 |
| bool | waitingForProcess = false |
| bool | waitForTimer = false |
| uint32_t | waitingLightPair = -1 |
Traffic Light Controller.
Implements the logic for controlling traffic lights at an intersection. Handles vehicle detection via button presses, manages green/yellow/red light timings, and processes multiple requests using a queue system.
| void changeLight | ( | uint32_t | lightA, |
| uint32_t | lightB ) |
Switch the active traffic light pair.
This function transitions the currently active traffic light pair from GREEN -> YELLOW -> RED, then releases the specified light pair to GREEN.
The GREEN light duration is dynamically calculated based on the highest car count detected between the two lights in the specified pair.
All timing is handled asychronously via the SysTick timer to avoid blocking delays.
| lightA | Index of the first traffic light in the pair |
| lightB | Index of the second traffic light in the pair |
| void EXTI15_10_IRQHandler | ( | void | ) |
External Interrupt Handler for Button Presses.
This interrupt handler is triggered when any of the buttons (BUTTON1 to BUTTON4) connected to EXTI lines 10 to 13 are pressed. It identifies which button was pressed, and increments the corresponding light's car count.
To prevent multiple counts from a single press, a debounce mechanism is implemented using a time threshold defined by DEBOUNCE_TIME.
If this is the first button press detected this round, it records the time and the button index to start a 3-second window for additional button presses.
Clears the interrupt pending flag after processing.
| void SysTick_CheckFirstPressTimeout | ( | void | ) |
Check for first button press timeout and process queued requests.
This function checks if the 3-second window after the first button press has elapsed. If so, it queues the requested light pairs based on the order of button presses and processes the first request in the queue.
Resets the first and second press tracking variables after processing.
This function is called periodically by the SysTick_Handler.
| void SysTick_handleGreenPhaseTimeout | ( | void | ) |
Monitor and handle expiration of the active GREEN light phase.
This function checks whether the allocated GREEN duration for the currently active traffic light pair has elapsed. When the timer expires, the active light pair is released and the next queued light pair (if any) is dequeued and processed.
This funciton is called periodically by the SysTick_Handler.
| void SysTick_handleYellowTransitionTimeout | ( | void | ) |
Handle expiration of the YELLOW light phase for a traffic light pair.
This function checks whether the 1-second YELLOW duration for the currently waiting traffic light pair has elapsed. Once the timer expires:
The next waiting light pair is released to GREEN
The variable 'WaitingLightPair' indicates which light pair is waiting:
This funciton is called periodically by the SysTick_Handler.
| uint32_t activeLightPair = -1 |
| uint32_t allocatedTime = 0 |
| uint32_t firstPair = -1 |
| bool firstPress = false |
| uint32_t firstPressTime = 0 |
| uint32_t secondPair = -1 |
| bool timerActive = false |
| uint32_t timerStartTime = 0 |
| bool waitForTimer = false |
| bool waitingForProcess = false |
| uint32_t waitingLightPair = -1 |
| uint32_t yellowStartTime = 0 |