Peak analysis tutorial modified for LED

laserTea

New member
Using a Teensy 4 and Teensy 4 audio shield - I just completed the Teensy Audio Tutorial & Workshop.

Now I'm attempting to use the peak analysis data to pulse an LED. I modified the TFT Display tutorial script and mapped the peak1.read() value to 0, 255 and am sending that to the LED's pin=10 as analogWrite (code below)

But when I add the analog.write to my code and upload it, my teensy just produces a tone and freezes... What am I doing improperly here? And what is the best way to channel this data for LED intensity?

I feel like I'm very close, but can't figure out this last step... Any guidance would be a great help!


Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioPlaySdWav           playSdWav1;     //xy=199,194
AudioAnalyzePeak         peak1;          //xy=496,241
AudioAnalyzePeak         peak2;          //xy=501,282
AudioOutputI2S           i2s1;           //xy=573,197
AudioConnection          patchCord1(playSdWav1, 0, i2s1, 0);
AudioConnection          patchCord2(playSdWav1, 0, peak1, 0);
AudioConnection          patchCord3(playSdWav1, 1, i2s1, 1);
AudioConnection          patchCord4(playSdWav1, 1, peak2, 0);
AudioControlSGTL5000     sgtl5000_1;     //xy=503,351
// GUItool: end automatically generated code

// Use these with the Teensy Audio Shield
#define SDCARD_CS_PIN    10
#define SDCARD_MOSI_PIN  7
#define SDCARD_SCK_PIN   14

//LED attached to pin 10
const int LEDPin =  10;

void setup() {
  pinMode(LEDPin, OUTPUT);
  Serial.begin(9600);
  delay(500);

  
  AudioMemory(10);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.2);
  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);
    }
  }
  delay(1000);
}

elapsedMillis msecs;

void loop() {
  if (playSdWav1.isPlaying() == false) {
    Serial.println("Start playing");
    playSdWav1.play("SDTEST3.WAV");
    delay(10); // wait for library to parse WAV info
  }
  
  if (msecs > 15) {
    if (peak1.available() && peak2.available()) {
      msecs = 0;
      float leftNumber = peak1.read();



      int LEDIntensity = map(leftNumber, 0, 1, 0, 255);
      
      
      analogWrite(LEDPin, LEDIntensity);

      
      Serial.print(LEDIntensity);
      Serial.print(", ");


    }
  }
}
 
You're using pin 10 for the SDcard Chip Select and for an LED. Try an unused pin for the LED - but one which also has PWM capability.
But when you're mapping the peak value to zero or one, you might as well use digitalWrite instead of analogWrite, in which case any unused digital pin will do.

Pete
 
Well, that sure was a silly oversight on my part. Thanks so much for pointing this out Pete!

I have my LED pulsing perfectly to audio now
 
Back
Top