You are on page 1of 5

Bautista, Kent Tyrone D.

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.

1. Write your pseudo code here

void loop() {
changeLights();
delay(5000);
2. Write your codes here:

int red1 = 10;


int yellow1 = 9;
int green1 = 8;

int red2 = 13;


int yellow2 = 12;
int green2 = 11;

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);
}

3. Describe the output created

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 are the type of pins you used?


Digital pins (Input/output Pins) -for pinMode function configures the Arduino to use a given pin as
an output. You have to do this for your LEDs to work at all. To set output pins to HIGH (for on), or
LOW (for off).
Power
Input voltage to Arduino when using an external power source. Also for GND: ground pins.

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.

You might also like