A traffic light simulation

Let me warn non-UK readers at the outset: this project simulates traffic lights as used in the UK. I believe in the USA and other countries The projectthe light sequence is somewhat different. The photographs on the right shows the PCB finished circuit board.
The two LEDs in the middle represent pedestrian crossing lights. The three LEDs on the right are connected to ports 1, 2 and 3, the middle two LEDs to ports 4 and 5 and the left LEDs to ports 6, 7 and 8.

The circuit board and finished circuit diagram are below. Everything is fairly simple and straightforward.
I use a 9V supply 470R resistors, but this value is not critical. It depends on the supply Voltage you use, the type of LED and how bright you want the things to shine.

The pcb layout The traffic lights diagram
As usual I supply an archive of the BBC BASIC for Windows file and a ready to run .exe file.

K8055_traffic_light_1_3.zip


There is also a YouTube video


The archive for the K8061 is below:


K8061_traffic_lights.zip
The program

All the work is done by two routines, which set or clear a single port.  SYS K8055_SetDigitalChannel%,1  will turn port 1 on, and
SYS K8055_ClearDigitalChannel%,0 will turn the same port off again. To make things easier, please study the timing diagram below, which shows how each LED changes through a full circle.

The traffic lights timing diagram

I have not included the two pedestrian lights in the program - this is so that the more adventurous readers can have a go at it themselves. You could even use two of the input ports as request buttons, just like the real thing.

As usual, if there are any questions or if I can help, just drop me an e-mail.

   10 REM Traffic light simulation
20 REM Version 1.3
30 REM Jochen Lueg
40 REM http://roevalley.com
50 REM Limavady, April 2012
60
70
80 PROCK8055_init
90 ON ERROR PROCerror
100 ON CLOSE PROCclose
110 T%=150
120 SYS K8055_OpenDevice%, 0
130 SYS K8055_ClearAllDigital%
140 PROCwindow(600,800,"Traffic light simulation")
150 OFF
160 PROCassign
170 PRINT "Simulation running....."
180 PRINT
190 PRINT "Press S to stop"
200 REPEAT
210 PRINT
220 PRINT
230 SYS K8055_SetDigitalChannel%,Green1% : REM Green 1
240 PRINT "Green 1"
250 SYS K8055_ClearDigitalChannel%,Amber1%
260 SYS K8055_ClearDigitalChannel%,Red1%
270 SYS K8055_ClearDigitalChannel%,Green2%
280 SYS K8055_ClearDigitalChannel%,Amber2%
290 SYS K8055_SetDigitalChannel%,Red2% : REM Red 2
300 PRINT "Red 2"
310 SYS K8055_SetDigitalChannel%,PedRed%
320 IF INKEY(-82) GOTO 1010
330 TIME=0:REPEAT UNTIL TIME >T%*4
340
350 SYS K8055_ClearDigitalChannel%,Green1%
360 SYS K8055_SetDigitalChannel%,Amber1% : REM Amber 1
370 PRINT "Amber 1"
380 SYS K8055_ClearDigitalChannel%,Red1%
390 SYS K8055_ClearDigitalChannel%,Green2%
400 SYS K8055_ClearDigitalChannel%,Amber2%
410 SYS K8055_SetDigitalChannel%,Red2% : REM Red 2
420 PRINT "Red 2"
430 SYS K8055_SetDigitalChannel%,PedRed%
440
450 IF INKEY(-82) GOTO 1010
460 TIME=0:REPEAT UNTIL TIME >T%
470
480 SYS K8055_ClearDigitalChannel%,Green1%
490 SYS K8055_ClearDigitalChannel%,Amber1%
500 SYS K8055_SetDigitalChannel%,Red1% : REM Red1
510 PRINT "Red 1"
520 SYS K8055_ClearDigitalChannel%,Green2%
530 SYS K8055_SetDigitalChannel%,Amber2% : REM Amber 2
540 PRINT "Amber 2"
550 SYS K8055_SetDigitalChannel%,Red2% : REM Red 2
560 PRINT "Red 2"
570 SYS K8055_SetDigitalChannel%,PedRed%
580
590 IF INKEY(-82) GOTO 1010
600 TIME=0:REPEAT UNTIL TIME >T%
610
620
630 SYS K8055_ClearDigitalChannel%,Green1%
640 SYS K8055_ClearDigitalChannel%,Amber1%
650 SYS K8055_SetDigitalChannel%,Red1% : REM Red 1
660 PRINT "Red 1"
670 SYS K8055_SetDigitalChannel%,Green2% : REM Green 2
680 PRINT "Green 2"
690 SYS K8055_ClearDigitalChannel%,Amber2%
700 SYS K8055_ClearDigitalChannel%,Red2%
710 SYS K8055_SetDigitalChannel%,PedRed%
720
730 IF INKEY(-82) GOTO 1010
740 TIME=0:REPEAT UNTIL TIME >T% *4
750
760 SYS K8055_ClearDigitalChannel%,Green1%
770 SYS K8055_ClearDigitalChannel%,Amber1%
780 SYS K8055_SetDigitalChannel%,Red1% : REM Red 1
790 PRINT "Red 1"
800 SYS K8055_ClearDigitalChannel%,Green2%
810 SYS K8055_SetDigitalChannel%,Amber2% : REM Amber 2
820 PRINT "Amber 2"
830 SYS K8055_ClearDigitalChannel%,Red2%
840 SYS K8055_SetDigitalChannel%,PedRed%
850
860 IF INKEY(-82) GOTO 1010
870 TIME=0:REPEAT UNTIL TIME >T%
880
890 SYS K8055_ClearDigitalChannel%,Green1%
900 SYS K8055_SetDigitalChannel%,Amber1% : REM Amber 1
910 PRINT "Amber 1"
920 SYS K8055_SetDigitalChannel%,Red1% : REM Red 1
930 PRINT "Red 1"
940 SYS K8055_ClearDigitalChannel%,Green2%
950 SYS K8055_ClearDigitalChannel%,Amber2%
960 SYS K8055_SetDigitalChannel%,Red2%
970 SYS K8055_SetDigitalChannel%,PedRed% : REM Red 2
980 PRINT "Red 2"
990 IF INKEY(-82) GOTO 1010
1000 TIME=0:REPEAT UNTIL TIME >T%
1010 UNTIL INKEY(-82)
1020 PROCclose
1030 END
1040
1050
1060 DEFPROCclose
1070 SYS K8055_ClearAllDigital%
1080 SYS K8055_CloseDevice%
1090 REPEAT UNTIL INKEY(0)=-1
1100 SYS "FreeLibrary",K8055_Board%
1110 QUIT
1120 END
1130
1140
1150 DEFPROCassign
1160 Green1%=1:Amber1%=2:Red1%=3
1170 PedGreen%=4:PedRed%=5
1180 Green2%=6:Amber2%=7:Red2%=8
1190 ENDPROC
1200
1210
1220 DEFPROCwindow(WindowWidth%,WindowHeight%,W$)
1230 MODE 30
1240 SYS "SetWindowPos",@hwnd%,0,0,0,WindowWidth%,WindowHeight%,6
1250 SYS "SetWindowText",@hwnd%,W$
1260 VDU 26
1270 COLOUR 15
1280 *FONT Lucida Console,14,2
1290 ENDPROC
1300
1310
1320 DEFPROCerror
1330 PRINT REPORT$;" at line ";ERL
1340 SYS K8055_ClearAllDigital%
1350 SYS K8055_ClearAllAnalog%
1360 SYS K8055_CloseDevice%
1370 SYS "FreeLibrary",K8055_Board%
1380 END
1390 ENDPROC
1400
1410
1420 DEFPROCK8055_init
1430 SYS"LoadLibrary","K8055D.dll" TO K8055_Board%
1440 SYS"GetProcAddress",K8055_Board%,"OpenDevice" TO K8055_OpenDevice%
1450 SYS"GetProcAddress",K8055_Board%,"CloseDevice" TO K8055_CloseDevice%
1460 SYS"GetProcAddress",K8055_Board%,"ReadAnalogChannel" TO K8055_ReadAnalogChannel%
1470 SYS"GetProcAddress",K8055_Board%,"ReadAllAnalog" TO K8055_ReadAllAnalog%
1480 SYS"GetProcAddress",K8055_Board%,"OutputAnalogChannel" TO K8055_OutputAnalogChannel%
1490 SYS"GetProcAddress",K8055_Board%,"OutputAllAnalog" TO K8055_OutputAllAnalog%
1500 SYS"GetProcAddress",K8055_Board%,"ClearAnalogChannel" TO K8055_ClearAnalogChannel%
1510 SYS"GetProcAddress",K8055_Board%,"ClearAllAnalog" TO K8055_ClearAllAnalog%
1520 SYS"GetProcAddress",K8055_Board%,"SetAnalogChannel" TO K8055_SetAnalogChannel%
1530 SYS"GetProcAddress",K8055_Board%,"SetAllAnalog" TO K8055_SetAllAnalog%
1540 SYS"GetProcAddress",K8055_Board%,"WriteAllDigital" TO K8055_WriteAllDigital%
1550 SYS"GetProcAddress",K8055_Board%,"ClearDigitalChannel" TO K8055_ClearDigitalChannel%
1560 SYS"GetProcAddress",K8055_Board%,"ClearAllDigital" TO K8055_ClearAllDigital%
1570 SYS"GetProcAddress",K8055_Board%,"SetDigitalChannel" TO K8055_SetDigitalChannel%
1580 SYS"GetProcAddress",K8055_Board%,"SetAllDigital" TO K8055_SetAllDigital%
1590 SYS"GetProcAddress",K8055_Board%,"ReadDigitalChannel" TO K8055_ReadDigitalChannel%
1600 SYS"GetProcAddress",K8055_Board%,"ReadAllDigital" TO K8055_ReadAllDigital%
1610 SYS"GetProcAddress",K8055_Board%,"ResetCounter" TO K8055_ResetCounter%
1620 SYS"GetProcAddress",K8055_Board%,"ReadCounter" TO K8055_ReadCounter%
1630 SYS"GetProcAddress",K8055_Board%,"SedtCounterDebouceTime" TO K8055_SetCounterDebounceTime%
1640 ENDPROC





Return to the interfacing indexBack to the Limavady home page

Tudor with sign