/******************************************************************************
*
* File Name: Problem_4
*
* Author: Ahmed Abdelgleel
*
* Description:
* This program simulates a traffic light system with pedestrian crossing support.
* The system cycles through green, yellow, and red lights for vehicles.
* Pedestrians can request to cross by pressing a button, which temporarily
* overrides the traffic light sequence.
*
*******************************************************************************/
#include <TaskScheduler.h>
// TaskScheduler instance
Scheduler ts;
// Pin Definitions
const int redPin = 5; // Red traffic light pin
const int yellowPin = 6; // Yellow traffic light pin
const int greenPin = 7; // Green traffic light pin
const int buttonPin = 2; // Pedestrian button pin
// Pedestrian Request State
volatile bool pedestrianRequest = false; // Tracks if a pedestrian button is pressed
// Task Functions
/**
* turnOnGreen:
* Turns on the green traffic light and turns off others.
*/
void turnOnGreen() {
digitalWrite(greenPin, HIGH); // Green light ON
digitalWrite(yellowPin, LOW); // Yellow light OFF
digitalWrite(redPin, LOW); // Red light OFF
}
/**
* turnOnYellow:
* Turns on the yellow traffic light and turns off others.
*/
void turnOnYellow() {
digitalWrite(greenPin, LOW); // Green light OFF
digitalWrite(yellowPin, HIGH); // Yellow light ON
digitalWrite(redPin, LOW); // Red light OFF
}
/**
* turnOnRed:
* Turns on the red traffic light and turns off others.
*/
void turnOnRed() {
digitalWrite(greenPin, LOW); // Green light OFF
digitalWrite(yellowPin, LOW); // Yellow light OFF
digitalWrite(redPin, HIGH); // Red light ON
}
/**
* handlePedestrianRequest:
* Handles pedestrian requests. When activated:
* - The traffic light turns red.
* - Pedestrians are given a 5-second crossing time.
*/
void handlePedestrianRequest() {
if (pedestrianRequest) {
turnOnRed(); // Set traffic light to red
delay(5000); // Allow 5 seconds for pedestrian crossing
pedestrianRequest = false; // Reset the request after handling
}
}
// Define Tasks
Task tGreen(5000, TASK_FOREVER, &turnOnGreen, &ts); // Green light stays for 5 seconds
Task tYellow(2000, TASK_FOREVER, &turnOnYellow, &ts); // Yellow light stays for 2 seconds
Task tRed(5000, TASK_FOREVER, &turnOnRed, &ts); // Red light stays for 5 seconds
Task tPedestrian(100, TASK_FOREVER, &handlePedestrianRequest, &ts); // Check pedestrian request every 100ms
void setup() {
// Pin Setup
pinMode(greenPin, OUTPUT); // Set green light pin as output
pinMode(yellowPin, OUTPUT); // Set yellow light pin as output
pinMode(redPin, OUTPUT); // Set red light pin as output
pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with internal pull-up
// Interrupt Setup
attachInterrupt(digitalPinToInterrupt(buttonPin), requestPedestrian, FALLING);
// Task Initialization
tGreen.enable(); // Enable green light task
tYellow.enableDelayed(5000); // Yellow light starts after green
tRed.enableDelayed(7000); // Red light starts after green + yellow
tPedestrian.enable(); // Enable pedestrian request handler
}
void loop() {
// TaskScheduler execution
ts.execute();
}
/**
* requestPedestrian:
* Interrupt Service Routine (ISR) triggered by button press.
* Sets the pedestrianRequest flag to true.
*/
void requestPedestrian() {
pedestrianRequest = true;
}