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 RajTOP 100Posted Aug 28, 2016, 12:42 AM EDT
Nice article
Kumaresh RajalingamPosted Feb 12, 2016, 10:12 PM EST
Thank you santhakumar sir
Santhakumar MunuswamyPosted Feb 12, 2016, 9:59 PM EST
Thanks for nice share
Kumaresh RajalingamPosted Feb 12, 2016, 7:31 PM EST
Thanks Pramod Sir
Kumaresh RajalingamPosted Feb 12, 2016, 7:31 PM EST
Thanks gowtham Sir
Kumaresh RajalingamPosted Feb 12, 2016, 7:31 PM EST
Thanks Raja sir
Pramod ThakurPosted Feb 12, 2016, 7:29 PM EST
Nice..
Gowtham KTOP 100Posted Feb 12, 2016, 5:57 AM EST
Good One
Raja TPosted Feb 12, 2016, 12:36 AM EST
Nice, Thanks for sharing
Kumaresh RajalingamPosted Feb 11, 2016, 9:09 PM EST
Thank you sibeesh bro
Kumaresh RajalingamPosted Feb 11, 2016, 9:08 PM EST
Thank you Amit sir
Kumaresh RajalingamPosted Feb 11, 2016, 9:08 PM EST
Thank you shubham sir
Sibeesh VenuTOP 100Posted Feb 11, 2016, 7:35 PM EST
Nice Share
Amit Kumar SinghPosted Feb 11, 2016, 7:32 PM EST
Nice one
Shubham KumarPosted Feb 11, 2016, 7:31 PM EST
nice one +1
Kumaresh RajalingamPosted Feb 11, 2016, 3:35 PM EST
Thanks Jaco
Kumaresh RajalingamPosted Feb 11, 2016, 3:35 PM EST
Thanks Kartiga
Sr KarthigaPosted Feb 11, 2016, 2:26 PM EST
Good one sir
Jaco ZwartsPosted Feb 11, 2016, 7:11 AM EST
Nice share