Tracking Frequencies with the Counter

  • Now that we’re tracking each button press, we’ll match counter values with pitches and NeoPixels.
  • To do this we create a long if statement that checks for different values of the counter we created. Starting on the lowest pitch, which is Ab, where the counter is initially set to 0, the board will output a blue hue on the 9th NeoPixel of the board. We will set the frequency variable to that of Ab’s frequency which we declared earlier.
  • Imagine the up button has now been pressed. The counter value would now be set to 1. Because we want to go up a pitch to A natural, we say if counter = 1, set the 9th NeoPixel to green and update the frequency to that of A natural’s.
  • We repeat this process all the way until the counter = 16 so that every pitch is accounted for.
  • You might be expecting there only to be 12 pitches and there are but the extra 5 are actually the enharmonics of other pitches such as A# and Bb.
Download: file
#If statements to determine which pitch the board is on
   #We will use the current counter value to set which frequency, neopixel, and color should be selected
   if counter == 0:  # Ab
       pixels[9] = (0, 0, 255) #Blue
       FREQUENCY = Ab3
   elif counter == 1:  # A
       pixels[9] = (0, 255, 0) #Green
       FREQUENCY = A3
   elif counter == 2:  # A#
       pixels[9] = (255, 0, 0) #Red
       FREQUENCY = As3
   elif counter == 3:  # Bb
       pixels[0] = (0, 0, 255)
       FREQUENCY = Bb3
   elif counter == 4:  # B
       pixels[0] = (0, 255, 0)
       FREQUENCY = B3
   elif counter == 5:  # C
       pixels[1] = (0, 255, 0)
       FREQUENCY = C4
   elif counter == 6:  # C#
       pixels[1] = (255, 0, 0)
       FREQUENCY = Cs4
   elif counter == 7:  # Db
       pixels[2] = (0, 0, 255)
       FREQUENCY = Db4
   elif counter == 8:  # D
       pixels[2] = (0, 255, 0)
       FREQUENCY = D4
   elif counter == 9:  # D#
       pixels[2] = (255, 0, 0)
       FREQUENCY = Ds4
   elif counter == 10:  # Eb
       pixels[3] = (0, 0, 255)
       FREQUENCY = Eb4
   elif counter == 11:  # E
       pixels[3] = (0, 255, 0)
       FREQUENCY = E4
   elif counter == 12:  # F
       pixels[4] = (0, 255, 0)
       FREQUENCY = F4
   elif counter == 13:  # F#
       pixels[4] = (255, 0, 0)
       FREQUENCY = Fs4
   elif counter == 14:  # Gb
       pixels[5] = (0, 0, 255)
       FREQUENCY = Gb4
   elif counter == 15:  # G
       pixels[5] = (0, 255, 0)
       FREQUENCY = G4
   elif counter == 16:  # G#
       pixels[5] = (255, 0, 0)
       FREQUENCY = Gs4
  • What happens when the counter goes above 16 or below 0?
  • Well if we press the up button when the counter is at 16 and the pitch is set to G#, we want it to go back to Ab where the counter = 0.
  • Conversley if we're at Ab where the clounter = 0 and we want to go down a pitch to G# we'll have to set the counter to 16.
  • To do this we add the statements below.
Download: file
elif counter > 16:  # if counter goes above 16 set back to 0
       counter = 0
elif counter < 0:  # if counter goes below 0 set back to 16
       counter = 16
This guide was first published on Mar 28, 2018. It was last updated on Mar 28, 2018. This page (Tracking Frequencies with the Counter) was last updated on Dec 05, 2019.