//traffic lights:red,yellow,green
fill(trafficFirstLightColor)
ellipse(315, 40, 20, 20)
fill(trafficSecondLightColor)
ellipse(315, 40 + 25 * 1, 20, 20)
fill(trafficThirdLightColor)
ellipse(315, 40 + 25 * 2, 20, 20)
//if three lights with black, first light on
x+=10
if (trafficFirstLightColor === "black" && trafficSecondLightColor === "black" && trafficThirdLightColor === "black" && x == 3000) {
setTimeout(() => {
trafficFirstLightColor = 'red';
}, x)
} else if (trafficFirstLightColor === "red" && x == 6000) {
trafficFirstLightColor = "black"
setTimeout(() => {
trafficSecondLightColor = 'yellow';
}, x-3000)
} else if (trafficSecondLightColor === "yellow" && x == 9000) {
trafficSecondLightColor = "black"
setTimeout(() => {
trafficThirdLightColor = 'green';
}, x-6000)
}
The attempt at creating a color changing traffic light every 3 seconds was unsuccessful...
Initially, all three traffic lights were set to black and assigned specific variables. Another variable x was created to be used in the if-else statement. When all lights are black, the first one turns red. Then after 3 seconds, the red light switches to yellow. Finally, after an additional 3 seconds, the yellow light changes to green. The plan seemed good but the code implementation had issues.