HPK taruh disini
The
traffic light is a tool for user signaling traffic, in order to control the
traffic that is attached at a crossroads, a pedestrian crossing, and the flow
of other traffic. Lights that indicate when the vehicle must go and stop
alternately from different directions. Traffic control at the intersection of
the road is intended to regulate the movement of vehicles in each group
movement of the vehicle in order to move alternately so as not to interfere
with each other between the existing flow. To tell for newbie how to make control of traffic light with arduino. with a simple
module.
The Arduino Uno is a microcontroller board based on the ATmega328. It has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz ceramic resonator, a USB connection, a power jack, an ICSP header, and a reset button. It contains everything needed to support the microcontroller; simply connect it to a computer with a USB cable or power it with a AC-to-DC adapter or battery to get started.
Component:
- · 1 Arduino Uno
- · 3 resistor 100ohm
- · 3 led (red,yellow,green)
To make a simple simulation traffic light connect resistor to
LED (anoda/+) and LED(katoda/-) to ground and another feet resistor to ardunio.
Coding
We’ll
start by defining variables so that we can address the lights by name rather
than a number. Start a new Arduino project, and begin with these lines:
int red =
13;
int yellow
= 12;
int green
= 11;
Next,
let’s add the setup function, where’ll we define the red, yellow and green LEDs
to be output mode. Since we’ve created variables to represent the pin numbers,
we can now refer to the pins by names instead.
void
setup(){
pinMode(red,OUTPUT);
pinMode(yellow,OUTPUT);
pinMode(green,OUTPUT);
}
That
was easy. Now for the difficult part – the actual logic of a traffic light. I’m
going to create a separate function for changing the lights, and you’ll see why
later.
When you first begin programming, the
code itself is very rudimentary – it’s figuring out the minute logic details
that presents the biggest problem. The key to being a good programmer is to be
able to look at any process, and break it down into its fundamental steps.
void
loop(){
changeLights();
delay(14000);
}
void
changeLights(){
// green
off, yellow for 3 seconds
digitalWrite(green,HIGH);
digitalWrite(yellow,LOW);
delay(3000);
// turn
off yellow, then turn red on for 5 seconds
digitalWrite(yellow,LOW);
digitalWrite(red,HIGH);
delay(5000);
// red and
yellow on for 2 seconds (red is already on though)
digitalWrite(yellow,HIGH);
delay(2000);
// turn
off red and yellow, then turn on green
digitalWrite(yellow,LOW);
digitalWrite(red,LOW);
digitalWrite(green,HIGH);
}