import machine from machine import Pin, PWM import time import utime import wave import os from wavePlayer import wavePlayer import test_sine #make sure the amplifier is on amp_shutdown_pin = machine.Pin(21, machine.Pin.OUT, machine.Pin.PULL_UP) amp_shutdown_pin.value(0) #define a function to play a frequency def play_frequency(pin, frequency, duration): if frequency == 0: utime.sleep(duration) else: pwm = machine.PWM(machine.Pin(pin)) pwm.freq(frequency) pwm.duty_u16(65535//8) # 100% duty cycle = 65535 utime.sleep(duration) pwm.deinit() # define the start-up melody, a C-scale #The key of C contains 7 notes: C, D, E, F, G, A, B #C4 261.63 # D4 293.66 # E4 329.63 #F4 349.23 # G4 392.00 #A4 440.00 #B4 493.88 #C5 523.25 # frequencies from here: https://pages.mtu.edu/~suits/notefreqs.html def start_up_melody(): melody = [ (261, 250), (293, 250), (329, 250), (349, 250), (392, 250), (440, 250), (493, 250), (523,250), ] for note in melody: play_frequency(19, note[0], note[1] / 1000) # Convert duration to seconds #if the file name is main.py, run the following if __name__ == "__main__": start_up_melody() amp_shutdown_pin.value(1) # test_sine.play() # wavePlayer().play('/sounds/hello_lucas_02.wav') button_back = Pin(7, Pin.IN, Pin.PULL_DOWN) # button on pin GP7, it connects to the button on the back of the toy button_01 = Pin(8, Pin.IN, Pin.PULL_DOWN) # button on pin GP8, it connects to the button on the 01 spot on the top of the toy button_02 = Pin(9, Pin.IN, Pin.PULL_DOWN) # button on pin GP9, it connects to the button on the 02 spot on the top of the toy button_03 = Pin(10, Pin.IN, Pin.PULL_DOWN) # button on pin GP10, it connects to the button on the 03 spot on the top of the toy button_04 = Pin(11, Pin.IN, Pin.PULL_DOWN) # button on pin GP11, it connects to the button on the 04 spot on the top of the toy button_05 = Pin(12, Pin.IN, Pin.PULL_DOWN) # button on pin GP12, it connects to the button on the 05 spot on the top of the toy button_06 = Pin(14, Pin.IN, Pin.PULL_DOWN) # button on pin GP14, it connects to the button on the 06 spot on the top of the toy button_extra = Pin(15, Pin.IN, Pin.PULL_DOWN) # button on pin GP15. It has no home yet - it is an extra button pin note_duration = 250 while True: # a = button_01.value() # print(a) print(button_back.value()) time.sleep(0.01) #The key of C contains 7 notes: C, D, E, F, G, A, B #C4 261.63 # D4 293.66 # E4 329.63 #F4 349.23 # G4 392.00 #A4 440.00 #B4 493.88 while button_01.value() == 1: amp_shutdown_pin.value(0) wavePlayer().play('/sounds/mama16.wav') amp_shutdown_pin.value(1) while button_02.value() == 1: amp_shutdown_pin.value(0) wavePlayer().play('/sounds/dog.wav') amp_shutdown_pin.value(1) while button_03.value() == 1: amp_shutdown_pin.value(0) wavePlayer().play('/sounds/cat.wav') amp_shutdown_pin.value(1) while button_04.value() == 1: amp_shutdown_pin.value(0) wavePlayer().play('/sounds/yes.wav') amp_shutdown_pin.value(1) while button_05.value() == 1: amp_shutdown_pin.value(0) wavePlayer().play('/sounds/thank_you.wav') amp_shutdown_pin.value(1) while button_06.value() == 1: amp_shutdown_pin.value(0) wavePlayer().play('/sounds/please.wav') amp_shutdown_pin.value(1) while button_back.value() == 1: while button_01.value() == 1: amp_shutdown_pin.value(0) play_frequency(19,130,note_duration/1000) # C3 frequency amp_shutdown_pin.value(1) while button_02.value() == 1: amp_shutdown_pin.value(0) play_frequency(19,146,note_duration/1000) amp_shutdown_pin.value(1) while button_03.value() == 1: amp_shutdown_pin.value(0) play_frequency(19,164,note_duration/1000) amp_shutdown_pin.value(1) while button_04.value() == 1: amp_shutdown_pin.value(0) play_frequency(19,174,note_duration/1000) amp_shutdown_pin.value(1) while button_05.value() == 1: amp_shutdown_pin.value(0) play_frequency(19,196,note_duration/1000) amp_shutdown_pin.value(1) while button_06.value() == 1: amp_shutdown_pin.value(0) play_frequency(19,220,note_duration/1000) amp_shutdown_pin.value(1) # while button_extra.value() == 1: #if button extra is pressed, the following action will be initiated. Right now, I have no button_extra # amp_shutdown_pin.value(0) # play_mario_original() # amp_shutdown_pin.value(1) # # while button_back.value() == 1: # amp_shutdown_pin.value(0) # play_frequency(19,493,250/1000) #frequency of B4 # amp_shutdown_pin.value(1)