I have attempted to combine the circuit from this Rick and Morty Portal Gun project with a circuit to play sound effects, based on this Playing WAV file Using Arduino project.
My intention is to play a "portal gun" sound effect when the encoder button is pressed, in addition to lighting up 3 LEDs and the 4-digit display, and to play a "self destruct" sound effect when the "self destruct" button is pressed, in addition to the LEDs and 4-digit display.
The combined circuit is depicted in the attached file Portal_Gun_Circuit.png. Per the instructions in the WAV audio project, the audio files are saved to the SD card in WAV format, at 16K samples per second, mono, and 8 bits per sample.
I did not have the BC546B NPN transistor specified for the WAV audio project, so after some googling I modified the audio amplifier portion of the circuit to use a BC547 NPN transistor I have on hand, based the suggestions in this post.
When I compile and upload the code in the attached file "sound_test.cpp", (using PlatformIO in Visual Studio Code), the WAV audio files play as expected through the circuit, so I know the files are correctly encoded, and the SD card, amplifier circuit and speaker work. The sound quality isn't great, but at least it's audible.
When I compile and upload "main_with_leds.cpp", which combines the code for the Portal Gun LED effects with the WAV playback code, then I hear no audio output, none of the LEDs light up, and the following output repeats in the serial monitor, every few seconds, even though I am not pressing any buttons:
--- Miniterm on COM3 9600,8,N,1 ---
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
Could not init SD card
Encoder Switch Pressed
16
Self Destruct Button Pressed
10
Play rp1602.wav
Done Playing rp1602.wav
Play sd08.wav
Encoder Switch Pressed
16
Self Destruct Button Pressed
10
Play rp1602.wav
Done Playing rp1602.wav
Play sd08.wav
Encoder Switch Pressed
16
Self Destruct Button Pressed
10
Play rp1602.wav
Done Playing rp1602.wav
Play sd08.wav
if I disable WAV playback by commenting out lines 129 tmrpcm.play("rp1602.wav");
and 140 tmrpcm.play("sd08.wav");
, then the LEDs and 4-digit display respond to the encoder, encoder switch and "destruct" switch as expected.
On one attempt with the WAV playback enabled, I did initially get LED output after uploading the code, but when I pressed the "destruct button", the speaker only buzzed and the system stopped responding to inputs; I wasn't able to reproduce it again.
Parts list:
- clone Arduino Nano (Geekcreit)
- 4-bit TM1637 LED display
- SD Card Adapter
- KY-040 Rotary Encoder
- 8 ohm 0.5 W speaker from this kit
For clarity and focus on the audio problem, I have omitted a second set of LEDs and LiPo battery power circuit shown in the circuit diagram for the original Portal Gun project.
I am an experienced software engineer, but Arduino and electronics neophyte. I believe the problem is hardware-related (says the software guy!).
Any guidance toward diagnosing and solving the missing audio output is appreciated.
Regards,
Philip
sound_test.cpp (2.49 KB)
main_with_leds.cpp (7.54 KB)