For our second assignment, we were asked to use code within our switch to do something creative with the LED lights. I decided to create a car game of sorts (however, having run of time, it was reduced to a lesser parody).
I coded 3 different LED lights to turn on as a traffic light would. This took a lot of help and I ended up learning new methods I hadn’t used before. I also wanted to attach a button pin to turn the traffic lights on and off which proved to be very difficult.
Lastly, I also wanted lights to turn on once the car touched the finish line – I did this using a simple circuit using foil and without any code.
If I had more time, I would have liked to build a proper traffic light and circuit and also two cars that could actually race.
int red = 2;
int yellow = 3;
int green = 4;
int buttonPin = 5;
// boolean variable to keep track of whether our sequence is on or off
bool start = false;
// variable to keep track of our prevous button state
int prevButtonState = LOW;
// a very large container of memory used as a variable for numbers that continually increase (Like time)
unsigned long triggerTime = 0;
// the interval between light changes
int interval = 2000;
// which light are we currently activating
int whichLight = 0;
void setup() {
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop() {
// read our button (switch)
int currentButtonState = digitalRead(buttonPin);
// if the button is currently being pressed and during the last frame it was not pressed then continue
if (currentButtonState == HIGH && prevButtonState == LOW) {
// flip start on or off
start = !start;
// reset our trigger time to the current time so we can trigger immediately
triggerTime = millis();
// reset our light sequence to the beginning
whichLight = 0;
}
// if sequence is activated then call changeLights()
if (start == true) {
changeLights();
} else { // otherwise turn everything off
digitalWrite(yellow, LOW);
digitalWrite(red, LOW);
digitalWrite(green, LOW);
}
// remember the current button state so we can have a previous button state the next time through loop
prevButtonState = currentButtonState;
}
// the actual sequence of light changes
void changeLights() {
// boolean to trigger the next light or not, start off by not triggering
bool triggerLight = false;
// get our current time
unsigned long currentMillis = millis();
// if the current time is greater than the time when we should trigger, THEN LET'S TRIGGER!
if (currentMillis >= triggerTime) {
triggerLight = true; // LET's TRIGGER
triggerTime = currentMillis + interval; // reset the trigger time for 2 seconds later
}
// if we're triggering the next light
if (triggerLight == true) {
// which light are we on
if (whichLight == 0) { // red
digitalWrite(red, HIGH);
} else if (whichLight == 1) { // yellow
digitalWrite(yellow, HIGH);
} else if (whichLight == 2) { // green
digitalWrite(yellow, LOW);
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
} else if (whichLight == 3) { // then turn everything off
start = false;
}
// increase the sequence to the next light, only to be triggered after the triggerTime == currentTime
whichLight = whichLight + 1;
}
}