Question: I need help, when i run this command, the doesn't seem to work and when it does the buzzer rings it is continous and won't

I need help, when i run this command, the doesn't seem to work and when it does the buzzer rings it is continous and won't stop. How can i make it only beep once when i move an object closer which triggers a change in temperature or far away based on the distance. I don't want a continous tone.

import math

from time import *

from grovepi import *

temp_sensor3 = 3

sound_buzzer5 = 5

ultrasonic_ranger6 = 6

myLED4 = 4

prev_temp = None

def read_temp_sensor():

try:

distance = ultrasonicRead(ultrasonic_ranger6)

if distance < 20:

buzzer_value = 0

elif distance < 15:

buzzer_value = 15

elif distance < 10:

buzzer_value = 25

else:

buzzer_value = 50

[temp,hum] = dht(temp_sensor3,0)

if ((math.isnan(temp) == False) and (math.isnan(hum) == False) and (hum >= 0)):

global prev_temp

if prev_temp is None:

prev_temp = temp

elif prev_temp != temp:

digitalWrite(sound_buzzer5, buzzer_value)

digitalWrite(myLED4, 1)

sleep(0.5)

digitalWrite(myLED4, 0)

prev_temp = temp

print("Temperature = %.2f Celcius\tHumidity = %.2f% %" % (temp, hum))

except KeyboardInterrupt:

print("Exiting...")

except IOError as IOe:

print("An error has occurred. %" % IOe)

except Exception as e:

print("An unexpected error has occurred: %s" % str(e))

while True:

read_temp_sensor()

sleep(1)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!