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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started