// we created those constants to understand the code easily
// Pin of the LEDs
const int led_green = 7;
const int led_yellow = 6;
const int led_red = 5;
// Pin of the seven segment display
const int pins[] = {1, 3, 4, 8, 9, 10, 11}; // equivalent of pins[] = {A, B, C, D, E, F, G};
const int num_pins = 7;
// Pin for the push button as interrupt
const int push_button = 2;
// delays we need for this project as follows (3 seconds, 1 second)
const int delay_3_sec = 3000;
const int delay_1_sec = 1000;
// create variable state (we have for states )
int state = 1; // inialise the varialbe to state 1 where Red is ON
int debounce = LOW; // used to debounce the push button
// values to control 7 segment display common cathode
const int num_to_display[10][7] = {
{1, 1, 1, 1, 1, 1, 0}, // 0
{0, 1, 1, 0, 0, 0, 0}, // 1
{1, 1, 0, 1, 1, 0, 1}, // 2
{1, 1, 1, 1, 0, 0, 1}, // 3
{0, 1, 1, 0, 0, 1, 1}, // 4
{1, 0, 1, 1, 0, 1, 1}, // 5
{1, 0, 1, 1, 1, 1, 1}, // 6
{1, 1, 1, 0, 0, 0, 0}, // 7
{1, 1, 1, 1, 1, 1, 1}, // 8
{1, 1, 1, 1, 0, 1, 1} // 9
};
void handleInt() { // ISR for the push boutton
if (digitalRead(push_button) == HIGH) {
// Wait for the button to stabilize (debounce)
while (digitalRead(push_button) == HIGH) {
// Do nothing, just wait until the button is released
}
debounce = HIGH;
}
}
void setup() {
pinMode(led_green, OUTPUT);// set the green led as output so we can turn it on and off:
pinMode(led_red, OUTPUT);// set the red led as output so we can turn it on and off:
pinMode(led_yellow, OUTPUT);// set the yellow led as output so we can turn it on and off:
pinMode(push_button, INPUT);// set the push button as input so we can read it
attachInterrupt(INT0, handleInt, CHANGE); // set the interrupt
for (int i = 0; i < num_pins; i++ ) {
pinMode(pins[i], OUTPUT); // set the seven_segment display as outputs for all the pins using a loop
}
}
// we created for functions to turn on and off the leds. A function for each state
// state_1 = red on , yellow off, green off
void state_1 () {
digitalWrite(led_red, HIGH);
digitalWrite(led_yellow, LOW);
digitalWrite(led_green, LOW);
}
// state_2 = red off , yellow off, green off
void state_2 () {
digitalWrite(led_red, LOW);
digitalWrite(led_yellow, LOW);
digitalWrite(led_green, LOW);
}
// state_3 = red off , yellow off, green on
void state_3 () {
digitalWrite(led_red, LOW);
digitalWrite(led_yellow, LOW);
digitalWrite(led_green, HIGH);
}
// state_4 = red off , yellow on, green off
void state_4() {
digitalWrite(led_red, LOW);
digitalWrite(led_yellow, HIGH);
digitalWrite(led_green, LOW);
}
// we created function to control the seven segement
void seven_segment_display (){
// we use the nested loop concept
for (int counter = 9 ; counter >= 0; counter--){// first loop counts the numbers be displayed
for (int i = 0; i < num_pins; i++ ) {// second loop controls the pins depending on the number to be dispayed
digitalWrite(pins[i], num_to_display[counter][i]);
}
delay (delay_1_sec); // delay of one second between the numbers
}
}
void loop() {
bool condition = true;// used in closed loop
for (int i = 0; i < num_pins; i++ ) {// set the seven to zero at the inialisation
digitalWrite(pins[i], num_to_display[0][i]);
}
state_1 (); // inialise the system to an initial state (for security) ( state_1 = red on , yellow off, green off )
while (condition == true){
// The design simulate the real trafic simulation lights
// so the state goes as follows:
// state_1 = red on , yellow off, green off (3 seconds)
// state_2 = red off , yellow off, green off (1 second)
// state_3 = red off , yellow off, green on (3 seconds)
// state_4 = red off , yellow on, green off (1 second)
//
if (state == 1) {// if it is in state_1 after 3 seconds it goes to state_2
delay (delay_3_sec);
state = 2;
state_2();
}
if (state == 2) { // if it is in state_2 after 1 second it goes to state_3
state = 3;
delay (delay_1_sec);
state_3();
}
if (state == 3) {// if it is in state_3 after 3 seconds it goes to state_4,
delay (delay_3_sec);
if (debounce == HIGH){ //if the push button is pushed and realeased it wil add 9 more seconds and call seven segment controll function
seven_segment_display();
debounce = LOW;
}
state = 4;
state_4();
}
if (state = 4) { // if it is in state_4 after 1 second it goes to state_4
delay(delay_1_sec);
state = 1;
state_1();
}
}
}