Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Pin variables
- const int button = 3;
- const int greenCrossing = 9;
- const int redCrossing = 10;
- const int greenLight = 11;
- const int amberLight = 12;
- const int redLight = 13;
- // Time delay variables
- long delayTime = 0;
- long lastSequence = 0;
- // Interrupt variables
- bool buttonActive = true; // Allows for easy managing of when the button can be successfully pressed
- bool awaitingSequence = false; // Allows for the lighting sequence to run again after the delay
- void setup() {
- // Setup pin modes
- pinMode(button, INPUT_PULLUP);
- pinMode(greenCrossing, OUTPUT);
- pinMode(redCrossing, OUTPUT);
- pinMode(greenLight, OUTPUT);
- pinMode(amberLight, OUTPUT);
- pinMode(redLight, OUTPUT);
- // Interrupt service, allows the ButtonPressed function to run at any point in the code
- attachInterrupt(digitalPinToInterrupt(3), buttonPressed, RISING);
- }
- void defaultDisplay() {
- digitalWrite(greenLight, HIGH);
- digitalWrite(redCrossing, HIGH);
- }
- void flashingLED(int pin1, int pin2) {
- for (int i = 1; i <= 5; i++) {
- digitalWrite(pin1, HIGH);
- digitalWrite(pin2, HIGH);
- delay(500);
- digitalWrite(pin1, LOW);
- digitalWrite(pin2, LOW);
- delay(500);
- }
- }
- void crossSequence() { // Lighting sequence to run when the button has been pressed and not being blocked by the 60 second delay
- digitalWrite(greenLight, LOW);
- digitalWrite(amberLight, HIGH);
- delay(3000);
- digitalWrite(amberLight, LOW);
- digitalWrite(redLight, HIGH);
- digitalWrite(redCrossing, LOW);
- digitalWrite(greenCrossing, HIGH);
- delay(10000);
- digitalWrite(redLight, LOW);
- buttonActive = true; // Allow another button press from this point onwards
- flashingLED(greenCrossing, amberLight);
- digitalWrite(greenLight, HIGH);
- digitalWrite(redCrossing, HIGH);
- lastSequence = millis(); // Log the time that the execution has finished
- delayTime = 20000;
- }
- void buttonPressed() { // Function to run when the interrupt is executed
- if ( buttonActive == true ) { // Button is currently accepting a press
- if ( awaitingSequence == false ) { // Has a sequence been queued already
- awaitingSequence = true; // Allow the sequence to run again at the next appropriate time [not currently running & not blocked by 60 sec delay]
- buttonActive = false; // Prevent the button acknowledging any more presses
- }
- }
- }
- void loop() {
- defaultDisplay();
- if (millis() > (lastSequence + delayTime)) { // Not being blocked by the 60 second delay
- if ( awaitingSequence == true ) { // Has the button been pressed at a time where a new sequence can be `queued`
- awaitingSequence = false;
- crossSequence();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement