#define PIN_RED 2 // The Arduino pin connected to R pin of traffic light module
#define PIN_YELLOW 3 // The Arduino pin connected to Y pin of traffic light module
#define PIN_GREEN 4 // The Arduino pin connected to G pin of traffic light module
//#define RED_TIME 2000 // RED time in millisecond
#define YELLOW_TIME 1000 // YELLOW time in millisecond
//#define GREEN_TIME 2000 // GREEN time in millisecond
#define RED 1 // Index in array
#define YELLOW 0 // Index in array
#define GREEN 2 // Index in array
const int pins[] = { PIN_YELLOW, PIN_GREEN, PIN_RED };
const int times = YELLOW_TIME ;
const int inputPin = 5;
unsigned long last_time = 0;
int light = GREEN; // start with RED light
int pirState; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int minimummSecsLowForInactive = 5000; // If the sensor reports low for
// more than this time, then assume no activity
long unsigned int timeLow;
long unsigned int RED_TIME;
long unsigned int GREEN_TIME;
boolean takeLowTime;
void setup() {
Serial.begin(9600);
pinMode(PIN_RED, OUTPUT);
pinMode(PIN_YELLOW, OUTPUT);
pinMode(PIN_GREEN, OUTPUT);
pinMode(inputPin, INPUT); // declare sensor as input
trafic_light_on(light);
}
// loop the PIR sensor detection
void loop() {
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
if (pirState) {
int light = YELLOW;// start with yellow
trafic_light_on(light);
if ((millis() - last_time) > times) { //wait for yellow 1s
light++;//turn to redlight
trafic_light_on(light);
Serial.println("People Detection!");
delay(50);
// turn to red
pirState = false;
}
RED_TIME = false;// start red time
}
}
else {
if (RED_TIME){
timeLow = millis();
RED_TIME = false;
}
if(!pirState && millis() - timeLow > minimummSecsLowForInactive)
// in red light and wait until over 5s
{
pirState = true;// change back to original state to detect
int light = GREEN;
trafic_light_on(light);
Serial.print("People Pass Safely!") ;
delay(50);
last_time = millis();
}
}
}
void trafic_light_on(int light) {
if (light == RED){
// red light on
digitalWrite(PIN_RED, HIGH); // turn on
digitalWrite(PIN_YELLOW, LOW); // turn off
digitalWrite(PIN_GREEN, LOW); // turn off
delay(RED_TIME); // keep red light on during a period of time
}
else if(light == YELLOW){
// yellow light on
digitalWrite(PIN_RED, LOW); // turn off
digitalWrite(PIN_YELLOW, HIGH); // turn on
digitalWrite(PIN_GREEN, LOW); // turn off
delay(YELLOW_TIME); // keep yellow light on during a period of time
}
else if(light == GREEN){
// green light on
digitalWrite(PIN_RED, LOW); // turn off
digitalWrite(PIN_YELLOW, LOW); // turn off
digitalWrite(PIN_GREEN, HIGH); // turn on
delay(GREEN_TIME); // keep green light on during a period of time
}
}