Arduino PIR Traffic Light

CLICK TITLE FOR MORE INFORMATION   




Wow! I will have traffic lights in my house now with wireless sensing capability with a PIR sensor. When the PIR doesn't sense anyone in front of it, it lights the red LED. If it does, the Arduino starts calculating the time once it detects something. If the time is below 5 seconds, the yellow LED will light. Once over 5 seconds, the red LED will light up. It will turn back to red once the PIR go back to LOW. 



And I've used:

Arduino Uno
Breadboard
Jumper Wires
10K Ohm Resistors
LEDs
PIR Sensor
And here's the sketch:

int PIR=7; 
int grn=2; 
int ylw=3; 
int red=4; 
int stat; 
unsigned long time; 
unsigned long sec; 



void setup(){
  pinMode(PIR,INPUT); 
  pinMode(grn,OUTPUT);
  pinMode(ylw,OUTPUT);
  pinMode(red,OUTPUT);
  delay(2500); 
}

void loop(){
  stat=digitalRead(PIR); 
  time=millis()/1000; 
  if(stat==HIGH){ 
    if(time++){ 
      sec++;    
    }
  }
  else{ 
    time!=sec; 
    sec=0; 
  }
  if(sec==0){ 
    digitalWrite(grn,LOW); 
    digitalWrite(ylw,LOW); 
    digitalWrite(red,HIGH); 
  }
  else if(sec>0 && sec<5){ 
    digitalWrite(grn,LOW); 
    digitalWrite(ylw,HIGH); 
    digitalWrite(red,LOW); 
  }
  else{ 
    digitalWrite(grn,HIGH); 
    digitalWrite(ylw,LOW); 
    digitalWrite(red,LOW); 
  }
  delay(1000);
}

And finally the video. Enjoy!



by Weng Fei

    
  
  


Related Posts Plugin for WordPress, Blogger...

Leave a Reply

Popular Posts