After some coercion from @jaaaaam I finally bought an Arduino from EarthShine Design.
The kit contains lots of things to get started with the Arduino, as well as being accompanied by a basic starter manual.
For the last few weeks i’ve been teaching myself to program using Processing using the Learning Processing book on O’Reilly’s Safari service.
Having no prior knowledge of programming, it took some time to get my head around how things work.
One of the examples in the starter manual was a traffic lights system.
The example given would just cycle a loop, and when you push the button, the loop would reset and it would run again.
I decided i’d like to try and challenge myself and change this.
I wanted the lights to
- Loop the car lights in sequence
- Check for request from the button for a pedestrian to cross
- If there is a request, start the crossing sequence
- Once crossing is completed, return to the car lights sequence
Here is the code I came up with
// Car light pins
int carRed = 12;
int carYellow = 11;
int carGreen = 10;
//Pedestrian light pins
int pedRed = 9;
int pedGreen = 8;
//Crossing button
int button = 2;
//Delay times
int crossTime = 5000;
int delayTime = 3000;
unsigned long previousMillis = 0;
//Crossing request
int request = 0;
// Array for all lights
int allLights[] = {
12 , 11, 10, 9 ,8};
// Array for carGreen pedGreen
int carsGo[] = {
10, 9};
// Array for carYellow pedGreen
int carsWait[] = {
11, 9};
// Array for carRed pedRed
int carsStop[] = {
12, 9};
// Array for carRed pedGreen
int pedsGo[] = {
12, 8};
void setup() {
//Set pins to output
pinMode(carRed, OUTPUT);
pinMode(carYellow, OUTPUT);
pinMode(carGreen, OUTPUT);
pinMode(pedRed, OUTPUT);
pinMode(pedGreen, OUTPUT);
//Set button pin to input
pinMode(button, INPUT);
//Set Car lights to green and Pedestrian lights to red
digitalWrite(carGreen, HIGH);
digitalWrite(pedRed, HIGH);
}
void loop(){
//Start normal traffic light sequence
normalLights();
//Check for crossing request
checkCrossingRequest();
//If crossing request is present, run pedestrian crossing sequence
lightsCheck();
}
//Function to turn all lights off
void allLightsOff(){
for(int i = 0 ; i < 5; i++){
digitalWrite(allLights[i], LOW);
}
}
//Function for pedestrian lights
void pedLights(){
//Delay for .5 sec
delay(500);
// Turn off all lights, turn on pedestrian red, turn on car yellow
allLightsOff();
digitalWrite(pedRed, HIGH);
digitalWrite(carYellow , HIGH);
//Delay for 1 sec
delay(1000);
// Turn off all lights, turn on pedestrian red, turn on car red
allLightsOff();
digitalWrite(pedRed, HIGH);
digitalWrite(carRed, HIGH);
//Delay for 1 sec
delay(1000);
// Turn off all lights, turn on pedestrian green, turn on car red
digitalWrite(pedRed, LOW);
digitalWrite(pedGreen, HIGH);
//Delay for crossing time
delay(crossTime);
//Flash lights 10 times, each time taking .5 sec
for (int x=0; x<10; x++) {
digitalWrite(pedGreen, HIGH);
delay(250);
digitalWrite(pedGreen, LOW);
delay(250);
}
//Turn off all lights, turn on pedestrian red, turn on car red
allLightsOff();
digitalWrite(carRed, HIGH);
digitalWrite(pedRed, HIGH);
//Wait 1 second
delay(1000);
}
//Normal light sequence
void normalLights(){
//Set currentMillis to the current time on the board
unsigned long currentMillis = millis();
//If current time minus the time the loop was last run is greater than 4 times the delay time, reset last run time
if(currentMillis - previousMillis > delayTime*4){
previousMillis = currentMillis;
}
// if loop lenght time is greater than 3 times the delay time, set car lights yellow off and red on
else if(currentMillis - previousMillis > delayTime*3){
digitalWrite(carYellow, LOW);
digitalWrite(carRed, HIGH);
}
// if loop lenght time is greater than 2 times the delay time, set car lights green off and yellow on
else if(currentMillis - previousMillis > delayTime*2){
digitalWrite(carGreen, LOW);
digitalWrite(carYellow, HIGH);
}
// if loop lenght time is greater than the delay time, set car lights red off and green on
else if(currentMillis - previousMillis < delayTime) {
digitalWrite(carRed, LOW);
digitalWrite(carGreen, HIGH);
}
}
//Check for a crossing request and return a value of 1 or 0
int checkCrossingRequest(){
//Set val to button
int val = digitalRead(button);
if (val == HIGH) {
return 1;
}
else{
return 0;
}
}
//Check the value of the crossing request, if 0 do nothing, if 1 do pedestrian sequence
void lightsCheck(){
request = checkCrossingRequest();
if (request > 0) {
pedLights();
}
}
Here is the hardware side of things
I’m looking forward to hooking up the 8×8 led display and 16×2 LCD I have.
Further projects will involve CoffeeTronics i hope









