• This last chunk of code will tell the speaker to sound the selected pitch when it hears a loud sound aka your breath!
  • We'll say that if the magnitude of the sound is greater than 5000 (value defined earlier) then sound the current pitch.
  • Next we create the pitch we want in the form of a sine wave that is created at the desired frequency.
  • Then we’ll tell the speaker to play the pitch for 1 second.
  • Lastly we tell the board to show the current value of the NeoPixels.
#If statement to trigger pitch when user blows into mic
   #We will say that on a any loud sound the pitch is triggered
   if magnitude > blowThresshold: #any time we get a sound with a magnitude greater than the value of blowThresshold, trigger the current pitch (can be changed at top where it is defined)
       SAMPLERATE = 8000
       length = SAMPLERATE // FREQUENCY #create length of sample
       sine_wave = array.array("H", [0] * length) #create an array for a sine wave
       for i in range(length):
           sine_wave[i] = int(math.sin(math.pi * 2 * i / 18) * (2 ** 15) + 2 ** 15) #fill the array with values
       sample = audioio.AudioOut(board.SPEAKER, sine_wave)
       sample.frequency = SAMPLERATE
       sample.play(loop=True)  # Play the sample
       time.sleep(pitchLength)  # Play for length of pitchLength
       sample.stop()  # we tell the board to stop
   pixels.show() #show the desired neopixel light up on board 
#End program!!!

All done with programming! Well almost...

This guide was first published on Mar 28, 2018. It was last updated on Mar 08, 2024.

This page (Triggering the Pitches) was last updated on Mar 08, 2024.

Text editor powered by tinymce.