Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Automated traffic light system that allows
- interrupts for pedestrians.
- */
- bool ButtonPressed = 0;
- bool executed = LOW;
- bool canInterrupt = HIGH;
- volatile bool changed = LOW;
- const int INT_Pin1 = 3;
- const int INT_NO = 1;
- void setup()
- {
- Serial.begin(9600);
- pinMode(8, OUTPUT); // Red Light
- pinMode(9, OUTPUT); // Amber Light
- pinMode(10, OUTPUT); // Green Light
- pinMode(12, OUTPUT); // Pedestrian Red
- pinMode(13, OUTPUT); // Pedrestrian Green
- pinMode(3, INPUT); // Button
- attachInterrupt(INT_NO, changedInt, FALLING);
- }
- void changedInt() {
- Serial.write("Interrupt1");
- if (canInterrupt) {
- changed = HIGH;
- Serial.write("Interrupt2");
- }
- }
- void changeLights() {
- changed = LOW;
- // Turn on the amber light and turn off the green light.
- digitalWrite(10, LOW);
- digitalWrite(9, HIGH);
- delay(3000); // Wait 3 seconds for the amber light to stay on.
- // Turn off amber light, turn on the red light and turn on green pedestrian light.
- digitalWrite(12, LOW);
- digitalWrite(9, LOW);
- digitalWrite(8, HIGH);
- digitalWrite(13, HIGH);
- delay(10000);
- // Flashing Stage: amber and green pedestrian light flash for 5 seconds, each is 0.5s
- digitalWrite(8, LOW);
- canInterrupt = HIGH; // Allows the interrupt to change the "changed" variable to indicate the button has been pressed
- 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
- digitalWrite(9, LOW);
- digitalWrite(13, LOW); // Turn off
- delay(500);
- digitalWrite(9, HIGH);
- digitalWrite(13, HIGH); // Turn on for Flash
- delay(500);
- }
- if (changed) { // Converts the changed signal from the interrupt into a button push for the if statement
- ButtonPressed = HIGH;
- } else {
- ButtonPressed = LOW;
- }
- digitalWrite(9, LOW);
- digitalWrite(8, LOW);
- digitalWrite(13, LOW);
- executed = HIGH;
- }
- void loop() {
- digitalWrite(10, HIGH);
- digitalWrite(12, HIGH);
- 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.
- executed = LOW;
- digitalWrite(10, HIGH);
- digitalWrite(12, HIGH);
- canInterrupt = LOW;
- delay(10000);
- }
- if (ButtonPressed) {
- changeLights();
- } else {
- ButtonPressed = digitalRead(3);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement