import machine
import utime
# Define the GPIO pin for the speaker or buzzer
SPEAKER_PIN = 15 # Change this to the GPIO pin you are using
# Define the sound frequencies for alphabets and numerics
ALPHABET_SOUNDS = {'a': 440, 'b': 466, 'c': 494, 'd': 523, 'e': 554, 'f': 587, 'g': 622, 'h': 659, 'i': 698, 'j': 740,
'k': 784, 'l': 831, 'm': 880, 'n': 932, 'o': 988, 'p': 1047, 'q': 1109, 'r': 1175, 's': 1245, 't': 1319,
'u': 1397, 'v': 1480, 'w': 1568, 'x': 1661, 'y': 1760, 'z': 1865}
NUMERIC_SOUNDS = {'0': 523, '1': 587, '2': 659, '3': 698, '4': 784, '5': 880, '6': 988, '7': 1047, '8': 1175, '9': 1319}
# Setup the GPIO pin for the speaker or buzzer
speaker = machine.PWM(machine.Pin(SPEAKER_PIN))
def play_sound(frequency, duration):
speaker.freq(frequency)
speaker.duty_u16(32767)
utime.sleep_ms(duration)
speaker.duty_u16(0)
while True:
user_input = input("Enter alphabet or numeric (q to quit): ").lower()
if user_input == 'q':
break
if user_input.isalpha() and len(user_input) == 1:
if user_input in ALPHABET_SOUNDS:
play_sound(ALPHABET_SOUNDS[user_input], 500)
else:
print("Invalid input. Enter a valid alphabet.")
elif user_input.isdigit() and len(user_input) == 1:
if user_input in NUMERIC_SOUNDS:
play_sound(NUMERIC_SOUNDS[user_input], 500)
else:
print("Invalid input. Enter a valid numeric.")
else:
print("Invalid input. Enter a single alphabet or numeric character.")