Hi! I'm using a Teensy 4.1 and a MAX98357A amplifier breakout connected to the Teensy using I2S. My ultimate goal is to flash an LED using PWM in accordance with sound playing through a speaker. I haven't implemented FFT yet because when I try to use analogWrite, the sound output becomes garbled. Using the following sketch which does not call analogWrite, the audio is clear and everything functions how I would expect:
However, when I call analogWrite using the following code, the sound becomes garbled/staticky but the LED fades as expected:
I've tried hard to find out what's going on and just can't figure it out. It seems like something happens when analogWrite will be called to block audio playback. Any troubleshooting advice is appreciated! Happy to post pictures and audio if someone thinks it would be helpful.
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
AudioPlaySdWav playSdWav1; //xy=230,309
AudioMixer4 mixer1; //xy=511,399
AudioOutputI2S audioOutput; //xy=755,392
AudioConnection patchCord1(playSdWav1, 0, mixer1, 0);
AudioConnection patchCord2(playSdWav1, 1, mixer1, 0);
AudioConnection patchCord7(mixer1, 0, audioOutput, 0);
#define SDCARD_CS_PIN BUILTIN_SDCARD
#define SDCARD_MOSI_PIN 11 // not actually used
#define SDCARD_SCK_PIN 13 // not actually used
void setup() {
Serial.begin(9600);
AudioMemory(12);
SPI.setMOSI(SDCARD_MOSI_PIN);
SPI.setSCK(SDCARD_SCK_PIN);
if (!(SD.begin(SDCARD_CS_PIN))) {
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
pinMode(2, OUTPUT);
}
void loop() {
if (playSdWav1.isPlaying() == false) {
playSdWav1.play("THUNDERCLAP.WAV");
}
for (int i = 0; i < 255; i++) {
// analogWrite(2, i);
delay(10);
}
}
However, when I call analogWrite using the following code, the sound becomes garbled/staticky but the LED fades as expected:
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
AudioPlaySdWav playSdWav1; //xy=230,309
AudioMixer4 mixer1; //xy=511,399
AudioOutputI2S audioOutput; //xy=755,392
AudioConnection patchCord1(playSdWav1, 0, mixer1, 0);
AudioConnection patchCord2(playSdWav1, 1, mixer1, 0);
AudioConnection patchCord7(mixer1, 0, audioOutput, 0);
#define SDCARD_CS_PIN BUILTIN_SDCARD
#define SDCARD_MOSI_PIN 11 // not actually used
#define SDCARD_SCK_PIN 13 // not actually used
void setup() {
Serial.begin(9600);
AudioMemory(12);
SPI.setMOSI(SDCARD_MOSI_PIN);
SPI.setSCK(SDCARD_SCK_PIN);
if (!(SD.begin(SDCARD_CS_PIN))) {
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
pinMode(2, OUTPUT);
}
void loop() {
if (playSdWav1.isPlaying() == false) {
playSdWav1.play("THUNDERCLAP.WAV");
}
for (int i = 0; i < 255; i++) {
analogWrite(2, i);
delay(10);
}
}
I've tried hard to find out what's going on and just can't figure it out. It seems like something happens when analogWrite will be called to block audio playback. Any troubleshooting advice is appreciated! Happy to post pictures and audio if someone thinks it would be helpful.