STM32 Traffic Control
Loading...
Searching...
No Matches
controller.c File Reference

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

Detailed Description

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.

Function Documentation

◆ changeLight()

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.

Parameters
lightAIndex of the first traffic light in the pair
lightBIndex of the second traffic light in the pair

◆ EXTI15_10_IRQHandler()

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.

◆ SysTick_CheckFirstPressTimeout()

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.

◆ SysTick_handleGreenPhaseTimeout()

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.

◆ SysTick_handleYellowTransitionTimeout()

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 YELLOW light pair is turned RED

The next waiting light pair is released to GREEN

The variable 'WaitingLightPair' indicates which light pair is waiting:

  • 0: Light pair 1-3
  • 1: Light pair 2-4

This funciton is called periodically by the SysTick_Handler.

Variable Documentation

◆ activeLightPair

uint32_t activeLightPair = -1

◆ allocatedTime

uint32_t allocatedTime = 0

◆ BUTTON

const uint32_t BUTTON[BUTTONS] = {BUTTON1, BUTTON2, BUTTON3, BUTTON4}

◆ firstPair

uint32_t firstPair = -1

◆ firstPress

bool firstPress = false

◆ firstPressTime

uint32_t firstPressTime = 0

◆ secondPair

uint32_t secondPair = -1

◆ timerActive

bool timerActive = false

◆ timerStartTime

uint32_t timerStartTime = 0

◆ waitForTimer

bool waitForTimer = false

◆ waitingForProcess

bool waitingForProcess = false

◆ waitingLightPair

uint32_t waitingLightPair = -1

◆ yellowStartTime

uint32_t yellowStartTime = 0