In this article, we are going to build little components with LED's called a Smart Traffic Light System
Required Components:
- Arduino Uno
- Led-3
- Bread Board
- Push Button
- Some Wires
Connections:
- Connect LED anode pin to the Arduino digital pin 10,11,12.
- And Cathode pin to the Gnd.
- Push button to the digital pin 7.
Program
- int one = 10;
- int two = 11;
- int three = 12;
- int btn = 7;
- int state = 0;
- void setup()
- {
- pinMode(one, OUTPUT);
- pinMode(two, OUTPUT);
- pinMode(three, OUTPUT);
- }
- void loop()
- {
- if (digitalRead(btn))
- {
- if (state == 0)
- {
- setLights(HIGH, LOW, LOW);
- state = 1;
- } else if (state == 1)
- {
- setLights(HIGH, HIGH, LOW);
- state = 2;
- } else if (state == 2)
- {
- setLights(LOW, LOW, HIGH);
- state = 3;
- } else if (state == 3)
- {
- setLights(LOW, HIGH, LOW);
- state = 0;
- }
- delay(1000);
- }
- }
- void Traffic(int red, int yellow, int green)
- {
- digitalWrite(one, red);
- digitalWrite(two, yellow);
- digitalWrite(three, green);
- }
Explanation:
Declare the variables as one, two, three btn in the integer data type and assign the digital pins to that variables and set the state as "0." In the setup() function, we want to set the pinMode for our LED's and in the loop() function, we want to give the condition for our LEDs, such as, first we want to read the button value from the analog pin. For that, we use the built-in function called digitalRead. It reads the analog value from the Arduino board
- if (state==0) set the led1 as HIGH
- if (state==1) led1,led2 are HIGH
- if (state==2) led3 is HIGH
- if(state==2)led2 is HIGH
And set the delay time as 1000 s for the 3 led's and create the function called Traffic with parameters such as
int red, int yellow, int green.
DigitalWrite:
Writes the HIGH or LOW value to the digital pins. Inside the Traffic function, we want to set the digitalWrite function like this,
- void Traffic(int red, int yellow, int green)
- {
- digitalWrite(one, red); //one
- digitalWrite(two, yellow); //two
- digitalWrite(three, green); //three
- }
Output
Read more articles on Arduino: