# Project#5 - Let's make a sound detector # Connections: Add jumpers to the LED headers (Labelled G16BLU and G17RED) to the left of the coin battery. # Also add jumpers to the GP15 S1 header and GP14 S2 headers to the right of the coin battery socket. # The DIP swtiche channels are tied to GPIOs 18-thrrough 21 # Add a jumper to the BUZ/G22 header to connect GP22 to the buzzer control circuit. # Add a jumer to the MIC/AD1 header to connect the AD1/GP27 pin to the microphone output import machine # Think of "import" as importing a library. This library allows for us to use GPIOs import utime # This "library" allows for us to tell time if connected to the internet, and use delays like 'utime.sleep(1)' #The below declarations are new since project#4 Buzzer = machine.Pin(22, machine.Pin.OUT) #Setup the Buzzer, which is connected to GPIO 22. Declare it as an output. Microphone = machine.ADC(27) # Setup GPIO27 to act as the ADC input for the microphone. This is the AD1 line # Button = machine.Pin(15, machine.Pin.IN) #Setup the SEL1 button pin to GPIO 15. Set it as an input and name it "Button" Button2 = machine.Pin(14, machine.Pin.IN) #Setup the SEL2 button pin to GPIO 14. Set it as an input and name it "Button2" DIPA = machine.Pin(21, machine.Pin.IN) #Setup DIP Switch Channel-A as an output DIPB = machine.Pin(20, machine.Pin.IN) #Setup DIP Switch Channel-B as an output DIPC = machine.Pin(19, machine.Pin.IN) #Setup DIP Switch Channel-C as an output DIPD = machine.Pin(18, machine.Pin.IN) #Setup DIP Switch Channel-D as an output LEDRED = machine.Pin(16, machine.Pin.OUT) # Set up GPIO16 as an output, and name it LEDRED LEDBLU = machine.Pin(17, machine.Pin.OUT) # Do the same for GPIO17 LEDRED.value(0) # Turn the red LED off. If you set it to (1), then you'd be turning it on LEDBLU.value(0) # Turn the blue LED off. If you set it to (1), then you'd be turning it on state = 0 # state is a variable/storage register. Set it to a value of 0. hold = 0 # hold is another variable that we'll use to store data in conversion = 3.3/(65535) # Conversion helps us to turn ADC readings (Numeral values) into a voltage representation. # 3.3v is the full scale voltage. 0-65565 is the full 16-bit scale value used for ADC conversions. 0000000000000000-1111111111111111 def NoiseTest(): #This function waits for noise. When noise is detected, the buzzer sounds print("Waiting for noise...") #Print this to the shell global state # Setting variable "state" as a global means that it will be kept intact across functions state = 0 # Reset state to a value of 0 LEDBLU.value(1) # Turn on the blue LED while state == 0: # Loop the following while state equals 0 hold = Microphone.read_u16() # Take a 160-bit analog to digital reading from the microphone and place it into the 'hold' variable. hold = hold*conversion # hold equals itself multiplied by 'conversion'. This will give us a voltage representation. if(hold > 2): # if the signal is higher than 2v, then a loud noise has been detected. If so, change state to 1 to end this while loop state = 1 # See above LEDBLU.value(0) # Turn the LED off Buzz() # call the buzz() function print("Noise detected... Moving on!") # Print this to the shell. This is the end of this function. def Buzz(): # This function beeps the buzzer 5 times print("Buzzer on...") # Print this to the shell for i in range(5): # This is a FOR loop, or 'Range" It is set to loop the following code 5 times. Buzzer.value(1) # Turn on the buzzer utime.sleep(0.25) # Delay of 250ms, or 1/4 Seconds Buzzer.value(0) # Turn the buzzer off utime.sleep(0.25) #250ms delay print("Buzzer off") # Once looped 5 times, the range ends, and this will be printed to the shell # This is the end of this function # The following is the main loop. It will loop calling the NoiseTest() function over and over without end. while True: NoiseTest()