int red = 13; //red light int yellow = 12; //yellow light int green = 11; //green light //7-seg display int a = 8; int b = 7; int c = 6; int d = 5; int e = 4; int f = 9; int g = 10; int button = 3; //push button int buttonState = 0; int PIR = 2; //PIR sensor int counter = 0; //delay timer simulator void setup() { Serial.begin(9600); pinMode(red, OUTPUT); pinMode(yellow, OUTPUT); pinMode(green, OUTPUT); pinMode(a, OUTPUT); pinMode(b, OUTPUT); pinMode(c, OUTPUT); pinMode(d, OUTPUT); pinMode(e, OUTPUT); pinMode(f, OUTPUT); pinMode(g, OUTPUT); pinMode(button, INPUT); pinMode(PIR, INPUT); } void zero() //display digit 0 { digitalWrite(b, LOW); digitalWrite(c, LOW); digitalWrite(a, LOW); digitalWrite(g, HIGH); digitalWrite(d, LOW); digitalWrite(e, LOW); digitalWrite(f, LOW); } void one() //display digit 1 { digitalWrite(b, LOW); digitalWrite(c, LOW); digitalWrite(a, HIGH); digitalWrite(g, HIGH); digitalWrite(d, HIGH); digitalWrite(e, HIGH); digitalWrite(f, HIGH); } void two() //display digit 2 { digitalWrite(b, LOW); digitalWrite(c, HIGH); digitalWrite(a, LOW); digitalWrite(g, LOW); digitalWrite(d, LOW); digitalWrite(e, LOW); digitalWrite(f, HIGH); } void three() //display digit 3 { digitalWrite(b, LOW); digitalWrite(c, LOW); digitalWrite(a, LOW); digitalWrite(g, LOW); digitalWrite(d, LOW); digitalWrite(e, HIGH); digitalWrite(f, HIGH); } void four() //display digit 4 { digitalWrite(b, LOW); digitalWrite(c, LOW); digitalWrite(a, HIGH); digitalWrite(g, LOW); digitalWrite(d, HIGH); digitalWrite(e, HIGH); digitalWrite(f, LOW); } void five() //display digit 5 { digitalWrite(b, HIGH); digitalWrite(c, LOW); digitalWrite(a, LOW); digitalWrite(g, LOW); digitalWrite(d, LOW); digitalWrite(e, HIGH); digitalWrite(f, LOW); } void six() //display digit 6 { digitalWrite(b, HIGH); digitalWrite(c, LOW); digitalWrite(a, LOW); digitalWrite(g, LOW); digitalWrite(d, LOW); digitalWrite(e, LOW); digitalWrite(f, LOW); } void seven() //display digit 7 { digitalWrite(b, LOW); digitalWrite(c, LOW); digitalWrite(a, LOW); digitalWrite(g, HIGH); digitalWrite(d, HIGH); digitalWrite(e, HIGH); digitalWrite(f, HIGH); } void eight() //display digit 8 { digitalWrite(b, LOW); digitalWrite(c, LOW); digitalWrite(a, LOW); digitalWrite(g, LOW); digitalWrite(d, LOW); digitalWrite(e, LOW); digitalWrite(f, LOW); } void nine() //display digit 9 { digitalWrite(b, LOW); digitalWrite(c, LOW); digitalWrite(a, LOW); digitalWrite(g, LOW); digitalWrite(d, HIGH); digitalWrite(e, HIGH); digitalWrite(f, LOW); } void off() //display is off { digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(a, HIGH); digitalWrite(g, HIGH); digitalWrite(d, HIGH); digitalWrite(e, HIGH); digitalWrite(f, HIGH); } void countdown() //calls on numbers for the 7-segment display { nine(); delay(1000); eight(); delay(1000); seven(); delay(1000); six(); delay(1000); five(); delay(1000); four(); delay(1000); three(); delay(1000); two(); delay(1000); one(); delay(1000); zero(); } void loop() { off(); //turns display off do //loop to simulate the delay timer and operate the push button and the PIR sensor during the delay { buttonState = digitalRead(button); //reads the button state (if it is pressed ot not) digitalWrite(red, HIGH); //turns the red LED and RGB LED (set to red) light on for 9 seconds if (buttonState == HIGH) //when the button is pressed on a red light, continue the program { break;//breaks out of the loop to coninue the program } else if (digitalRead(PIR) == 1)//when motion is detected on a red light, continue the program { break;//breaks out of the loop to coninue the program } else { counter = counter + 1; //increases count number to keep the red light on for 9 seconds Serial.println(" "); //gives a considerable delay, rather than just the counter } }while(counter < 800); //the number (800) must be adjusted for the red signal delay - this is due to different processor speed in the computer it runs on if (counter = 800) { counter = 0;//sets counter to 0 again } digitalWrite(red, LOW); digitalWrite(green, HIGH); countdown(); //counts down to red light digitalWrite(green, LOW); digitalWrite(yellow, HIGH); delay(3000); digitalWrite(yellow, LOW); }