COM2 - Lab Activity 2
COM2 - Lab Activity 2
BSME – 3GN
Arduino
Activity 2 – Coding Two Way Traffic Light. Traffic lights are used to control traffic and
improve safety of the motorist to avoiding collisions and unnecessary events in traffic. These
lights change from GREEN to YELLOW to RED. Having a transition for GREEN to YELLOW
is 5 seconds. YELLOW to RED is 3 seconds and RED to GREEN is 5 seconds. The adjacent
way of the traffic light has the same transition time of LEDs.
void loop() {
changeLights();
delay(5000);
2. Write your codes here:
int button = 6;
void setup() {
pinMode(red1, OUTPUT);
pinMode(yellow1, OUTPUT);
pinMode(green1, OUTPUT);
pinMode(red2, OUTPUT);
pinMode(yellow2, OUTPUT);
pinMode(green2, OUTPUT);
void loop() {
changeLights();
delay(5000);
void changeLights() {
//
digitalWrite(red1, LOW);
digitalWrite(red2, LOW);
digitalWrite(green2, LOW);
digitalWrite(green1, LOW);
digitalWrite(yellow1, HIGH);
digitalWrite(yellow2,LOW);
delay(3000);
//
digitalWrite(yellow1, LOW);
digitalWrite(red1, HIGH);
digitalWrite(yellow2, LOW);
digitalWrite(red2, LOW);
digitalWrite(green2, HIGH);
delay(5000);
//
digitalWrite(red1, LOW);
digitalWrite(red2, LOW);
digitalWrite(green2, LOW);
digitalWrite(green1, LOW);
digitalWrite(yellow1, LOW);
digitalWrite(yellow2, HIGH);
delay(3000);
//
digitalWrite(green1, HIGH);
digitalWrite(yellow1, LOW);
digitalWrite(red1, LOW);
digitalWrite(yellow2, LOW);
digitalWrite(red2, HIGH);
delay(5000);
}
It’s fine because it works the same as the given instruction, like at first both yellow
turns on means get ready then the green light at the right and the red light at the left
will turn on next after the yellow is off meaning that the other way is available and at
go while the other is at stop so it won’t bother the other lane at go. And so it
continues as it is and serves as a traffic light without a doubt.
CONCLUSION
What function and structure of your program is essential in resolving the given problem?
How?
The delay because it triggers the main function of the traffic light which is the change of light for
it gives time to each light consequently so it won’t make any necessary “go” signals that may
result to a collision in a traffic light.