ESP32 Starter Kit
3. Test Code
We simply stimulate the traffic lights: green LED lights up for 5s, yellow LED blinks for 3 times, and red LED lights
up for 5s. And we set this to loop. The blinking of yellow LED can utilize for()statement we have mentioned in project
3. Thus, we only need to set the lighting time to complete a traffic light.
/*
keyestudio ESP32 Inventor Learning Kit
Project 4 Traffic Light
http://www.keyestudio.com
*/
int greenPin = 27; //Green LED connects to IO27
int yellowPin = 26; //Yellow LED connects to IO26
int redPin = 25; //Red LED connects to IO25
void setup() {
//Set all LED interfaces to output mode
pinMode(greenPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(redPin, OUTPUT);
}
void loop() {
digitalWrite(greenPin, HIGH); //Light green LED up
delay(5000); //Delay 5s
digitalWrite(greenPin, LOW); //Turn green LED off
for (int i = 1; i <= 3; i++) { //Execute for 3 times
digitalWrite(yellowPin, HIGH); //Light yellow LED up
delay(500); //Delay 0.5s
digitalWrite(yellowPin, LOW); // Turn yellow LED off
delay(500); //Delay 0.5s
}
digitalWrite(redPin, HIGH); //Light red LED up
delay(5000); //Delay 5s
digitalWrite(redPin, LOW); //Turn red LED off
}
4. Test Result
After uploading the code, green LED will light up for 5s, yellow LED will blink for 3 times, and red LED will light up
for 5s, in circulation.
8.5.6 Project 5Rainbow Ambient Light
1. Description
Arduino 2812RGB LED is a programable colorful dreamy light, whose color, brightness and rhythm are adjustable.
This rainbow ambient light can be used as a dynamic decoration at will. Or you may control it to “dance with music”.
Importantly, it can be improved as an alarm. Its built-in sensor detects the ambient surroundings to warn users by
changing its color, brightness and rhythm.
54 Chapter 8. Arduino Tutorial