📜 ⬆️ ⬇️

Traffic lights in the garage. Present

It was a fine and nasty rain. The mood was vile.
He lay on the side of the road and quietly rusted. Nearby on the post a young fellow gleamed with lights.
I stopped and put it in the trunk.
I'm not alone anymore.
And he is not alone.

Brought to the country, dismantled, laundered from the dirt and painted from the spray.

Application was found immediately. The new car turned out to be wider than the previous one and check-in to the garage became more problematic. Any "bells-on-strings" - this is not for us. So - stationary parking sensors.

Equipment

1. Widespread ultrasonic sensors HC-SR04. 2 pieces, one for each wall. The Internet is full of information about them. Description below.
')
Ultrasonic Ranging Module HC - SR04

Product features:

Can be up to 3mm. The modules include ultrasonic transmitters, receiver and control circuit. The basic principle of work:
Using IO trigger for at least 10us high level signal.
If there is a pulse signal back.
If you’re trying to get back to your car, you’ll find out how to get back.
Test distance = (high level time velocity of sound (340M / S) / 2

Wire connecting direct as following:

5V Supply
Trigger Pulse Input
Echo pulse output
0V Ground
Electric parameter

Working Voltage DC 5 V
Working Current 15mA
Working Frequency 40Hz
Max Range 4m
Min Range 2cm
Measuring Angle 15 degree
Trigger Input Signal 10uS TTL pulse
Echo Output Signal
Dimension 45 * 20 * 15mm

image

Timing diagram

The Timing diagram is shown below. If you want to take a bit of energy, you’ll be able to get back over the cycle of 10 kHz, and then you will be able to bang the pulse. This is the distance between the counting and the echo signal. Formula: uS / 58 = centimeters or uS / 148 = inch; or: the range = high-level time * velocity (340M / S) / 2; In order to prevent the echo signal.

image

Fixed on the side walls at the entrance to the garage using strips of galvanized tin. The mount should allow you to adjust the sensors "in place" depending on the size of the car (height, width of the hood, the shape of the wings ...).

image

2. The controller - PIC16f84a. Why this:
a) I have them;
b) it has 2 I / O ports, a total of 13 bits, which is exactly what is needed. 3 pcs. - traffic light control (Red - Yellow - Green), 6pcs. - left and right LEDs (K-ZHZ), 2 pieces. - pulse location (Trig) on ​​the left and right sensors, 2 pcs. - reflected pulse (Echo) from the left and right sensors.

3. Two three-color LEDs, one for each wall. LEDs Red - Yellow - Green not found. Maybe it does not happen, and maybe it was looking bad. Therefore, the role of yellow in LEDs plays blue. For complete identity, of course, it was possible to use separate LEDs. And there would be no problems with yellow. But I considered such a compromise admissible.

image

4. Control units for traffic lights. 3 pieces, one for each lamp.
Ideal control unit on-off TV.

image

It consists of a low-power power transformer (220 to 7 volts), a diode rectifier, a stabilizer on 1425 (or equivalent), a transistor switch and a relay. When a logical “1” is fed to the input, the key opens, the relay is activated and 220v is output. In addition, since the KREN provides stabilized 5v, it is reasonable to power the microcontroller from one of these units. They are full on any radio market. If someone is annoyed by clicking the relay, it is easy to make a circuit on the thyristors.

5. Quartz resonator at 4 MHz.
This is a very convenient frequency for those microcontrollers that I have. It is easy to count the time parameters. The period of clock pulses = 250ns, the machine cycle consists of four cycles, therefore, equal to 1 μs. Commands in RISC controllers are executed in 1 or 2 machine cycles, so the calculation of the time that passes “from how ... to” is no problem. You can use another within the allowed frequencies, but you will have to recalculate the timeouts. This is quite a small problem.

6. Several resistors and capacitors recommended by the standard PIC16f84a switching circuit and two current-limiting resistors for LEDs. Datashit watch, for example, here: link .

General view of the assembled device:

image
image

Board with a microcontroller:

image

Schematic diagram

image

Algorithm

Simple, almost linear. On power-up test equipment is carried out. Ostensibly test, more for beauty. With an interval of 1s red-yellow-green switches, then with the same interval all the lights turn on three times. After that - the transition to an infinite loop of polling the left and right channels. When each channel passes, depending on the result, the corresponding bit (flag) PORTB is set and one of the LEDs of this channel turns on. At the end of the full cycle, an analysis is made of the state of the Red-Yellow-Green flags of both channels and, based on this, the corresponding traffic light is turned on. The truth table looks like this:

Any channel Red . Traffic light - Red .
Both channels are Green . Traffic light - Green .
All other cases. Traffic light - Yellow .

In a few words, the core of the algorithm "step by step":

1. We form the impulse Trig;
2. We expect the arrival of the response pulse Echo;
3. We start timeout 1ms;
4. After the timeout expires, we check if there is still “log.1” at the input of Echo;
5. If not, then we are on a dangerous approach to the sensor of the corresponding channel. Turn on the red flag of this channel and switch to another channel;
6. If Echo is still there, repeat pp.3 and 4;
7. If not, then we are at an average distance from the sensor. Turn on the yellow flag and go to another channel;
8. If Echo is still there, then we are far away. We are waiting for it to end, turn on the green flag and go to another channel.

image

What is the point? We do not need to calculate the exact distance to the object. That is, it is not necessary to divide the pulse width Echo by 58, as indicated in the sensor description. We are not building a rangefinder. We need to know only the position of the object in relation to two identical ranges of distances from the object to the sensor. For this, a timeout of 1ms is used (see section 3), which ultimately results in: 1000µs: 58 = 17cm. So - closer 17cm - red, from 17 to 34cm - yellow, then 34cm - green. Changing the constants in the subprogram “Pause 1ms”, you can vary the distance depending on the size of the car, the width of the gate and driving skills.

Firmware

The firmware listing is shown below. Assembler The constants, except for those in timeouts, are in binary, because the device is bit-oriented. And when debugging or analyzing, you would still have to translate into a binary system. To prevent freezes in the program, Watchdog Timer is used.
Listing
list p=16F84A ; list directive to define processor #include <p16F84A.inc> ; processor specific variable definitions __CONFIG _CP_OFF & _WDT_ON & _PWRTE_ON & _XT_OSC ;********************** Contact Assignment ***************************** ; Contact # Name Function ; 17 RA0 output TrigLeft ; 18 RA1 output TrigRight ; 01 RA2 input EchoLeft ; 02 RA3 input EchoRight ; 03 RA4 output GenRed ; 06 RB0 output RedLeft ; 07 RB1 output YellowLeft ; 08 RB2 output GreenLeft ; 09 RB3 output RedRight ; 10 RB4 output YellowRight ; 11 RB5 output GreenRight ; 12 RB6 output GenYellow ; 13 RB7 output GenGreen ; ;******************* Constants ************************** ; Red 00000001 Left channel ; Yellow 00000010 Left channel ; Green 00000100 Left channel ; ; Red 00001000 Right channel ; Yellow 00010000 Right channel ; Green 00100000 Right channel ;***** VARIABLE DEFINITIONS ***************************** CBLOCK 0x0C Reg_1 ; 0x0C use within Pause subroutine Reg_2 ; 0x0D use within Pause subroutine Reg_3 ; 0x0E use within Pause subroutine ENDC #define bank0 bcf STATUS,RP0 ; set bank0 #define bank1 bsf STATUS,RP0 ; set bank1 #define TrigLeft_1 bsf PORTA,0 ; set Trigleft #define TrigLeft_0 bcf PORTA,0 ; clean Trigleft #define TrigRight_1 bsf PORTA,1 ; set Trigright #define TrigRight_0 bcf PORTA,1 ; clean Trigright #define EchoLeft PORTA,2 #define EchoRight PORTA,3 #define RedLeft b'00000001 #define YellowLeft b'00000010 #define GreenLeft b'00000100 #define RedRight b'00001000 #define YellowRight b'00010000 #define GreenRight b'00100000 ;********************************************************************** ORG 0x000 ; processor reset vector goto main ; go to beginning of program ;---------------------------------------------------------------------- ;------------------ SUBROUTINE Pulse -------- ;delay = 10 machine cycles = 10 µs Pulse movlw .3 movwf Reg_1 wr0 decfsz Reg_1,F goto wr0 return ;------------------ SUBROUTINE Pause 1ms -------- ;delay = 1'000 machine cycles Pause movlw .75 movwf Reg_1 movlw .3 movwf Reg_2 wr1 decfsz Reg_1,F goto wr1 clrwdt decfsz Reg_2,F goto wr1 nop return ;-------------------- SUBROUTINE Pause 1sec --- ;delay = 1'000'000 machine cycles Pause1 movlw .254 movwf Reg_1 movlw .17 movwf Reg_2 movlw .6 movwf Reg_3 wr2 decfsz Reg_1,F goto wr2 clrwdt decfsz Reg_2,F goto wr2 decfsz Reg_3,F goto wr2 nop nop return ;-------------------- SUBROUTINE Pause 50ms -------- ;delay = 50'000 machine cycles Pause2 movlw .216 movwf Reg_1 movlw .65 movwf Reg_2 wr3 decfsz Reg_1,F goto wr3 clrwdt decfsz Reg_2,F goto wr3 return ;--------------------- SUBROUTINE General Red --------------- GenRed bsf PORTA,4 bcf PORTB,6 bcf PORTB,7 return ;--------------------- SUBROUTINE General Green ------------- GenGreen bsf PORTB,7 bcf PORTA,4 bcf PORTB,6 return ;--------------------- SUBROUTINE General Yellow ------------ GenYellow bsf PORTB,6 bcf PORTA,4 bcf PORTB,7 return ;--------------------- SUBROUTINE General All ------------ GenAll bsf PORTA,4 bsf PORTB,6 bsf PORTB,7 return ;--------------------------------------------------------- main bank1 ; set Bank 1 movlw 0x00 movwf TRISB ; PORTB - out movlw b'00001100 movwf TRISA ; RA2,RA3 - input; RA0,RA1,RA4 - output bank0 ; return to Bank 0 clrf PORTA clrf PORTB ; turn off all lights ;---------------------- TEST ----------------------------- call GenRed call Pause1 call GenYellow call Pause1 call GenGreen call Pause1 bcf PORTA,4 bcf PORTB,6 bcf PORTB,7 call Pause1 call GenAll call Pause1 bcf PORTA,4 bcf PORTB,6 bcf PORTB,7 call Pause1 call GenAll call Pause1 bcf PORTA,4 bcf PORTB,6 bcf PORTB,7 call Pause1 call GenAll call Pause1 bcf PORTA,4 bcf PORTB,6 bcf PORTB,7 call Pause1 goto SoundRight ;------------------LEFT---------------------- SoundLeft TrigLeft_1 call Pulse TrigLeft_0 btfss EchoLeft goto $-1 call Pause btfss EchoLeft goto SetRedLeft call Pause btfss EchoLeft goto SetYellowLeft goto SetGreenLeft SetRedLeft movlw RedLeft iorwf PORTB,1 bcf PORTB,1 bcf PORTB,2 goto SoundRight SetYellowLeft movlw YellowLeft iorwf PORTB,1 bcf PORTB,0 bcf PORTB,2 goto SoundRight SetGreenLeft movlw GreenLeft iorwf PORTB,1 bcf PORTB,0 bcf PORTB,1 clrwdt btfsc EchoLeft ; wait Echo end goto $-2 goto SoundRight ;----------------RIGHT--------------------- SoundRight call Pause2 TrigRight_1 call Pulse TrigRight_0 btfss EchoRight goto $-1 call Pause btfss EchoRight goto SetRedRight call Pause btfss EchoRight goto SetYellowRight goto SetGreenRight SetRedRight movlw RedRight iorwf PORTB,1 bcf PORTB,4 bcf PORTB,5 goto SetGenLight SetYellowRight movlw YellowRight iorwf PORTB,1 bcf PORTB,3 bcf PORTB,5 goto SetGenLight SetGreenRight movlw GreenRight iorwf PORTB,1 bcf PORTB,3 bcf PORTB,4 clrwdt btfsc EchoRight ; wait Echo end goto $-2 ;------------------------ Set General Lights ---------------- SetGenLight btfss PORTB,0 ; check RedLeft goto CheckRedRight call GenRed goto SoundLeft CheckRedRight btfss PORTB,3 goto CheckGreenLeft call GenRed goto SoundLeft CheckGreenLeft btfss PORTB,2 goto SetGenYellow btfss PORTB,5 goto SetGenYellow call GenGreen goto SoundLeft SetGenYellow call GenYellow goto SoundLeft END 


Video




Thanks and commitment

The author expresses his gratitude to his wife, who during real tests stood fearlessly at the garage door and ensured that the author did not drive either the left or the right wing of the car into the wall at the entrance. If there are fearless followers who want to repeat the project, the author will answer all.

Source: https://habr.com/ru/post/242097/


All Articles