Created
January 6, 2011 19:27
-
-
Save havvg/768416 to your computer and use it in GitHub Desktop.
Basic Arduino sketch implementing two cross linked traffic lights.
This file contains 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
class TrafficLight | |
{ | |
/** | |
* Accessors for traffic lights. | |
* | |
* Each of these values define the index, where to find a given part of the respective traffic light. | |
*/ | |
static const int green = 0; | |
static const int yellow = 1; | |
static const int red = 2; | |
// The list of the light pins. | |
int lights[3]; | |
// The pin of the inductor. | |
int inductor; | |
// The state of the inductor. | |
int state; | |
/** | |
* This is a linked traffic light that will operate with this one. | |
*/ | |
TrafficLight *tLink; | |
public: | |
/** | |
* Creates a new traffic light, set all lights to OUTPUT and the inductor to INPUT. | |
*/ | |
TrafficLight(int green, int yellow, int red, int inductor) | |
{ | |
setGreenLightPin(green); | |
setYellowLightPin(yellow); | |
setRedLightPin(red); | |
setInductorPin(inductor); | |
reset(); | |
state = 0; | |
} | |
/** | |
* When desctructing a traffic light, we deactivate all lights. | |
*/ | |
~TrafficLight() | |
{ | |
reset(); | |
} | |
void setLink(TrafficLight &tLight) | |
{ | |
tLink = &tLight; | |
} | |
void reset() | |
{ | |
disableGreen(); | |
disableYellow(); | |
disableRed(); | |
} | |
int getState() | |
{ | |
return state; | |
} | |
void switchRed() | |
{ | |
disableGreen(); | |
enableYellow(); | |
delay(1000); | |
disableYellow(); | |
enableRed(); | |
if (tLink) | |
{ | |
delay(1000); | |
tLink->switchGreen(); | |
} | |
} | |
void switchGreen() | |
{ | |
if (tLink) | |
{ | |
tLink->switchRed(); | |
delay(1000); | |
} | |
enableYellow(); | |
delay(1000); | |
disableYellow(); | |
disableRed(); | |
enableGreen(); | |
} | |
protected: | |
void enableGreen() | |
{ | |
digitalWrite(lights[green], HIGH); | |
} | |
void disableGreen() | |
{ | |
digitalWrite(lights[green], LOW); | |
} | |
void enableYellow() | |
{ | |
digitalWrite(lights[yellow], HIGH); | |
} | |
void disableYellow() | |
{ | |
digitalWrite(lights[yellow], LOW); | |
} | |
void enableRed() | |
{ | |
digitalWrite(lights[red], HIGH); | |
} | |
void disableRed() | |
{ | |
digitalWrite(lights[red], LOW); | |
} | |
void setGreenLightPin(int pin) | |
{ | |
lights[green] = pin; | |
pinMode(pin, OUTPUT); | |
} | |
void setYellowLightPin(int pin) | |
{ | |
lights[yellow] = pin; | |
pinMode(pin, OUTPUT); | |
} | |
void setRedLightPin(int pin) | |
{ | |
lights[red] = pin; | |
pinMode(pin, OUTPUT); | |
} | |
void setInductorPin(int pin) | |
{ | |
inductor = pin; | |
pinMode(pin, INPUT); | |
} | |
}; | |
/** | |
* We have two different traffic lights right now. | |
*/ | |
const int nTrafficLights = 2; | |
TrafficLight trafficLights[] = { | |
TrafficLight(11, 12, 13, 4), | |
TrafficLight(8, 9, 10, 2) | |
}; | |
void setup() | |
{ | |
trafficLights[0].setLink(trafficLights[1]); | |
reset(); | |
} | |
void reset() | |
{ | |
for (int i = 0; i < nTrafficLights; i++) | |
{ | |
trafficLights[i].reset(); | |
} | |
} | |
void loop() | |
{ | |
trafficLights[0].switchRed(); | |
delay(2000); | |
trafficLights[0].switchGreen(); | |
delay(2000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment