Hey guys! I am having a problem with my uni project at the moment, where I am making a traffic light system (python). Though one very specific part is giving me some strife. When the light of the tunnel is dark enough (exact value irrelevant) 2 flood lights (White LED's) must switch on. I have wired it up the best I can, though the feed tells me that once I cover my LDR, the light level reaches 0, hence the lights turn on, but an instant later the value springs up to 1000 and turns off, resulting in the lights essentially blinking. I am aiming to make it stay on until my finger is removed from the LDR, but it is all too inconsistent. I have tried moving the LDR away from the lights, but I think it must be a part of my code. So if anyone could offer some help, I would be so grateful.
Should put my code in too lol
from pymata4 import pymata4
import time
"""Initialize the board"""
board = pymata4.Pymata4()
"""Pin Definitions"""
trigPin = 11
echoPin = 10
redLight = 8
yellowLight = 9
greenLight = 2
warningLight1 = 7
warningLight2 = 6
ldrPin = 0 # Analog pin for the LDR
heightThreshold = 20 # cm
lightThreshold = 100 # LDR threshold value for low light
"""Set up the ultrasonic sensor and lights"""
board.set_pin_mode_sonar(trigPin, echoPin, timeout=4000000)
board.set_pin_mode_digital_output(redLight)
board.set_pin_mode_digital_output(greenLight)
board.set_pin_mode_digital_output(yellowLight)
board.set_pin_mode_digital_output(warningLight1)
board.set_pin_mode_digital_output(warningLight2)
board.set_pin_mode_analog_input(ldrPin)
"""Lights turn off at start and red light is on by default"""
def resetLights():
board.digital_write(redLight, 0)
board.digital_write(greenLight, 0)
board.digital_write(yellowLight, 0)
board.digital_write(warningLight1, 0)
board.digital_write(warningLight2, 0)
def cleanup():
"""Turn off all LEDs and shutdown properly"""
print("\nCleaning up LEDs...")
resetLights()
board.shutdown()
print("Cleanup complete. Exiting.")
exit()
resetLights()
board.digital_write(redLight, 1)
noObjectMessagePrinted = False
try:
while True:
"""Read distance from the ultrasonic sensor"""
time.sleep(0.1)
distance = board.sonar_read(trigPin)
"""Read light level from the LDR"""
lightLevel = board.analog_read(ldrPin)
if lightLevel is not None:
print(f"Light Level: {lightLevel[0]}")
else:
print("Error: No reading from LDR.")
if distance is not None and len(distance) > 0:
print(f"Distance: {distance[0]} cm")
else:
print("Error: No reading from ultrasonic sensor.")
if lightLevel is not None and lightLevel[0] < lightThreshold:
# Turn on warning lights in low light
board.digital_write(warningLight1, 1)
board.digital_write(warningLight2, 1)
print("Low light detected. Warning lights ON.")
else:
# Turn off warning lights in normal light
board.digital_write(warningLight1, 0)
board.digital_write(warningLight2, 0)
if distance is not None and len(distance) > 0 and 0 < distance[0] < heightThreshold:
vehicleHeight = distance[0]
print("Distance:", vehicleHeight)
"""Traffic Light sequence begins once US sensor detects a vehicle"""
resetLights()
board.digital_write(yellowLight, 1)
time.sleep(2)
resetLights()
board.digital_write(greenLight, 1)
time.sleep(5)
"""Green light flashing if truck is still there"""
while True:
time.sleep(0.1)
distance = board.sonar_read(trigPin)
if distance[0] >= heightThreshold:
print("Vehicle no longer detected")
break
board.digital_write(greenLight, 0)
time.sleep(0.25)
board.digital_write(greenLight, 1)
time.sleep(0.25)
resetLights()
board.digital_write(redLight, 1)
noObjectMessagePrinted = False
else:
"""Stays red if no vehicle is there"""
if not noObjectMessagePrinted:
print("No object within threshold. Staying red.")
noObjectMessagePrinted = True
resetLights()
board.digital_write(redLight, 1)
except KeyboardInterrupt:
cleanup()
@lche2006 welcom to the forum.
So would it not be a good idea to include a schematic of your wiring?
You might want to look at this How to get the best out of this forum before you proceed any further.
We only know what you tell us. So far you have not told us enough to be useful.
As a retired senior lecturer at a UK University I would have thought that a traffic light system is a bit Noddy *. This is something I would expect as a much lower level project than Undergraduate level.
- If you are unsure about Noddy then see:-
Noddy
Thanks for the reply. Totally should have probably looked at all the rules first before jumping to posting. I'm doing a general first-year eng class, so it's essentially coding and electrical engineering for dumbies, probably due to Australia's non-existent coding in undergrad curriculum. For the most part, other areas have been easy. I am clearly neglecting something quite obvious with the LDR's code or wiring. I will be uploading a schematic ASAP
The LED You turn on is shining on the LDR?
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.