Last active
May 28, 2016 15:08
-
-
Save chibani/ecc079ce30894ff5434bbb0690af7b68 to your computer and use it in GitHub Desktop.
Mini trafic lights (mainly for kids) with an Arduino
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
// Traffic lights | |
// See : (french) http://blog.loicg.net/bricolage/2016/05/27/feux-tricolores-arduino.html | |
const int roadA[3] = {7,8,9};// Pins mapping for A group (Green, Orange, Red) | |
const int roadB[3] = {10,11,12}; // Pins mapping for B group (Green, Orange, Red) | |
const int sequenceA[6] = {0,1,2,2,2,2};// Lights sequence for A (Green, Orange, Red, Red, Red, Red) | |
const int sequenceB[6] = {2,2,2,0,1,2};// Lights sequence for B (Red, Red, Red, Green, Orange, Red) | |
const int timing[3] = {10,2,1};// In seconds (time for Green, Orange, Red) | |
const int timeSequence[6] = {timing[0], timing[1],timing[2],timing[0], timing[1],timing[2]};// Green (A), Orange (A), Red (both), Green (B), Orange (B), Red (both) | |
volatile int sequence = 0; // Sequence number. Will go from 0 to 5, then back to 0. | |
void setup() { | |
//Pins setup | |
for(int a=0; a < 3; a++){ | |
pinMode(roadA[a], OUTPUT); | |
pinMode(roadB[a], OUTPUT); | |
// Light on/off (to check if they work) | |
digitalWrite(roadA[a], HIGH); | |
digitalWrite(roadB[a], HIGH); | |
delay(200); | |
digitalWrite(roadA[a], LOW); | |
digitalWrite(roadB[a], LOW); | |
delay(200); | |
} | |
} | |
void loop() { | |
// Use sequence configuration | |
setLights(sequence); | |
// Some sleep, to let the light shine | |
delay(timeSequence[sequence]*1000); | |
// Update the sequence (for next loop) | |
if(sequence==5){ | |
sequence=0; | |
}else{ | |
sequence++; | |
} | |
} | |
// Will set the good light on, according to the current sequence number | |
void setLights(int i) { | |
// Loop through the 3 colors | |
for(int a=0; a < 3; a++){ | |
// If current sequence for A corresponds to the current color | |
if(sequenceA[i]==a){ | |
digitalWrite(roadA[a], HIGH); // We turn this light on | |
}else{ | |
digitalWrite(roadA[a], LOW); // Otherway, we turn it off | |
} | |
// The same logic applies to B... | |
if(sequenceB[i]==a){ | |
digitalWrite(roadB[a], HIGH); | |
}else{ | |
digitalWrite(roadB[a], LOW); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment