TUTORIAL CROSSING OF TRAFFIC LIGHTS WITH PIC 16F84A

Traffic lights with pic
In the next tutorial, we will teach you how to program a junction with 2 traffic lights that control the flow of cars, The two traffic lights are exchanging states to grant green light to each other and their intermediate states of amber light and red light.

Description
The traffic light goes through a series of states marked by different lights, green, amber, and red, each representing a different action for the person driving. Green means that the vehicle can move forward, and when the time is running out it starts to flash to warn drivers, then it turns off to light up the amber light that means caution and the driver must slow down and stop, then stop. Turn on the red light that indicates total stop, and thus gives way to the green light of another traffic light, and continue the cycle.


Material’s list
– Pic16f84a
– 2 Ceramic 15pf Capacitors
– 1 4Mhz quartz crystal
– 6 Resistances of 330
– 1 Resistance of 100
– 1 10k resistor
– 2 Push button
– 2 red, amber, green LEDs
Development
In the following image we can see the simulated connection diagram in proteus
traffic light with pic
Now it only remains to see the program, I leave it commented for your understanding.
</ br>

</ br>
</ br> #include // we include the pic to use
#fuses xt, nowdt, noprotect, // fuses
#use delay (clock = 4000000) // 4Mhz criztal
#byte portb = 0x06 // We declare that we will use port B
#byte trisb = 0x086int a, memory = 0; // We declare two variables void main () // main function
{
SET_TRIS_A (0xFF); // We declare port A as inputs
set_tris_b (0x00); // We declare port B as outputs
portb = 0x00; // We initialize port B with the outputs at 0
while (true) // Here the program remains cycled
delay_ms (1); {if (input (PIN_A0) == 1) // If pin A0 is = 1 we save the state with memory
{
memory = memory + 1; // We use a memory to know that the button was pressed
}
if (memory == 1) // IF memory is activated = 1 enter if
{
do
{
output_high (PIN_B5); // On green 1
output_high (PIN_B0); // On red 2
delay_ms (500); // green and red cycle
output_low (PIN_B5); // Off green 1
for (a = 1; a <= 4; a ++) // blink green 1

{
delay_ms (100);
output_high (PIN_B5); // blink green 1
delay_ms (100);
output_low (PIN_B5); // blink green 1
}

output_high (PIN_B4); // lit yellow 1
delay_ms (500);
output_low (PIN_B4); // off yellow 1
output_high (PIN_B3); // on red 1
output_high (PIN_B2); // on green 2
output_low (PIN_B0); // off red 2
delay_ms (1000);
output_low (PIN_B2); // green off 2

{
for (a = 1; a <= 4; a ++) // blink green 2

{
delay_ms (100);
output_high (PIN_B2); // green 2
delay_ms (100);
output_low (PIN_B2); // green 2
}

output_high (PIN_B1); // on yellow
delay_ms (500);
output_low (PIN_B1); // off yellow 2
output_low (PIN_B3); // off red 1
}
}
while (true);