A basic projects needs to have 3 LEDs on the breadboard, which need to work as follows: • When the distance measured is greater than 20 cm, light the Green LED • When the distance is between 5 cm and 20 cm, light the Yellow LED • When the distance is 5 cm or less, light the Red LED • Only one LED is on at any given time The breadboard also needs a buzzer connected, which needs to operate as follows: • Off for distance above 20 cm • Beep for distance between 5 cm and 20 cm • Constant on for distance 5 cm and belowYou will need to use multiple nested if statements, but you should not resort to compound logical statements (and/or keywords). Consider the following pseudo-code: read_sensor function (run as a thread): while sensor is active: read distance if distance is long: activate green LED, deactivate yellow and red LEDs turn off buzzer else distance is medium: activate yellow LED, deactivate green and red LEDs beep buzzer if buzzer is allowed else: activate red LED, deactivate green and yellow LEDs turn on buzzer if buzzer is allowed wait a moment 8. Avoid using the sleep statement. It should only be used once in your entire program, to slow down the main program loop.
#!/usr/bin/python3 """Carlos Herrera""" from signal import signal, SIGTERM, SIGHUP, pause from time import sleep from threading import Thread from gpiozero import DistanceSensor, Buzzer, LEDBoard, Button, LED reading = True distance = True buzzer = True sensor = DistanceSensor(echo=20, trigger=21, max_distance=1) buzzer = Buzzer(24) green_long = LEDBoard(13) yellow_med = LEDBoard(19) red_short = LEDBoard(26) button = Button(6) def safe_exit(signum, frame): exit(1) def green_long(): gpio.output(green_long,GPIO.HIGH) gpio.output(yellow_med, GPIO.LOW) def yellow_med(): gpio.output(yellow_med, GPIO.HIGH) gpio.putput(green_long, GPIO.LOW) def read_distance(): while reading: print("Distance: " + '{:0.2f}'.format(sensor.value*100) + " cm") sleep(0.1) distance = reading if distance > 30: green.on elif distance>= 20 && < 15:="" yellow.on="" else="">< 5: red_short.on signal(sigterm, safe_exit) signal(sighup, safe_exit) return distance try: reader = thread(target=read_distance, daemon=true) reader.start() sensor.when_in_range = buzzer.on sensor.when_out_of_range = buzzer.off button.when_pressed = buzzer.off pause() distance = read_distance() pass finally: reading = false buzzer.close() sensor.close() leds.close() 5:="" red_short.on="" signal(sigterm,="" safe_exit)="" signal(sighup,="" safe_exit)="" return="" distance="" try:="" reader="Thread(target=read_distance," daemon="True)" reader.start()="" sensor.when_in_range="buzzer.on" sensor.when_out_of_range="buzzer.off" button.when_pressed="buzzer.off" pause()="" distance="read_distance()" pass="" finally:="" reading="False" buzzer.close()="" sensor.close()=""> 5: red_short.on signal(sigterm, safe_exit) signal(sighup, safe_exit) return distance try: reader = thread(target=read_distance, daemon=true) reader.start() sensor.when_in_range = buzzer.on sensor.when_out_of_range = buzzer.off button.when_pressed = buzzer.off pause() distance = read_distance() pass finally: reading = false buzzer.close() sensor.close() leds.close()>