I built around your code to have start lights and individual bumpers to signal each lane finish. There is a lot of room for improvement.
Modified to have an order of placing.
// https://forum.arduino.cc/t/turn-led-strip-on-when-cars-finish-race/1175747/
int redLED1 = 5, yellowLED1 = 6, greenLED1 = 7; // LEDs for lane 1
int redLED2 = 8, yellowLED2 = 9, greenLED2 = 10;
int redLED3 = 11, yellowLED3 = 12, greenLED3 = 13;
int end1 = 4, end2 = 3, end3 = 2; // bumper buttons for 1, 2, and 3
int place = 1; // finish order
void setup() {
Serial.begin(115200); // configure serial monitor
pinMode(redLED1, OUTPUT); // set redLED1 pin to OUTPUT
pinMode(yellowLED1, OUTPUT);
pinMode(greenLED1, OUTPUT);
pinMode(redLED2, OUTPUT);
pinMode(yellowLED2, OUTPUT);
pinMode(greenLED2, OUTPUT);
pinMode(redLED3, OUTPUT);
pinMode(yellowLED3, OUTPUT);
pinMode(greenLED3, OUTPUT);
pinMode(end1, INPUT_PULLUP); // set end1 pin to INPUT with PULLUP resistor
pinMode(end2, INPUT_PULLUP);
pinMode(end3, INPUT_PULLUP);
welcome(); // call the welcome(); function once
race(); // call the race(); function once
}
void loop() {
bumper();
}
void race() {
// Countdown with lights then sets to yellow
lane1("red"); // call function lane1(); with red as the argument
lane2("red");
lane3("red");
delay(1000); // wait for one second
lane1("yellow");
lane2("yellow");
lane3("yellow");
delay(1000);
lane1("green");
lane2("green");
lane3("green");
delay(3000);
lane1("yellow");
lane2("yellow");
lane3("yellow");
}
void lane1(char color[]) { // recieve an array of characters for the color
if (color == "red") { // when red is passed to this function
digitalWrite(redLED1, HIGH); // turn red LED on
digitalWrite(yellowLED1, LOW); // turn yellow LED off
digitalWrite(greenLED1, LOW); // turn green LED off
}
if (color == "yellow") {
digitalWrite(redLED1, LOW);
digitalWrite(yellowLED1, HIGH);
digitalWrite(greenLED1, LOW);
}
if (color == "green") {
digitalWrite(redLED1, LOW);
digitalWrite(yellowLED1, LOW);
digitalWrite(greenLED1, HIGH);
}
}
void lane2(char color[]) {
if (color == "red") {
digitalWrite(redLED2, HIGH);
digitalWrite(yellowLED2, LOW);
digitalWrite(greenLED2, LOW);
}
if (color == "yellow") {
digitalWrite(redLED2, LOW);
digitalWrite(yellowLED2, HIGH);
digitalWrite(greenLED2, LOW);
}
if (color == "green") {
digitalWrite(redLED2, LOW);
digitalWrite(yellowLED2, LOW);
digitalWrite(greenLED2, HIGH);
}
}
void lane3(char color[]) {
if (color == "red") {
digitalWrite(redLED3, HIGH);
digitalWrite(yellowLED3, LOW);
digitalWrite(greenLED3, LOW);
}
if (color == "yellow") {
digitalWrite(redLED3, LOW);
digitalWrite(yellowLED3, HIGH);
digitalWrite(greenLED3, LOW);
}
if (color == "green") {
digitalWrite(redLED3, LOW);
digitalWrite(yellowLED3, LOW);
digitalWrite(greenLED3, HIGH);
}
}
void bumper() {
if (place < 4) { // if all lanes finished, no need to read buttons
while ((digitalRead(end1) == LOW) || (digitalRead(end2) == LOW) || (digitalRead(end3) == LOW)) {
//Code to change LED lights in the order that the cars finish
if (!digitalRead(end1)) { // if bumper 1 is pressed
delay(150);
digitalWrite(redLED1, HIGH); // light LED 1
Serial.print("Lane 1 Place: ");
Serial.println(place++); // increment Place
}
if (!digitalRead(end2)) {
delay(150);
digitalWrite(redLED2, HIGH);
Serial.print("Lane 2 Place: ");
Serial.println(place++);
}
if (!digitalRead(end3)) {
delay(150);
digitalWrite(redLED3, HIGH);
Serial.print("Lane 3 Place: ");
Serial.println(place++);
}
}
}
}
void welcome() {
Serial.println("Wait for final yellow. Press a button.");
}
diagram.json for wokwi.com
{
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-nano", "id": "nano", "top": 33.6, "left": -0.5, "attrs": {} },
{
"type": "wokwi-pushbutton",
"id": "btn1",
"top": -61,
"left": 182.4,
"attrs": { "color": "green" }
},
{
"type": "wokwi-pushbutton",
"id": "btn2",
"top": -109,
"left": 182.4,
"attrs": { "color": "green" }
},
{
"type": "wokwi-pushbutton",
"id": "btn3",
"top": -157,
"left": 182.4,
"attrs": { "color": "green" }
},
{
"type": "wokwi-led",
"id": "led1",
"top": -186,
"left": -149.8,
"attrs": { "color": "green" }
},
{
"type": "wokwi-resistor",
"id": "r1",
"top": -149.65,
"left": -201.6,
"attrs": { "value": "330" }
},
{
"type": "wokwi-led",
"id": "led2",
"top": -176.4,
"left": -130.6,
"attrs": { "color": "yellow" }
},
{
"type": "wokwi-resistor",
"id": "r2",
"top": -140.05,
"left": -201.6,
"attrs": { "value": "330" }
},
{
"type": "wokwi-led",
"id": "led3",
"top": -166.8,
"left": -111.4,
"attrs": { "color": "red" }
},
{
"type": "wokwi-resistor",
"id": "r3",
"top": -130.45,
"left": -201.6,
"attrs": { "value": "330" }
},
{
"type": "wokwi-led",
"id": "led4",
"top": -128.4,
"left": -149.8,
"attrs": { "color": "green" }
},
{
"type": "wokwi-resistor",
"id": "r4",
"top": -92.05,
"left": -201.6,
"attrs": { "value": "330" }
},
{
"type": "wokwi-led",
"id": "led5",
"top": -118.8,
"left": -130.6,
"attrs": { "color": "yellow" }
},
{
"type": "wokwi-resistor",
"id": "r5",
"top": -82.45,
"left": -201.6,
"attrs": { "value": "330" }
},
{
"type": "wokwi-led",
"id": "led6",
"top": -109.2,
"left": -111.4,
"attrs": { "color": "red" }
},
{
"type": "wokwi-resistor",
"id": "r6",
"top": -72.85,
"left": -201.6,
"attrs": { "value": "330" }
},
{
"type": "wokwi-led",
"id": "led7",
"top": -70.8,
"left": -149.8,
"attrs": { "color": "green" }
},
{
"type": "wokwi-resistor",
"id": "r7",
"top": -34.45,
"left": -201.6,
"attrs": { "value": "330" }
},
{
"type": "wokwi-led",
"id": "led8",
"top": -61.2,
"left": -130.6,
"attrs": { "color": "yellow" }
},
{
"type": "wokwi-resistor",
"id": "r8",
"top": -24.85,
"left": -201.6,
"attrs": { "value": "330" }
},
{
"type": "wokwi-led",
"id": "led9",
"top": -51.6,
"left": -111.4,
"attrs": { "color": "red" }
},
{
"type": "wokwi-resistor",
"id": "r9",
"top": -15.25,
"left": -201.6,
"attrs": { "value": "330" }
}
],
"connections": [
[ "nano:GND.2", "btn1:2.l", "black", [ "v0" ] ],
[ "btn1:1.l", "nano:2", "green", [ "h0" ] ],
[ "nano:GND.2", "btn2:2.l", "black", [ "v0" ] ],
[ "nano:3", "btn2:1.l", "green", [ "v0" ] ],
[ "nano:GND.2", "btn3:2.l", "black", [ "v0" ] ],
[ "led1:C", "r1:2", "green", [ "v0" ] ],
[ "led2:C", "r2:2", "green", [ "v0" ] ],
[ "led3:C", "r3:2", "green", [ "v0" ] ],
[ "led4:C", "r4:2", "green", [ "v0" ] ],
[ "led5:C", "r5:2", "green", [ "v0" ] ],
[ "led6:C", "r6:2", "green", [ "v0" ] ],
[ "led7:C", "r7:2", "green", [ "v0" ] ],
[ "led8:C", "r8:2", "green", [ "v0" ] ],
[ "led9:C", "r9:2", "green", [ "v0" ] ],
[ "nano:4", "btn3:1.l", "green", [ "v0" ] ],
[ "nano:5", "led3:A", "green", [ "v0" ] ],
[ "nano:6", "led2:A", "green", [ "v0" ] ],
[ "nano:7", "led1:A", "green", [ "v0" ] ],
[ "nano:GND.2", "r9:1", "black", [ "v-38.4", "h-326.4", "v-9.6" ] ],
[ "r9:1", "r8:1", "green", [ "v0" ] ],
[ "r8:1", "r7:1", "green", [ "v0" ] ],
[ "r7:1", "r6:1", "green", [ "v0" ] ],
[ "r6:1", "r5:1", "green", [ "v0" ] ],
[ "r5:1", "r4:1", "green", [ "v0" ] ],
[ "r4:1", "r3:1", "green", [ "v0" ] ],
[ "r3:1", "r2:1", "green", [ "v0" ] ],
[ "r2:1", "r1:1", "green", [ "v0" ] ],
[ "nano:13", "led7:A", "green", [ "v9.6", "h-9.6", "v-134.4" ] ],
[ "nano:12", "led8:A", "green", [ "v0" ] ],
[ "nano:11", "led9:A", "green", [ "v0" ] ],
[ "nano:10", "led4:A", "green", [ "v0" ] ],
[ "nano:9", "led5:A", "green", [ "v0" ] ],
[ "nano:8", "led6:A", "green", [ "v0" ] ]
],
"dependencies": {}
}