Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Automated traffic light system that allows
  3.   interrupts for pedestrians.
  4. */
  5.  
  6. bool ButtonPressed = 0;
  7. bool executed = LOW;
  8. bool canInterrupt = HIGH;
  9. volatile bool changed = LOW;
  10.  
  11. const int INT_Pin1 = 3;
  12. const int INT_NO = 1;
  13.  
  14.  
  15.  
  16. void setup()
  17. {
  18.   Serial.begin(9600);
  19.   pinMode(8, OUTPUT); // Red Light
  20.   pinMode(9, OUTPUT); // Amber Light
  21.   pinMode(10, OUTPUT); // Green Light
  22.   pinMode(12, OUTPUT); // Pedestrian Red
  23.   pinMode(13, OUTPUT); // Pedrestrian Green
  24.   pinMode(3, INPUT); // Button
  25.   attachInterrupt(INT_NO, changedInt, FALLING);
  26. }
  27.  
  28. void changedInt() {
  29.   Serial.write("Interrupt1");
  30.   if (canInterrupt) {
  31.     changed = HIGH;
  32.     Serial.write("Interrupt2");
  33.   }
  34.  
  35. }
  36.  
  37. void changeLights() {
  38.   changed = LOW;
  39.   // Turn on the amber light and turn off the green light.
  40.   digitalWrite(10, LOW);
  41.   digitalWrite(9, HIGH);
  42.   delay(3000); // Wait 3 seconds for the amber light to stay on.
  43.  
  44.   // Turn off amber light, turn on the red light and turn on green pedestrian light.
  45.   digitalWrite(12, LOW);
  46.   digitalWrite(9, LOW);
  47.   digitalWrite(8, HIGH);
  48.   digitalWrite(13, HIGH);
  49.   delay(10000);
  50.  
  51.   // Flashing Stage: amber and green pedestrian light flash for 5 seconds, each is 0.5s
  52.   digitalWrite(8, LOW);
  53.   canInterrupt = HIGH; // Allows the interrupt to change the "changed" variable to indicate the button has been pressed
  54.   for (int i = 0; i < 5; i++) { // Loops through 5 times, to cause the lights to flash on and off with a 500ms delay between each flash
  55.     digitalWrite(9, LOW);
  56.     digitalWrite(13, LOW); // Turn off
  57.  
  58.     delay(500);
  59.  
  60.     digitalWrite(9, HIGH);
  61.     digitalWrite(13, HIGH); // Turn on for Flash
  62.     delay(500);
  63.  
  64.   }
  65.   if (changed) { // Converts the changed signal from the interrupt into a button push for the if statement
  66.     ButtonPressed = HIGH;
  67.   } else {
  68.     ButtonPressed = LOW;
  69.   }
  70.   digitalWrite(9, LOW);
  71.   digitalWrite(8, LOW);
  72.   digitalWrite(13, LOW);
  73.   executed = HIGH;
  74. }
  75.  
  76.  
  77.  
  78. void loop() {
  79.   digitalWrite(10, HIGH);
  80.   digitalWrite(12, HIGH);
  81.   if (executed) { // Checks that the program was previously executed so to use the 60 second delay. Otherwise there is no delay at the start of the program.
  82.     executed = LOW;
  83.     digitalWrite(10, HIGH);
  84.     digitalWrite(12, HIGH);
  85.     canInterrupt = LOW;
  86.     delay(10000);
  87.   }
  88.  
  89.   if (ButtonPressed) {
  90.     changeLights();
  91.   } else {
  92.     ButtonPressed = digitalRead(3);
  93.   }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement