====== Alertor ====== **//The Circuit://** {{:hpc0:doorbell.jpg?nolink|}} **//The Code://** 1 #!/usr/bin/env python3 2 ######################################################################## 3 # Filename : Alertor.py 4 # Description : Make Alertor with buzzer and button 5 # Author : www.freenove.com 6 # modification: 2019/12/27 7 ######################################################################## 8 import RPi.GPIO as GPIO 9 import time 10 import math 11 12 buzzerPin = 11 # define the buzzerPin 13 buttonPin = 12 # define the buttonPin 14 15 #Initial setup of the board and buzzer 16 def setup(): 17 global p 18 GPIO.setmode(GPIO.BOARD) # Use PHYSICAL GPIO Numbering 19 GPIO.setup(buzzerPin, GPIO.OUT) # set RGBLED pins to OUTPUT mode 20 GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set buttonPin to INPUT mode. Set up to HIGH 21 p = GPIO.PWM(buzzerPin, 1) 22 p.start(0); 23 24 25 #function that will activate the alertor when button is pressed 26 def loop(): 27 while True: 28 if GPIO.input(buttonPin)==GPIO.LOW: 29 alertor() 30 print ('alertor turned on >>> ') 31 else : 32 stopAlertor() 33 print ('alertor turned off <<<') 34 35 #function that sets the frequency of sound made 36 def alertor(): 37 p.start(50) 38 for x in range(0,361): # Make frequency of the alertor that of a sine wave 39 sinVal = math.sin(x * (math.pi / 180.0)) # calculate the sine value 40 toneVal = 2000 + sinVal * 500 # Add to the frequency so that humans can hear it 41 p.ChangeFrequency(toneVal) # Change to the new Frequency 42 time.sleep(0.001) 43 44 #function that stops the alertor 45 def stopAlertor(): 46 p.stop() 47 48 #function that clears the board 49 def destroy(): 50 GPIO.output(buzzerPin, GPIO.LOW) # Turn off buzzer 51 GPIO.cleanup() # Release GPIO resource 52 53 if __name__ == '__main__': 54 print ('Program is starting...') 55 setup() 56 try: 57 loop() 58 except KeyboardInterrupt: # When program ends clean up 59 destroy() This project has the same setup as the [[hpc0:doorbell|doorbell]] project, with the only change being that instead of an active buzzer we replaced it with a passive buzzer. After replacing the buzzer and activating the code we find that buzzer makes a new sound that changes frequency. This change in frequency is using number along a sine wave, which is then converted into a frequency that we can hear. As for the code I added comments to describe the functions, made a couple of the longer comments more readable, and made the comments align with each other.