Traffic Light Code
Viper1
Posts: 6
Hello all, This is my first post and I am a novice to Parallax microcontrollers. I saw this traffic light on a youtube video and having some interest in these types of projects, I just had to try it.
The code below in it's original unedited state (except for ports) but there seems to be some problems with this or I am missing something.
I have studied the code and understand most of it. The code for the most part is running as it should. The Ping sensor is active and displays distance perhaps in mm and does change the switch dependent on distance but I am having difficulty with the reset portion of this code as it does shut off all lights once the LIGHT_ON_TIME is reached but does not seem to reset so the light are active again. I have done some experimenting with the code and changing line 53 from 0 to 1 "timeLightHasBeenOn = timeLightHasBeenOn + 1" does produce a timing output . But my main obstacle is the reset portion of this code I am sure this timer is directly related to my issue as is line 103 "timeLightHasBeenOn = 0".
Any help with this would be greatly appreciated. TY
• I believe the system is suppose to reset after a certain X time but does not
• Would like distance to read in inches and cm and the timer to read actual second.
The code below in it's original unedited state (except for ports) but there seems to be some problems with this or I am missing something.
I have studied the code and understand most of it. The code for the most part is running as it should. The Ping sensor is active and displays distance perhaps in mm and does change the switch dependent on distance but I am having difficulty with the reset portion of this code as it does shut off all lights once the LIGHT_ON_TIME is reached but does not seem to reset so the light are active again. I have done some experimenting with the code and changing line 53 from 0 to 1 "timeLightHasBeenOn = timeLightHasBeenOn + 1" does produce a timing output . But my main obstacle is the reset portion of this code I am sure this timer is directly related to my issue as is line 103 "timeLightHasBeenOn = 0".
Any help with this would be greatly appreciated. TY
• I believe the system is suppose to reset after a certain X time but does not
• Would like distance to read in inches and cm and the timer to read actual second.
1. 'Distance constants 2. GREEN_DISTANCE CON 11000 3. YELLOW_DISTANCE CON 7500 4. RED_DISTANCE CON 4600 5. CmConstant CON 2260 6. InConstant CON 890 7. 'Time constants 8. LIGHT_ON_TIME CON 1000 9. 'Light States 10. LIGHT_OFF CON 0 11. RED_LIGHT CON 1 12. YELLOW_LIGHT CON 2 13. GREEN_LIGHT CON 3 14. 'IO PINS 15. GREEN_IO_PORT CON 13 16. YELLOW_IO_PORT CON 14 17. RED_IO_PORT CON 15 18. PING1_IO_PORT CON 5 19. 'Variables 20. timeLightHasBeenOn VAR Word 21. distanceSensor1 VAR Word 22. lightState VAR NIB 23. distanceFiltered VAR Word 24. distance VAR Word 25. cmDistance VAR Word 26. inDistance VAR Word 27. time VAR Word 28. 'Initialize variables 29. lightState = LIGHT_OFF 30. timeLightHasBeenOn = 0 31. distanceFiltered = 0 32. 'Turn off lights 33. LOW GREEN_IO_PORT 34. LOW YELLOW_IO_PORT 35. LOW RED_IO_PORT 36. 'Loop forever 37. DO 38. 'Get the distance from sensor #1 39. PULSOUT PING1_IO_PORT, 5 40. PULSIN PING1_IO_PORT, 5, distanceSensor1 41. 'cmDistance = cmConstant ** time 42. 'inDistance = inConstant ** time 43. 'DEBUG HOME, DEC5 cmDistance, " cm" 44. 'DEBUG CR, DEC5 inDistance, " in" 45. distanceFiltered = distanceFiltered - (distanceFiltered/5-distanceSensor1) 46. distance = distanceFiltered / 5 47. DEBUG HOME, "distanceRaw = ", DEC5 distanceSensor1, "; distanceFilt = ", DEC5 distance, "; timer = ", DEC5 timeLightHasBeenOn, CR 48. 'Check for car present 49. IF (distance < GREEN_DISTANCE) THEN 50. 'If car has just arrived, then turn on the light!!! 51. IF (timeLightHasBeenOn < LIGHT_ON_TIME) THEN 52. 'Increment timer 53. timeLightHasBeenOn = timeLightHasBeenOn + 0 54. 'Determine which light: green, yellow, or red 55. SELECT distance 56. 'Green light - bumper not detected yet 57. CASE > YELLOW_DISTANCE 58. IF lightState <> GREEN_LIGHT THEN 59. lightState = GREEN_LIGHT 60. 'Turn green on 61. HIGH GREEN_IO_PORT 62. LOW YELLOW_IO_PORT 63. LOW RED_IO_PORT 64. ENDIF 65. 'Yellow light - bumper detected but not at the top height yet 66. CASE > RED_DISTANCE 67. IF lightState <> YELLOW_LIGHT THEN 68. lightState = YELLOW_LIGHT 69. 'Turn yellow on 70. LOW GREEN_IO_PORT 71. HIGH YELLOW_IO_PORT 72. LOW RED_IO_PORT 73. ENDIF 74. 'Red light - at the top height 75. CASE ELSE 76. IF lightState <> RED_LIGHT THEN 77. lightState = RED_LIGHT 78. 'Turn red on 79. LOW GREEN_IO_PORT 80. LOW YELLOW_IO_PORT 81. HIGH RED_IO_PORT 82. ENDIF 83. ENDSELECT 84. 'Car has been here for a while so turn off the light 85. ELSE 86. IF lightState <> LIGHT_OFF THEN 87. lightState = LIGHT_OFF 88. LOW GREEN_IO_PORT 89. LOW YELLOW_IO_PORT 90. LOW RED_IO_PORT 91. ENDIF 92. ENDIF 93. 'No car is present 94. ELSE 95. 'Make sure lights are turned off 96. IF lightState <> LIGHT_OFF THEN 97. lightState = LIGHT_OFF 98. LOW GREEN_IO_PORT 99. LOW YELLOW_IO_PORT 100. LOW RED_IO_PORT 101. ENDIF 102. 'Reset the timer 103. timeLightHasBeenOn = 0 104. ENDIF 105. PAUSE 100 106. LOOP 107. END
Comments
Which Parallax microcontroller are you using?
Please post your code using the Code directive "C" in the menu bar.
As Dave asked above, it looks like Basic Stamp code- so I'll move this to the Basic Stamp forum for you, as you should catch the eye of more Stamp experts that way.
I've added the code tags for you too, just to make the code easier to read.
When you post code next time, then click that C button above the posting text box, and you'll get code tags appear to preserve the code formatting. As always, lot's of little things to help and learn along the way!
Controller board is a Board of Education USB 28803 using 3 Relays 27715 and a Ping sensor
Could you post a link to the YouTube video?
Just copy and paste the URL without any special tags. The forum software automatically embed the video.
The actual code required for the lights was very simple and involves having a small movement routine that tracked the few readings and compared them looking for a sustained change above a small threshold.
If there is movement:
Reload the countdown timer
If it is very close - turn on red light
If it is close but needs to come closer - turn on yellow light
If it is within parking distance (less than "too far away") - turn on green light
If it is too far away - turn off lights (optional)
If there is no movement and timer <> 0
Countdown timer
If timer = 0 then turn off the lights
Did you finish your project and how does it work