Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Pin variables
  2. const int button = 3;
  3. const int greenCrossing = 9;
  4. const int redCrossing = 10;
  5. const int greenLight = 11;
  6. const int amberLight = 12;
  7. const int redLight = 13;
  8.  
  9. // Time delay variables
  10. long delayTime = 0;
  11. long lastSequence = 0;
  12.  
  13. // Interrupt variables
  14. bool buttonActive = true; // Allows for easy managing of when the button can be successfully pressed
  15. bool awaitingSequence = false;  // Allows for the lighting sequence to run again after the delay
  16.  
  17. void setup() {
  18.   // Setup pin modes
  19.   pinMode(button, INPUT_PULLUP);
  20.   pinMode(greenCrossing, OUTPUT);
  21.   pinMode(redCrossing, OUTPUT);
  22.   pinMode(greenLight, OUTPUT);
  23.   pinMode(amberLight, OUTPUT);
  24.   pinMode(redLight, OUTPUT);
  25.  
  26.   // Interrupt service, allows the ButtonPressed function to run at any point in the code
  27.   attachInterrupt(digitalPinToInterrupt(3), buttonPressed, RISING);
  28. }
  29.  
  30.  
  31. void defaultDisplay() {
  32.   digitalWrite(greenLight, HIGH);
  33.   digitalWrite(redCrossing, HIGH);
  34. }
  35.  
  36. void flashingLED(int pin1, int pin2) {
  37.   for (int i = 1; i <= 5; i++) {
  38.     digitalWrite(pin1, HIGH);
  39.     digitalWrite(pin2, HIGH);
  40.     delay(500);
  41.     digitalWrite(pin1, LOW);
  42.     digitalWrite(pin2, LOW);
  43.     delay(500);
  44.   }
  45. }
  46.  
  47.  
  48. void crossSequence() { // Lighting sequence to run when the button has been pressed and not being blocked by the 60 second delay
  49.     digitalWrite(greenLight, LOW);
  50.     digitalWrite(amberLight, HIGH);
  51.     delay(3000);
  52.    
  53.     digitalWrite(amberLight, LOW);
  54.     digitalWrite(redLight, HIGH);
  55.     digitalWrite(redCrossing, LOW);
  56.     digitalWrite(greenCrossing, HIGH);  
  57.     delay(10000);
  58.    
  59.     digitalWrite(redLight, LOW);
  60.     buttonActive = true; // Allow another button press from this point onwards
  61.     flashingLED(greenCrossing, amberLight);
  62.     digitalWrite(greenLight, HIGH);
  63.     digitalWrite(redCrossing, HIGH);
  64.     lastSequence = millis(); // Log the time that the execution has finished
  65.     delayTime = 20000;
  66. }
  67.  
  68. void buttonPressed() { // Function to run when the interrupt is executed
  69.   if ( buttonActive == true ) { // Button is currently accepting a press
  70.     if ( awaitingSequence == false ) { // Has a sequence been queued already
  71.         awaitingSequence = true; // Allow the sequence to run again at the next appropriate time [not currently running & not blocked by 60 sec delay]
  72.         buttonActive = false;    // Prevent the button acknowledging any more presses
  73.     }
  74.   }
  75. }
  76.  
  77. void loop() {
  78.   defaultDisplay();
  79.   if (millis() > (lastSequence + delayTime)) { // Not being blocked by the 60 second delay
  80.     if ( awaitingSequence == true ) { // Has the button been pressed at a time where a new sequence can be `queued`
  81.         awaitingSequence = false;
  82.         crossSequence();
  83.     }
  84.   }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement