I used a Teensy 4.0 and an Audio Shield to build a little audio player. It plays a one minute rain noise WAV file on loop out the headphone jack. I have a pair of cheap Walmart USB powered speakers plugged in to the headphone jack, and they're both powered off the same USB power supply. I use this to generate rain noise in my baby's room so that he doesn't get woken up by creaking floorboards or squeaking doors.
When I first build the noise maker it worked perfectly. I had intended to run it only while putting the baby to sleep or during naps, but it tends to get left on a lot. For the first couple months of use it continued to work as expected, but recently it's developed some strange behavior. At seemingly random times it will stop playing for a couple dozen seconds, then start playing again. I tried plugging it into my computer to see if it sent the "startup" serial message when it bugged out, but instead the USB disconnected right as it started playing again. So maybe it's locking up and then resetting? I really don't know, and I'm not sure how to troubleshoot this. Power cycling doesn't seem to have much if any effect.
I'll include the code below, but the full project write-up is also available on GitHub: https://github.com/ofthedove/RainBox
There are two sketches in the repo. The `teensy_noise_maker` sketch is the one in use.
I'm using a SanDisk Extreme PLUS 32GB SD card that contains only the demo WAV files and my rain noise WAV file. I've had the card for a bit, but it's not seen much use as far as I know, and the sketch should only be reading from it.
When I first build the noise maker it worked perfectly. I had intended to run it only while putting the baby to sleep or during naps, but it tends to get left on a lot. For the first couple months of use it continued to work as expected, but recently it's developed some strange behavior. At seemingly random times it will stop playing for a couple dozen seconds, then start playing again. I tried plugging it into my computer to see if it sent the "startup" serial message when it bugged out, but instead the USB disconnected right as it started playing again. So maybe it's locking up and then resetting? I really don't know, and I'm not sure how to troubleshoot this. Power cycling doesn't seem to have much if any effect.
I'll include the code below, but the full project write-up is also available on GitHub: https://github.com/ofthedove/RainBox
There are two sketches in the repo. The `teensy_noise_maker` sketch is the one in use.
I'm using a SanDisk Extreme PLUS 32GB SD card that contains only the demo WAV files and my rain noise WAV file. I've had the card for a bit, but it's not seen much use as far as I know, and the sketch should only be reading from it.
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
AudioPlaySdWav playWav1;
AudioOutputI2S i2s;
AudioConnection patchCord1(playWav1, 0, i2s, 0);
AudioConnection patchCord2(playWav1, 1, i2s, 1);
AudioControlSGTL5000 sgtl5000_1;
#define SDCARD_CS_PIN 10
#define SDCARD_MOSI_PIN 11
#define SDCARD_SCK_PIN 13
#define VOLUME_PIN 15
void setup() {
Serial.begin(9600);
Serial.println("Starting");
AudioMemory(20);
SPI.setMOSI(SDCARD_MOSI_PIN);
SPI.setSCK(SDCARD_SCK_PIN);
if (!(SD.begin(SDCARD_CS_PIN))) {
// stop here, but print a message repetitively
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
sgtl5000_1.enable();
sgtl5000_1.volume(0.5);
}
void loop() {
playWav1.play("RAINLP1.WAV");
// A brief delay for the library read WAV info
delay(5);
while (playWav1.isPlaying())
{
float vol = analogRead(VOLUME_PIN);
vol = vol / 1023.0;
sgtl5000_1.volume(vol);
delay(10);
}
}