Last active
August 5, 2018 18:55
-
-
Save sunsided/d370fe447c6a4dcdaf3d3be076ddc1ab to your computer and use it in GitHub Desktop.
Backup of "Another LED traffic light with the Arduino" (https://pastebin.com/f4f3d205a)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// arduino LED traffic light for cars and pedestrians | |
// Pin 7 gives power for the button, which is linked to pin 2 (--> IRQ 0) | |
// Red LEDs on 13 and 6, yellow LED on 12, green LEDs on 11 and 5, all with a 150 Ohm resistor | |
// http://www.youtube.com/watch?v=5iLaYhyKOtM | |
// http://www.vimeo.com/9589691 | |
// Sketch backup from https://pastebin.com/f4f3d205a | |
const int trafficPinR = 13; | |
const int trafficPinY = 12; | |
const int trafficPinG = 11; | |
const int buttonEnergyPin = 7; | |
const int pedestrianPinR = 6; | |
const int pedestrianPinG = 5; | |
const int irq = 0; | |
const int durationMs = 4000; | |
int lastTime; | |
typedef enum EState { | |
INVALID = -1, | |
RED = 0, | |
REDWAIT = 1, | |
YELLOW = 2, | |
GREEN = 3, | |
GREENWAIT = 4, | |
REDYELLOW = 5 | |
} State; | |
State light = RED; | |
volatile int buttonPressed = 0; | |
int switchPedestrianToGreen = 0; | |
// Pedestrian light class | |
typedef class CRedGreenLight | |
{ | |
private: | |
State light; | |
int pinRed; | |
int pinGreen; | |
int timer; | |
public: | |
CRedGreenLight(int redPin, int greenPin) : light(INVALID), timer(0), pinRed(redPin), pinGreen(greenPin) | |
{ | |
} | |
void setup() | |
{ | |
pinMode(pinRed, OUTPUT); | |
pinMode(pinGreen, OUTPUT); | |
switchToRed(); | |
} | |
int isRed() | |
{ | |
return light == REDWAIT; | |
} | |
void pulse(int deltaT) | |
{ | |
timer += deltaT; | |
if(timer > durationMs) | |
{ | |
switch(light) | |
{ | |
case GREEN: | |
light = GREENWAIT; | |
timer = -durationMs; | |
break; | |
case GREENWAIT: | |
switchToRed(); | |
break; | |
case RED: | |
light = REDWAIT; | |
timer = 0; | |
break; | |
case REDWAIT: | |
break; | |
} | |
} | |
} | |
void switchToRed() | |
{ | |
if(light == RED || light == REDWAIT) return; | |
Serial.println("- Pedestrian light: Red."); | |
light = RED; | |
digitalWrite(pinGreen, LOW); | |
digitalWrite(pinRed, HIGH); | |
timer = 0; | |
} | |
void switchToGreen() | |
{ | |
if(light == GREEN || light == GREENWAIT) return; | |
Serial.println("- Pedestrian light: Green."); | |
light = GREEN; | |
digitalWrite(pinRed, LOW); | |
digitalWrite(pinGreen, HIGH); | |
timer = 0; | |
} | |
} RedGreenLight; | |
// Traffic light class | |
typedef class CRedYellowGreenLight | |
{ | |
private: | |
int pinRed; | |
int pinYellow; | |
int pinGreen; | |
State currentState; | |
State nextState; | |
int timer; | |
public: | |
CRedYellowGreenLight(int redPin, int yellowPin, int greenPin) : pinRed(redPin), pinYellow(yellowPin), pinGreen(greenPin), currentState(INVALID), nextState(INVALID) | |
{ | |
} | |
void setup() | |
{ | |
pinMode(pinRed, OUTPUT); | |
pinMode(pinYellow, OUTPUT); | |
pinMode(pinGreen, OUTPUT); | |
dispatch(RED); | |
} | |
void pulse(int deltaT) | |
{ | |
timer += deltaT; | |
if(timer > durationMs) setState(nextState); | |
} | |
int isRed() | |
{ | |
return currentState == REDWAIT; | |
} | |
int isGreen() | |
{ | |
return currentState == GREENWAIT; | |
} | |
void switchToRed() | |
{ | |
if(currentState == RED || currentState == REDWAIT || currentState == YELLOW) return; | |
Serial.println("+ Switching traffic light to YELLOW..."); | |
setState(YELLOW); | |
} | |
void switchToGreen() | |
{ | |
if(currentState == REDYELLOW || currentState == GREEN || currentState == GREENWAIT) return; | |
Serial.println("+ Switching traffic light to GREEN..."); | |
setState(REDYELLOW); | |
} | |
private: | |
void setState(State state) | |
{ | |
if(state != INVALID) | |
{ | |
nextState = dispatch(state); | |
timer = 0; | |
} | |
} | |
State dispatch(State state) | |
{ | |
switch(state) | |
{ | |
case YELLOW: | |
Serial.println("- Traffic light: Yellow."); | |
currentState = YELLOW; | |
digitalWrite(pinYellow, HIGH); | |
digitalWrite(pinGreen, LOW); | |
return RED; | |
case RED: | |
Serial.println("- Traffic light: Red."); | |
currentState = RED; | |
digitalWrite(pinRed, HIGH); | |
digitalWrite(pinYellow, LOW); | |
return REDWAIT; | |
case REDWAIT: | |
Serial.println("- Traffic light: Red (unlocked)."); | |
currentState = REDWAIT; | |
return INVALID; | |
case REDYELLOW: | |
Serial.println("- Traffic light: Red+yellow."); | |
currentState = REDYELLOW; | |
digitalWrite(pinRed, HIGH); | |
digitalWrite(pinYellow, HIGH); | |
digitalWrite(pinGreen, LOW); | |
return GREEN; | |
case GREEN: | |
Serial.println("- Traffic light: Green."); | |
currentState = GREEN; | |
digitalWrite(pinRed, LOW); | |
digitalWrite(pinYellow, LOW); | |
digitalWrite(pinGreen, HIGH); | |
return GREENWAIT; | |
case GREENWAIT: | |
Serial.println("- Traffic light: Green (unlocked)."); | |
currentState = GREENWAIT; | |
return INVALID; | |
} | |
} | |
} RedYellowGreenLight; | |
RedGreenLight pedestrianLight(pedestrianPinR, pedestrianPinG); | |
RedYellowGreenLight trafficLight(trafficPinR, trafficPinY, trafficPinG); | |
void setup() | |
{ | |
Serial.begin(9600); | |
Serial.println("* Initializing ..."); | |
// 5V on the energy pin | |
pinMode(buttonEnergyPin, OUTPUT); | |
digitalWrite(buttonEnergyPin, HIGH); | |
// Attach ISR to button | |
attachInterrupt(irq, onButtonPressed, RISING); | |
// Initialize lights (setting them to red) | |
pedestrianLight.setup(); | |
trafficLight.setup(); | |
// Set last time | |
lastTime = millis(); | |
// Switch traffic light to green | |
delay(2000); | |
trafficLight.switchToGreen(); | |
} | |
// ISR | |
void onButtonPressed() | |
{ | |
buttonPressed = 1; | |
} | |
void loop() | |
{ | |
// Calculate time difference | |
int currentTime = millis(); | |
int deltaT = currentTime - lastTime; | |
lastTime = currentTime; | |
// React on Serial input | |
if(Serial.available()) | |
{ | |
if(Serial.read() == ' ') | |
{ | |
Serial.println("* Virtual button pressed."); | |
buttonPressed = 1; | |
} | |
} | |
// React on ISR signal | |
if(buttonPressed) | |
{ | |
if(!switchPedestrianToGreen) Serial.println("* Button pressed."); | |
buttonPressed = 0; | |
switchPedestrianToGreen = 1; | |
} | |
// Dispatch "button pressed" event | |
if(switchPedestrianToGreen) | |
{ | |
if(trafficLight.isGreen()) | |
{ | |
trafficLight.switchToRed(); | |
} | |
else if(trafficLight.isRed()) | |
{ | |
switchPedestrianToGreen = 0; | |
pedestrianLight.switchToGreen(); | |
} | |
} | |
else if(pedestrianLight.isRed()) | |
{ | |
trafficLight.switchToGreen(); | |
} | |
// Refresh lights | |
pedestrianLight.pulse(deltaT); | |
trafficLight.pulse(deltaT); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment