/*
start pin number and end pin number
led pin goes from 5 to 10
*/
int START_PIN = 5;
int END_PIN = 10;
/*
declare variables for each pin on north side
*/
int NORTH_GREEN = 8;
int NORTH_AMBER = 9;
int NORTH_RED = 10;
//north button is set at 2
int NORTH_BUTTON = 2;
/*
declare variables for each pin on west side
*/
int WEST_GREEN = 5;
int WEST_AMBER = 6;
int WEST_RED = 7;
//west button is set at 3
int WEST_BUTTON = 3;
//declare duration variable for various timings
int RED_DURATION = 5000;
int GREEN_DURATION = 4000;
int AMBER_DURATION = 1000;
int CHECK_DURATION = 100;
/*
3 states:-
1) 0 -- no button is pressed
2) 1 -- north pedestrain button was pressed
3) 2 -- west pedestrain button was pressed
*/
int SYSTEM_STATE = 0;
//setup
void setup() {
//set pin mode for the led pins
for (int i = START_PIN; i < END_PIN + 1; i++) {
pinMode(i, OUTPUT);
}
//attach interrupt for north and west button
attachInterrupt(digitalPinToInterrupt(NORTH_BUTTON), north_button_pressed, RISING);
attachInterrupt(digitalPinToInterrupt(WEST_BUTTON), west_button_pressed, RISING);
Serial.begin(9600);
}
//loop
void loop() {
//run is the regular loop
run();
}
//when north button is pressed set the system state to 1
void north_button_pressed() {
SYSTEM_STATE = 1;
}
//when west button is pressed set the system state to 2
void west_button_pressed() {
SYSTEM_STATE = 2;
}
//check if there is any change in system state
void check_system_state() {
if (SYSTEM_STATE == 1) {
Serial.println("north button pressed..");
SYSTEM_STATE = 0;
/*
if the system state is 1; it means that the north button is pressed
when north button is pressed call run() function
run function first turns on green for north direction and then green
for west direction and so on
*/
run();
} else if (SYSTEM_STATE == 2) {
Serial.println("west button pressed..");
SYSTEM_STATE = 0;
/*
if the system state is 2; it means that the west button is pressed
when west button is pressed call first call west_green_to_red() function
because the west side must turn on green and then call run() which turns
green for north and then west and so on
*/
west_green_to_red();
run();
}
}
/*
run() function first turns north light green then west light green and
so on continues
*/
void run() {
north_green_to_red();
west_green_to_red();
}
/*
north_green_to_red() function first turns on north green for green duration
then north amber for amber duration
and then north red
*/
void north_green_to_red() {
north_green_west_red();
/*
for loop to check if there has been any change in the system every 100 millisecond
if theres is change in system it does proceed accordingly
otherwise it run without anything
turn on for green duration
*/
for (int i = 1; i * CHECK_DURATION <= GREEN_DURATION; i++) {
delay(CHECK_DURATION);
check_system_state();
}
north_amber_west_red();
/*
for loop to check if there has been any change in the system every 100 millisecond
if theres is change in system it does proceed accordingly
otherwise it run without anything
turn on for amber duration
*/
for (int i = 1; i * CHECK_DURATION <= AMBER_DURATION; i++) {
delay(CHECK_DURATION);
check_system_state();
}
north_red_west_green();
}
void west_green_to_red() {
west_green_north_red();
/*
for loop to check if there has been any change in the system every 100 millisecond
if theres is change in system it does proceed accordingly
otherwise it run without anything
turn on for green duration
*/
for (int i = 1; i * CHECK_DURATION <= GREEN_DURATION; i++) {
delay(CHECK_DURATION);
check_system_state();
}
west_amber_north_red();
/*
for loop to check if there has been any change in the system every 100 millisecond
if theres is change in system it does proceed accordingly
otherwise it run without anything
turn on for amber duration
*/
for (int i = 1; i * CHECK_DURATION <= AMBER_DURATION; i++) {
delay(CHECK_DURATION);
check_system_state();
}
west_red_north_green();
}
/*
state when north direction led is green and west direction led is red
*/
void north_green_west_red() {
digitalWrite(NORTH_GREEN, HIGH);
digitalWrite(NORTH_AMBER, LOW);
digitalWrite(NORTH_RED, LOW);
digitalWrite(WEST_RED, HIGH);
digitalWrite(WEST_AMBER, LOW);
digitalWrite(WEST_GREEN, LOW);
}
/*
state when north direction led is amber and west direction led is red
*/
void north_amber_west_red() {
digitalWrite(NORTH_GREEN, LOW);
digitalWrite(NORTH_AMBER, HIGH);
digitalWrite(NORTH_RED, LOW);
digitalWrite(WEST_RED, HIGH);
digitalWrite(WEST_AMBER, LOW);
digitalWrite(WEST_GREEN, LOW);
}
/*
state when north direction led is red and west direction led is green
*/
void north_red_west_green() {
digitalWrite(NORTH_GREEN, LOW);
digitalWrite(NORTH_AMBER, LOW);
digitalWrite(NORTH_RED, HIGH);
digitalWrite(WEST_RED, LOW);
digitalWrite(WEST_AMBER, LOW);
digitalWrite(WEST_GREEN, HIGH);
}
/*
state when north direction led is red and west direction led is green
*/
void west_green_north_red() {
digitalWrite(WEST_RED, LOW);
digitalWrite(WEST_AMBER, LOW);
digitalWrite(WEST_GREEN, HIGH);
digitalWrite(NORTH_GREEN, LOW);
digitalWrite(NORTH_AMBER, LOW);
digitalWrite(NORTH_RED, HIGH);
}
/*
state when north direction led is red and west direction led is amber
*/
void west_amber_north_red() {
digitalWrite(WEST_RED, LOW);
digitalWrite(WEST_AMBER, HIGH);
digitalWrite(WEST_GREEN, LOW);
digitalWrite(NORTH_GREEN, LOW);
digitalWrite(NORTH_AMBER, LOW);
digitalWrite(NORTH_RED, HIGH);
}
/*
state when north direction led is green and west direction led is red
*/
void west_red_north_green() {
digitalWrite(WEST_RED, HIGH);
digitalWrite(WEST_AMBER, LOW);
digitalWrite(WEST_GREEN, LOW);
digitalWrite(NORTH_GREEN, HIGH);
digitalWrite(NORTH_AMBER, LOW);
digitalWrite(NORTH_RED, LOW);
}