Introduction
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
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
- }

Read more articles on Arduino:

Delpin Susai RajPosted Aug 28, 2016, 4:42 AM
Nice article
Kumaresh RajalingamPosted Feb 13, 2016, 3:12 AM
Thank you santhakumar sir
Santhakumar MunuswamyPosted Feb 13, 2016, 2:59 AM
Thanks for nice share
Kumaresh RajalingamPosted Feb 13, 2016, 12:31 AM
Thanks Pramod Sir
Kumaresh RajalingamPosted Feb 13, 2016, 12:31 AM
Thanks gowtham Sir
Kumaresh RajalingamPosted Feb 13, 2016, 12:31 AM
Thanks Raja sir
Pramod ThakurPosted Feb 13, 2016, 12:29 AM
Nice..
Gowtham KPosted Feb 12, 2016, 10:57 AM
Good One
Raja TPosted Feb 12, 2016, 5:36 AM
Nice, Thanks for sharing
Kumaresh RajalingamPosted Feb 12, 2016, 2:09 AM
Thank you sibeesh bro
Kumaresh RajalingamPosted Feb 12, 2016, 2:08 AM
Thank you Amit sir
Kumaresh RajalingamPosted Feb 12, 2016, 2:08 AM
Thank you shubham sir
Sibeesh VenuPosted Feb 12, 2016, 12:35 AM
Nice Share
Amit Kumar SinghPosted Feb 12, 2016, 12:32 AM
Nice one
Shubham KumarPosted Feb 12, 2016, 12:31 AM
nice one +1
Kumaresh RajalingamPosted Feb 11, 2016, 8:35 PM
Thanks Jaco
Kumaresh RajalingamPosted Feb 11, 2016, 8:35 PM
Thanks Kartiga
Sr KarthigaPosted Feb 11, 2016, 7:26 PM
Good one sir
Jaco ZwartsPosted Feb 11, 2016, 12:11 PM
Nice share