Embedded C Program for Traffic Light Controller
GPIO pins to turn on red, yellow, and green lights in sequence with delays, for example: RED ON for 5s, GREEN ON for 5s, YELLOW ON for 2s in a loop.Examples
How to Think About It
Algorithm
Code
#include <stdio.h> #include <unistd.h> // for sleep function void red_light_on() { printf("Red light ON\n"); } void red_light_off() { printf("Red light OFF\n"); } void green_light_on() { printf("Green light ON\n"); } void green_light_off() { printf("Green light OFF\n"); } void yellow_light_on() { printf("Yellow light ON\n"); } void yellow_light_off() { printf("Yellow light OFF\n"); } int main() { while(1) { red_light_on(); sleep(5); // red for 5 seconds red_light_off(); green_light_on(); sleep(5); // green for 5 seconds green_light_off(); yellow_light_on(); sleep(2); // yellow for 2 seconds yellow_light_off(); } return 0; }
Dry Run
Let's trace one full cycle of the traffic light controller through the code
Turn on red light
Red light ON printed, program waits 5 seconds
Turn off red light
Red light OFF printed
Turn on green light
Green light ON printed, program waits 5 seconds
Turn off green light
Green light OFF printed
Turn on yellow light
Yellow light ON printed, program waits 2 seconds
Turn off yellow light
Yellow light OFF printed
| Step | Light State | Action | Delay (seconds) |
|---|---|---|---|
| 1 | Red ON | Print 'Red light ON' | 5 |
| 2 | Red OFF | Print 'Red light OFF' | 0 |
| 3 | Green ON | Print 'Green light ON' | 5 |
| 4 | Green OFF | Print 'Green light OFF' | 0 |
| 5 | Yellow ON | Print 'Yellow light ON' | 2 |
| 6 | Yellow OFF | Print 'Yellow light OFF' | 0 |
Why This Works
Step 1: Control lights with functions
Each light has ON and OFF functions that simulate turning the light on or off by printing messages.
Step 2: Use delays to keep lights on
The sleep() function pauses the program to keep each light on for the correct time.
Step 3: Repeat cycle forever
The while(1) loop repeats the sequence endlessly to simulate continuous traffic light operation.
Alternative Approaches
#include <stdio.h> // Pseudocode: Setup timer interrupt to change lights // ISR changes light state every fixed interval int main() { // Initialize timer and lights while(1) { // Main loop does nothing, ISR handles lights } return 0; }
#include <stdio.h>
#include <unistd.h>
enum State {RED, GREEN, YELLOW};
int main() {
enum State state = RED;
while(1) {
if(state == RED) { printf("Red ON\n"); sleep(5); state = GREEN; }
else if(state == GREEN) { printf("Green ON\n"); sleep(5); state = YELLOW; }
else { printf("Yellow ON\n"); sleep(2); state = RED; }
}
return 0;
}Complexity: O(1) time, O(1) space
Time Complexity
The program runs in an infinite loop with fixed delays; no input size affects runtime, so time complexity is constant O(1).
Space Complexity
Uses a fixed number of variables and no dynamic memory, so space complexity is constant O(1).
Which Approach is Fastest?
All approaches run in real time with delays; using interrupts can be more efficient for CPU usage but more complex.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Simple loop with delays | O(1) | O(1) | Easy to understand and implement |
| Timer interrupts | O(1) | O(1) | Precise timing and efficient CPU use |
| State machine | O(1) | O(1) | Clear logic and easy to extend |