Question: The following Python code for a raspberry pi, I have added a temparature sensor which works, however, I also added a sound buzzer and an

The following Python code for a raspberry pi, I have added a temparature sensor which works, however, I also added a sound buzzer and an ultra sonic ranger. I would like to add the buzzer values such that when the distance chnages the buzzer buzzes with different intensity

Ultrasonic Ranger Distance Buzzer value

<20cm 0

<20cm 15

<15cm 25

<10 cm 50

#importing the necessary libraries

import math

from time import *

from grovepi import *

#define a global variable to store teh digital port (D3)

#*************************************************

#Connet the temperature Sensor(DHT)

# to a digital port(in this example)

#we will use (D3)

#*************************************************

temp_sensor3 = 3 #connect DHT11 to D3

sound_buzzer5= 5 #connect the Sound Buzzer to D5

ultrasonic_ranger6=6 #connect the ultra sonic ranger on 6

myLED4= D4 # connect the LED to D4

prev_temp = 0

#create a function to read temperature and humidity sensor data

#*************************************************

#function: read_temp_sensor()

#this function will capture sensor

#data from DHT11 via GrovePi

#**************************************************

def read_temp_sensor():

try:

#create a vector of two elements to store

#temperature and humidity values

#->grovepi.dht is a method that returns 2,

# values(first in temperature where as,

# the second is humidity)

#arg1: defines the port number

#arg2: type of DHT sensor(0 for this kit)

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

#ensure that the temperature and humidity values are not empty or null

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

temperature = temp #copy temp value into variable temperature

humidity =hum #copy hum value into variable humidity

print("Temperature = %.2f Celcius\tHumidity = %.2f% %" % (temperature, humidity))

return temperature

except KeyboardInterrupt:

print("exiting...")

except IOError as IOe:

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

except Exception as e:

print("some error")

def check_temperature(prev_temp, curr_temp):

if prev_temp != curr_temp:

digitalWrite(sound_buzzer5, 1)

digitalWrite(myLED4, 1)

sleep(1)

digitalWrite(sound_buzzer5, 0)

digitalWrite(myLED4, 0)

return curr_temp

return prev_temp

def check_distance(ultrasonic_ranger6):

try:

distance = ultrasonicRead(ultrasonic_ranger6)

if distance <= 10: #adjust this distance as needed

digitalWrite(sound_buzzer5, 1)

digitalWrite(myLED4, 1)

sleep(1)

digitalWrite(sound_buzzer5, 0)

digitalWrite(myLED4, 0)

except KeyboardInterrupt:

print("exiting...")

except IOError as IOe:

print("An error has occ

except IOError as IOe:

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

except Exception as e:

print("some error")

#function to turn on the buzzer

def buzzer_on():

digitalWrite(sound_buzzer5, 1)

return

#function to turn off the buzzer

def buzzer_off():

digitalWrite(sound_buzzer5, 0)

return

#function to turn on the LED

def LED_on():

digitalWrite(myLED4, 1)

return

#function to turn off the LED

def LED_off():

digitalWrite(myLED4, 0)

return

#read distance from ultrasonic ranger

def read_ultrasonic_ranger():

try:

#read the distance from the ultrasonic ranger

distance = ultrasonicRead(ultrasonic_ranger6)

return distance

except:

print("Error reading ultrasonic ranger")

return

#main program

while (1):

#read temperature from the temperature sensor

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

#check if the temperature has changed

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))

#check if the temperature has changed

if temperature > 30:

#turn on the buzzer

buzzer_on()

LED_on()

#wait for 1 second

time.sleep(1)

#turn off the buzzer

buzzer_off()

LED_off()

#read distance from the ultrasonic ranger

distance = read_ultrasonic_ranger()

#check if the distance has changed

if distance < 10:

#turn on the buzzer

buzzer_on()

LED_on()

#wait for 1 second

time.sleep(1)

#turn off the buzzer

buzzer_off()

LED_off()

#wait for 1 second before next iteration

time.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!