Teensy audio not working with external power

Status
Not open for further replies.

Robbert

Active member
I've been writing a project for two Teensy's including SGTL5000 audio shields. The code for both teensy's can be found here: https://github.com/RobbertGroenendijk/protocol_1
They're communicating RGB color data by sending the data converted to frequency back-and-forth between the two of them. The code is designed to run on both, and they will switch their sender/receiver state after each full RGB transmit.

When uploading the code to the Teensy it works fine (using PlatformIO in Atom IDE) but when I unplug it and put in a external power source to the Teensy it seems something is not working properly. The indicator light on the Teensy does not turn on and the audio is not working. Though the indicator LEDs that I hooked up to check for receiver/sender state work fine so it seems the Teensy is doing it's job. When I plug the teensy in my computer I first have to upload a Teensy audio example sketch with the Arduino IDE before the Teensy works fine again.

I have a feeling it has something to do with me including the Teensy audio library into a separate audio-processor class but can't figure out the problem. I hope someone can help me out with this.
All classes are located in the lib folder, and everything is compiled automatically by PlatformIO this way.

class_audioProcessor.h
Code:
#ifndef class_audioProcessor_h
#define class_audioProcessor_h

#include "Arduino.h"

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

class AudioProcessor {
  public:
    void setup();
    void reset();
    void testTone(char _c);
    void stopTone();
    void toggleMixer(int _mixerNumber, float _volume);
    void sendTone(int _frequency);
    int getHighestFFTBand(int _beginBand, int _endBand);
    void saveSignal();
    int getSignal();
    void flushFft();
    void printFFT();

    int arrayIndex;
    int fftBandArray[200];

    unsigned long previousMillis;
    unsigned long currentMillis;

    AudioProcessor();
};

#endif

class_audioProcessor.cpp:
Code:
#include "Arduino.h"
#include "class_audioProcessor.h"

// GUItool: begin automatically generated code
AudioSynthWaveformSine   sine1;          //xy=257,311
AudioInputI2S            i2s2;           //xy=338,197
AudioMixer4              mixer2;         //xy=482,313
AudioMixer4              mixer1;         //xy=491,187
AudioAnalyzeFFT1024      fft1024_1;      //xy=653,149
AudioOutputI2S           i2s1;           //xy=662,290
AudioControlSGTL5000     sgtl5000;     //xy=340,58
AudioConnection          patchCord1(sine1, 0, mixer2, 0);
AudioConnection          patchCord2(sine1, 0, mixer2, 1);
AudioConnection          patchCord3(i2s2, 0, mixer1, 0);
AudioConnection          patchCord4(i2s2, 1, mixer1, 1);
AudioConnection          patchCord5(mixer2, 0, i2s1, 0);
AudioConnection          patchCord6(mixer2, 0, i2s1, 1);
AudioConnection          patchCord7(mixer1, fft1024_1);
// GUItool: end automatically generated code

AudioProcessor::AudioProcessor() {
  // Construct everything needed for Teensy audio library
  AudioMemory(10);
  sgtl5000.enable();
  sgtl5000.volume(0.5);
  sgtl5000.inputSelect(AUDIO_INPUT_MIC);
  sgtl5000.micGain(90);

  sine1.amplitude(0.5);

  int mixerChannelCount = 4;
  for (int i = 0; i < mixerChannelCount; i ++) {
    mixer1.gain(i,0.75);
    mixer2.gain(i,0.75);
  }
  arrayIndex = 0;
  previousMillis = millis();
}
void AudioProcessor::reset() {
  flushFft();
  int mixerChannelCount = 4;
  for (int i = 0; i < mixerChannelCount; i ++) {
    mixer1.gain(i,0.75);
    mixer2.gain(i,0.75);
  }
  previousMillis = millis();

  Serial.println("AUDIOPROCESSOR reset");
}
void AudioProcessor::printFFT() {
  if (fft1024_1.available()) {
    // each time new FFT data is available
    // print it all to the Arduino Serial Monitor
    Serial.print("FFT: ");
    for (int i=0; i<40; i++) {
      float n = fft1024_1.read(i);
      if (n >= 0.01) {
        Serial.print(n);
        Serial.print(" ");
      } else {
        Serial.print("  -  "); // don't print "0.00"
      }
    }
    Serial.println();
  }
}
void AudioProcessor::setup() {

};
void AudioProcessor::testTone(char _c) {
  if (_c == 'r') {
    sine1.frequency(1000);
    toggleMixer(2,0.75);
    delay(200);
    sine1.frequency(2000);
    toggleMixer(2,0.75);
    delay(200);
    sine1.frequency(3000);
    toggleMixer(2,0.75);
    delay(200);
    stopTone();
  } else if (_c == 's') {
    sine1.frequency(5000);
    toggleMixer(2,0.75);
    delay(200);
    sine1.frequency(4000);
    toggleMixer(2,0.75);
    delay(200);
    sine1.frequency(3000);
    toggleMixer(2,0.75);
    delay(200);
    stopTone();
  }
};
void AudioProcessor::sendTone(int _frequency) {
  sine1.frequency(_frequency);
  toggleMixer(2,0.75);
};
void AudioProcessor::stopTone() {
  mixer1.gain(0,0.0);
  mixer1.gain(1,0.0);
  mixer1.gain(2,0.0);
  mixer1.gain(3,0.0);

  mixer2.gain(0,0.0);
  mixer2.gain(1,0.0);
  mixer2.gain(2,0.0);
  mixer2.gain(3,0.0);
};
int AudioProcessor::getHighestFFTBand(int _beginBand, int _endBand) {
  int highestBand = 0;
  float highestAmplitude = 0;

        for (int i = _beginBand; i < _endBand; i++) {
          float fftNumber = fft1024_1.read(i);
          //Serial.println(fftNumber);

          if (fftNumber > highestAmplitude) {
            highestAmplitude = fftNumber;
            highestBand = i;
          }
        }
        Serial.print("Current highest FFT band: ");
        Serial.println(highestBand);
        return highestBand;
}
void AudioProcessor::saveSignal() {
  currentMillis = millis();
  unsigned long elapsedTime = currentMillis - previousMillis;
  // Listen for 1 second every 5 milliseconds
  if (elapsedTime > 5) {

    toggleMixer(1,0.75);
    fftBandArray[arrayIndex] = getHighestFFTBand(0,255);

    if (arrayIndex < 200) {
      arrayIndex += 1;
      previousMillis = millis();
    } else {
      arrayIndex = 0;
      previousMillis = millis();
    }
  }
}
int AudioProcessor::getSignal() {
  //Serial.println(fftBandArray[123]);
  unsigned int occurNumber = 0;
  unsigned int index;
  unsigned int currentOccurNumber = 0;

  for (int i = 0; i < 200; i++) {
    for (int j = 0; j < 200; j++) {
      if (fftBandArray[i] > 0) {
        if (fftBandArray[i] == fftBandArray[j]) {

          currentOccurNumber += 1;
        }
      }
    }
    if (currentOccurNumber > occurNumber) {
      occurNumber = currentOccurNumber;
      index = i;
    }
  }
  Serial.print("most occurring fft number:");
  Serial.println(fftBandArray[index]);

  return fftBandArray[index];

}
void AudioProcessor::toggleMixer(int _mixerNumber, float _volume) {
  if (_mixerNumber == 1) {
    mixer1.gain(0,_volume);
    mixer1.gain(1,_volume);
  } else {
    mixer2.gain(0,_volume);
    mixer2.gain(1,_volume);
  }
}
void AudioProcessor::flushFft() {
  for (int i = 0; i < 200; i++){
    fftBandArray[i] = 0;
  }
}
 
So after posting this and fiddling around for some time more I managed to fix the problem. Somehow it seems the audio library class instances have to be initiated in the main loop. As I still wanted to be able to use my audioProcessor class (both because the code was working & for organising functions) I decided to add a setup class and pass pointers to the class instances made for the audio library in the main loop. This way, there are no double declarations, and classes are still defined by the main loop. But I do have the ability to use them in an embedded class. Hope this helps out someone else if ever ran into the same problem.

in void setup():
Code:
receiver.audioProcessor.setup(&sgtl5000,&sine1,&mixer1,&mixer2,&fft1024_1);
sender.audioProcessor.setup(&sgtl5000,&sine1,&mixer1,&mixer2,&fft1024_1);

setup function of the audioProcessor:
Code:
void AudioProcessor::setup(AudioControlSGTL5000* _sgtl5000,AudioSynthWaveformSine* _sine, AudioMixer4* _mixer1, AudioMixer4* _mixer2,AudioAnalyzeFFT1024* _fft1024) {

  sgtl5000 = _sgtl5000;
  sine1 = _sine;
  mixer1 = _mixer1;
  mixer2 = _mixer2;
  fft1024_1 = _fft1024;

  AudioMemory(10);
  sgtl5000->enable();
  sgtl5000->volume(0.5);
  sgtl5000->inputSelect(AUDIO_INPUT_MIC);
  sgtl5000->micGain(90);
  sine1->amplitude(0.5);

  int mixerChannelCount = 4;
  for (int i = 0; i < mixerChannelCount; i ++) {
    mixer1->gain(i,0.75);
    mixer2->gain(i,0.75);
  }
  arrayIndex = 0;
  previousMillis = millis();
}
 
Status
Not open for further replies.
Back
Top