djchateau

Stop Light Revised

Jun 14th, 2013
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Stop Light
  3.   Simulates a stop light, Parallel project to my BASIC Stamp 2 Project
  4.  
  5. */
  6.  
  7. //  pin 9  for green
  8. //  pin 10 for yellow
  9. //  pin 11 for red
  10. #define green 9
  11. #define yellow 10
  12. #define red 11
  13.  
  14. // pin 6  for button input
  15. #define button 6;
  16.  
  17. //  walk status
  18. boolean walk = false;
  19.  
  20. void setLight(byte color){
  21.     digitalWrite(red,LOW);
  22.     digitalWrite(yellow,LOW);
  23.     digitalWrite(green,LOW);
  24.  
  25.         switch (color) {
  26.         case red:
  27.             digitalWrite(red,HIGH);
  28.             break;
  29.         case yellow:
  30.             digitalWrite(yellow,HIGH);
  31.             break;
  32.         case green:
  33.             digitalWrite(green,HIGH);
  34.             break;
  35.     }
  36. }
  37.  
  38. // setup
  39. void setup()  {
  40.   //  Initialize pins
  41.   pinMode(green, OUTPUT);
  42.   pinMode(yellow, OUTPUT);
  43.   pinMode(red, OUTPUT);
  44.   pinMode(button, INPUT);
  45.  
  46.   //debugging stuff
  47.   Serial.begin(9600);
  48. }
  49.  
  50. // main loop
  51. void loop()  {
  52.   // read the value of the switch
  53.   // digitalRead() checks to see if there is a voltage
  54.   // on the pin or not
  55.  
  56.     setLight(green);
  57.  
  58.     for (int i = 0; i <= 2000; i++) {
  59.         if (!walk) {
  60.             walk = digitalRead(button);
  61.             delay(1);
  62.         } else {
  63.             delay(2000 - i);
  64.             i = 2000;
  65.         }
  66.     }
  67.  
  68.     setLight(yellow);
  69.  
  70.     for (i=0; i <= 1000; i++) {
  71.         if (!walk) {
  72.             walk = digitalRead(button);
  73.             delay(1);
  74.         } else {
  75.             delay(1000-i);
  76.             i = 1000;
  77.         }
  78.     }
  79.  
  80.     setLight(red);
  81.  
  82.     for (i=0; i <= 2500; i++) {
  83.         if (!walk) {
  84.             walk = digitalRead(button);
  85.             delay(1);
  86.         } else {
  87.             delay(2500-i);
  88.             i = 2500;
  89.         }
  90.     }
  91.  
  92.     if (walk) {
  93.         delay(5000);
  94.     }
  95.  
  96.     walk = false;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment