SHARE
TWEET

Flashing traffic and pedestrian light with buzzer

a guest Sep 24th, 2011 647 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int carRed = 12;
  2. int carYellow = 11;
  3. int carGreen = 10;
  4. int pedRed = 9;
  5. int pedGreen = 8;
  6. int buzzer = 13;
  7. int button = 2;
  8. int crossTime = 5000;
  9. unsigned long changeTime; // since button pressed
  10.  
  11. void setup(){
  12.    pinMode(carRed,OUTPUT);
  13.    pinMode(carYellow,OUTPUT);
  14.    pinMode(carGreen,OUTPUT);
  15.   pinMode(pedRed,OUTPUT);
  16.   pinMode(pedGreen,OUTPUT);
  17.   pinMode(buzzer,OUTPUT);
  18.  pinMode(button,INPUT);
  19.  
  20.   // turn on the default mode
  21.  digitalWrite(carGreen,HIGH) ;
  22.  digitalWrite(pedRed,HIGH);
  23. }
  24.  
  25. void loop(){
  26.   int state = digitalRead(button);
  27.   // check if button has been pressed and its over 5 seconds since last press
  28.   if (state == HIGH && (millis() - changeTime)>5000){
  29.     changeLights();
  30.   }
  31. }
  32.  
  33. void changeLights(){
  34.     digitalWrite(carGreen,LOW);
  35.     digitalWrite(carYellow,HIGH);
  36.     delay(2000);
  37.    
  38.     digitalWrite(carYellow,LOW);
  39.     digitalWrite(carRed,HIGH);
  40.    delay(1000);
  41.  
  42.    digitalWrite(pedRed,LOW);
  43.    digitalWrite(pedGreen,HIGH);
  44.    
  45.     beep(crossTime,250,250,false);  
  46.     beep(4000,100,100,true);
  47.    
  48.    
  49.     // turn red back on for pedestrians
  50.    digitalWrite(pedRed,HIGH);
  51.   delay(1000);
  52.  
  53.    digitalWrite(carYellow,HIGH);
  54.    digitalWrite(carRed,LOW);
  55.    delay(1000);
  56.    
  57.    digitalWrite(carGreen,HIGH);
  58.    digitalWrite(carYellow,LOW);
  59.  
  60.  changeTime= millis();  
  61. }
  62.  
  63. // beep for total of how long, time on, time off, and whether to flash
  64. // the light at the same time
  65. void beep(unsigned int time,int intervala,int intervalb,boolean flash){
  66.    unsigned long beepTime = millis();
  67.    unsigned long stillBeeping = millis();
  68.    while((stillBeeping - beepTime)<time){
  69.      digitalWrite(buzzer,HIGH);
  70.      
  71.      if(flash == true){
  72.        digitalWrite(pedGreen,HIGH);
  73.      }
  74.      delay(intervala);
  75.      
  76.      digitalWrite(buzzer,LOW);
  77.      if(flash == true){
  78.        digitalWrite(pedGreen,LOW);
  79.      }
  80.      
  81.      delay(intervalb);
  82.      stillBeeping = millis();
  83.      
  84.    }
  85. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top