from machine import Pin, PWM, ADC
import time
#Notes Frequency to be played by the buzzer
notes = {
'C0': 16, 'C#0': 17, 'D0': 18, 'D#0': 19, 'E0': 20, 'F0': 21, 'F#0': 23, 'G0': 24, 'G#0': 26, 'A0': 27, 'A#0': 29, 'B0': 31,
'C1': 33, 'C#1': 35, 'D1': 37, 'D#1': 39, 'E1': 41, 'F1': 44, 'F#1': 46, 'G1': 49, 'G#1': 52, 'A1': 55, 'A#1': 58, 'B1': 62,
'C2': 66, 'C#2': 70, 'D2': 74, 'D#2': 78, 'E2': 82, 'F2': 87, 'F#2': 92, 'G2': 98, 'G#2': 104, 'A2': 110, 'A#2': 117, 'B2': 124,
'C3': 131, 'C#3': 139, 'D3': 147, 'D#3': 156, 'E3': 165, 'F3': 175, 'F#3': 185, 'G3': 196, 'G#3': 208, 'A3': 220, 'A#3': 233, 'B3': 247,
'C4': 262, 'C#4': 277, 'D4': 294, 'D#4': 311, 'E4': 330, 'F4': 349, 'F#4': 370, 'G4': 392, 'G#4': 415, 'A4': 440, 'A#4': 466, 'B4': 494,
'C5': 523, 'C#5': 554, 'D5': 587, 'D#5': 622, 'E5': 659, 'F5': 698, 'F#5': 740, 'G5': 784, 'G#5': 831, 'A5': 880, 'A#5': 932, 'B5': 988,
'C6': 1047, 'C#6': 1109, 'D6': 1175, 'D#6': 1245, 'E6': 1319, 'F6': 1397, 'F#6': 1480, 'G6': 1568, 'G#6': 1661, 'A6': 1760, 'A#6': 1865, 'B6': 1976,
'C7': 2093, 'C#7': 2218, 'D7': 2349, 'D#7': 2489, 'E7': 2637, 'F7': 2794, 'F#7': 2960, 'G7': 3136, 'G#7': 3322, 'A7': 3520, 'A#7': 3729, 'B7': 3951,
'C8': 4186, 'C#8': 4435, 'D8': 4699, 'D#8': 4978, 'E8': 5274, 'F8': 5588, 'F#8': 5920, 'G8': 6272, 'G#8': 6645, 'A8': 7040, 'A#8': 7459, 'B8': 7902,
}
# Pin connections
button1_do = Pin(12, Pin.IN, Pin.PULL_UP)
button2_re = Pin(11, Pin.IN, Pin.PULL_UP)
button3_mi = Pin(10, Pin.IN, Pin.PULL_UP)
button4_fa = Pin(9, Pin.IN, Pin.PULL_UP)
button5_sol = Pin(7, Pin.IN, Pin.PULL_UP)
button6_la = Pin(6, Pin.IN, Pin.PULL_UP)
button7_si = Pin(5, Pin.IN, Pin.PULL_UP)
button8_do = Pin(4, Pin.IN, Pin.PULL_UP)
buzzer = PWM(Pin(8))
pot = ADC(Pin(28))
#pot.atten(ADC.ATTN_11DB)
def map_range(value, in_min, in_max, out_min, out_max):
return (value - in_min) * (out_max - out_min) // (in_max - in_min) + out_min
def playtone(freq, volume):
'''
This function plays the tone in the desired frequency.
It takes the notes above as a parameter
'''
buzzer.duty_u16(volume)
buzzer.freq(freq)
def bequiet():
'''
This function stops the buzzer
'''
buzzer.duty_u16(0)
def main():
while True:
if button1_do.value() == 0:
pot_value = pot_value = pot.read_u16()
mapped_pot_value = map_range(pot_value, 0, 65535, 0, 1000)
print(" Potentiometer Value:", mapped_pot_value)
print("DO")
playtone(notes['C4'],mapped_pot_value)
elif button2_re.value() == 0:
pot_value = pot.read_u16()
print("RE")
playtone(notes['D4'],pot_value)
elif button3_mi.value() == 0:
pot_value = pot.read_u16()
print("MI")
playtone(notes['E4'],pot_value)
elif button4_fa.value() == 0:
pot_value = pot.read_u16()
print("FA")
playtone(notes['F4'],pot_value)
elif button5_sol.value() == 0:
pot_value = pot.read_u16()
print("SOL")
playtone(notes['G4'],pot_value)
elif button6_la.value() == 0:
pot_value = pot.read_u16()
print("LA")
playtone(notes['A4'],pot_value)
elif button7_si.value() == 0:
pot_value = pot.read_u16()
print("SI")
playtone(notes['B4'],pot_value)
elif button8_do.value() == 0:
pot_value = pot.read_u16()
print("DO>")
playtone(notes['C5'],pot_value)
print(pot_value)
else:
#print("button NOT pressed")
bequiet()
if __name__ == "__main__":
main()