Question
I have this modified code how can i add the following, I want every time i bring an object closer, it triggers the change in
I have this modified code how can i add the following,
I want every time i bring an object closer, it triggers the change in temperature and the buzzer buzzes once with a different tone for the diffferent centimeters
the buzzer should not be continous just one buzz and it sleeps and when the temperature and humidity change as i bring the object closer it buzzes.
could anyone help.
ultrasonic ranger distance buzzer value
>20 cm 0
<20cm 15
<15cm 25
<10 cm 50
# Import necessary libraries
import math
from time import sleep
from grovepi import *
# Define constants for the digital ports
TEMP_SENSOR = 3 # DHT11 to D3
SOUND_BUZZER = 5 # Sound Buzzer to D5
ULTRASONIC_RANGER = 6 # Ultrasonic ranger to D6
LED = 4 # LED to D4
# Read temperature and humidity from the DHT11 sensor
def read_temp_sensor():
try:
# Get temperature and humidity values
[temp, hum] = dht(TEMP_SENSOR, 0)
# Check if values are not empty or NaN
if ((math.isnan(temp) == False) and (math.isnan(hum) == False) and (hum >= 0)):
temperature = temp
humidity = hum
print("Temperature = %.2f Celcius\tHumidity = %.2f%%" % (temperature, humidity))
return temperature
except (KeyboardInterrupt, IOError, Exception) as e:
print("An error has occurred:", e)
# Check and update temperature
def check_temperature(prev_temp, curr_temp):
if prev_temp != curr_temp:
digitalWrite(SOUND_BUZZER, 1)
digitalWrite(LED, 1)
sleep(1)
digitalWrite(SOUND_BUZZER, 0)
digitalWrite(LED, 0)
return curr_temp
return prev_temp
# Check and update distance
def check_distance(distance):
if distance <= 10: # adjust as needed
digitalWrite(SOUND_BUZZER, 1)
digitalWrite(LED, 1)
sleep(1)
digitalWrite(SOUND_BUZZER, 0)
digitalWrite(LED, 0)
# Read distance from ultrasonic ranger
def read_ultrasonic_ranger():
try:
# Get distance from ultrasonic ranger
distance = ultrasonicRead(ULTRASONIC_RANGER)
return distance
except (KeyboardInterrupt, IOError, Exception) as e:
print("An error has occurred:", e)
# Main program
prev_temp = 0
while True:
curr_temp = read_temp_sensor()
prev_temp = check_temperature(prev_temp, curr_temp)
distance = read_ultrasonic_ranger()
check_distance(distance)
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