# Project#7 - Time to Play some Audio with the help of SPI and our audio player! # We need to set up our SPI commication lines for this project, as that is how we instruct our audio player to play sound bites # Please refer to the little buddy talker library on the project page for lookup values # Connections: Add jumpers to the LED headers (Labelled G16BLU and G17RED) to the left of the coin battery. # Also add jumpers to the GP15 S1 header and GP14 S2 headers to the right of the coin battery socket. # The DIP swtiche channels are tied to GPIOs 18-thrrough 21 # Add a jumper to the BUZ/G22 header to connect GP22 to the buzzer control circuit. # Add a jumper to the MIC/AD1 header to connect the AD1/GP27 pin to the microphone output # Add a jumper to the header just behind the light sensor (Right between the microphone and the light sensor) # Make sure that you have a stereo cable connected between the AUDIO output jack and the STEREO INPUT line if your powered speakers # NO CONNETIONS NEED TO BE MADE FOR THE AUDIO PLAYER. THEY ARE DEDICATED TO GPIOs2,3,4,5 AND 6. THESE GPIOS CAN'T BE USED FOR OTHER ITEMS! import machine # Think of "import" as importing a library. This library allows for us to use GPIOs import utime # This "library" allows for us to tell time if connected to the internet, and use delays like 'utime.sleep(1)' #The below declarations are new since project#6 ASTOP = machine.Pin(6, machine.Pin.IN) #Setup the ASTOP (AUDIO STOP) pint o GPIO 6. Set it as an input. cs = machine.Pin(5, machine.Pin.OUT) #Setup the CS (Chip select pin for the audio chip. GPIO5. Set as output. cs.value(1) # Set the CS line to 1 (HIGH/3.3v) spi_sck=machine.Pin(2) # Set SPI clock to GPIO pin 2 spi_tx=machine.Pin(3) # Set SPI MASTER OUT to GPIO pin 3 spi_rx=machine.Pin(4) # Set SPI MASTER IN to GPIO pin 4 spi=machine.SPI(0,baudrate=500000,sck=spi_sck,mosi=spi_tx, phase=1, polarity=0) # Setup SPI communications settings utime.sleep(1) # Sleep/Wait for one second hold = '\x00' # 'hold' acts as the pointer for the little buddy talker chip. Load the HEX value of the audio bite that you want to play, then call the playaudio() function. # LightSensor = machine.ADC(26) # Setup GPIO26 to act as the ADC input for the light sensor. Buzzer = machine.Pin(22, machine.Pin.OUT) #Setup the Buzzer, which is connected to GPIO 22. Declare it as an output. Microphone = machine.ADC(27) # Setup GPIO27 to act as the ADC input for the microphone. This is the AD1 line Button = machine.Pin(15, machine.Pin.IN) #Setup the SEL1 button pin to GPIO 15. Set it as an input and name it "Button" Button2 = machine.Pin(14, machine.Pin.IN) #Setup the SEL2 button pin to GPIO 14. Set it as an input and name it "Button2" DIPA = machine.Pin(21, machine.Pin.IN) #Setup DIP Switch Channel-A as an output DIPB = machine.Pin(20, machine.Pin.IN) #Setup DIP Switch Channel-B as an output DIPC = machine.Pin(19, machine.Pin.IN) #Setup DIP Switch Channel-C as an output DIPD = machine.Pin(18, machine.Pin.IN) #Setup DIP Switch Channel-D as an output LEDRED = machine.Pin(16, machine.Pin.OUT) # Set up GPIO16 as an output, and name it LEDRED LEDBLU = machine.Pin(17, machine.Pin.OUT) # Do the same for GPIO17 LEDRED.value(0) # Turn the red LED off. If you set it to (1), then you'd be turning it on LEDBLU.value(0) # Turn the blue LED off. If you set it to (1), then you'd be turning it on state = 0 # state is a variable/storage register. Set it to a value of 0. hold = 0 # hold is another variable that we'll use to store data in conversion = 3.3/(65535) # Conversion helps us to turn ADC readings (Numeral values) into a voltage representation. # 3.3v is the full scale voltage. 0-65565 is the full 16-bit scale value used for ADC conversions. 0000000000000000-1111111111111111 def playaudio(): print("Playing sound bite...") # Print this to the shell global hold # global means that this valiable can be used across functions. cs.value(0) # Clear the cs pin to 0v to activate the audio chip utime.sleep(0.001) # Wait 1ms spi.write('\x98') # '\x98' is the first value that is always sent to the audio chip spi.write(hold) # The HEX value stored in 'hold' will be sent to the audio chip, which will instruct it to play the sound bite in question utime.sleep(0.001) # Wait another 1ms cs.value(1) # Set the cs pin to 5v to end the transaction utime.sleep(0.01) # Wait 10ms while ASTOP.value() == True: #Turn on the red LED for as long as the ASTOP chip signal is high (true). Once this line goes low, the audio bite is done playing. LEDRED.value(1) # Turn on the red LED LEDRED.value(0) # When the while loop ends, the audio bite has completed, and the LED turns off. print("Done...") # Print this to the shell # This is the end of this function # This is the main code loop. If is very simple. Play the first three audio bites on the little buddy talker over and over... while True: # Do the following forever... hold = '\x00' # /x00 equals 0 in HEX format playaudio() # Call the playaudio function to play the audio bite. utime.sleep(1) # Wait one full second hold = '\x01' # /x01 equals 1 in HEX format playaudio() # Call the playaudio function to play the audio bite. utime.sleep(1) # Wait one full second hold = '\x02' # /x02 equals 2 in HEX format playaudio() # Call the playaudio function to play the audio bite. utime.sleep(1) # Wait one full second print("Let's do it again!") # Print this to the shell and start over # This is the end of the main loop. It will look over and over and over forever.