Teensy hangs up after few manipulations with LEDs

Status
Not open for further replies.

skaurus

Active member
Hi!

I have a Teensy 3.2 with soldered Audio board (Teensy is atop); other connections I have:
  • to MIC and GND pins of audio board soldered pins, then wires, then audio jack
  • to 7 and 13 pins on the bottom of the Audio board soldered wires which lead to data input and clock input pads of APA102 LED
  • behind first LED there are few more, attached sequentially

All this lies just on my table or my lap. Some wire connections are isolated with duct tape. Some not so well I guess.

Teensy takes power from usb cable. LEDs take power from 5v and gnd wires of another usb cable. Both usb cables are plugged into MacBook.

Test code that I have:
Code:
#include "FastLED.h"

// How many leds in your strip?
#define NUM_LEDS 5

// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN.  For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806 define both DATA_PIN and CLOCK_PIN
#define DATA_PIN 7
#define CLOCK_PIN 14

// Define the array of leds
CRGB leds[NUM_LEDS];

void setup() {
    Serial.begin(9600);
    FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN, BGR>(leds, NUM_LEDS);
}

void loop() { 
    for (var led = NUM_LEDS - 1; led >= 0; led--) {
        // Turn the LED on, then pause
        leds[0] = CRGB::Red;
        Serial.println("Red");
        FastLED.show();
        delay(5000);

        leds[0] = CRGB::Green;
        Serial.println("Green");
        FastLED.show();
        delay(5000);

        leds[0] = CRGB::Blue;
        Serial.println("Blue");
        FastLED.show();
        delay(5000);

        // Now turn the LED off, then pause
        leds[0] = CRGB::Black;
        Serial.println("Black");
        FastLED.show();
        delay(500);
    }
}

Now to my problem. First, loop in my program may run fine for say 5 iterations and then it's frozed. Some LED just stays lighted and no Serial data coming anymore.
Second, this may be related to the first problem. Quite often (say in 25% cases) uploading code to Teensy says that I need to press pushbutton. Per my understanding this should not happen. Few times Teensyloader said me that auto mode was disabled because Teensy tried to reload too often (or something like this, I haven't had sleep this night and may mess up details).

I'm not really sure as I didn't extensive testing but this hang ups probably weren't happening while I tested Teensy + Audio board in Audio tutorials, without attached LEDs.

How do I debug this case? What is the most probable causes?
I hope this bug could be easily squashed...
 
Forgot to mention that I have 220uF capacitor in between 5V and ground in LED power line (will replace it to 1500uF today) and 440 Ohm resistor (220+220 actually) in between pin 7 and LED data pad. Took it from Adafruit NeoPixels uberguide, "Powering NeoPixels" section. I guess it is more or less the same for any LEDs.
 
I'm assuming that you don't have an SD card plugged into this, since that would also be trying to work on those pins.

The symptoms sound like a brown out/excess current condition but your explanation indicates you have <40 LEDs and are using code that only actually lights the first LED so in theory things should be nice and stable. If you see more than the first LED lighting then are having noise on the SPI signal, which since you don't mention a level converter anywhere there may just be due to the fact you are driving 5V LEDs with a 3.3V signal. A basic test here if you are using a very short strand may be to series a decent sized diode on the 5V power just to the LEDs and see how they go running off ~4.5 volts. This makes the 3.3V logic higher with respect to the power supply and more likely to reach a clear 'HIGH' condition. For the final project you will need to look at a proper level conversion solution such as that built into the prop shield.
 
Hi!

I do not have a SD card indeed and have currently 9 leds.
Teensy and leds are powered from two different battery holders which have 4.5 and 4.8V voltage.
Clock and data pins are connected directly to leds without the logic converter. So it is likely my problem? Clock and data pins voltage should be close to power voltage?
 
Yeah, I have found in Adafruit Dotstar (their name for apa102) guide that data and clock should be 5v too.
Thank you very much for pointing this out!
 
Last edited:
I was able to drive leds from 3.6V, but my setup was hanging up anyway.
I am at music festival and there is no logic shifter around, BUT forcing software SPI fixed the problem!

All good but now I have different one - everything working when Teensy powered from notebook, but from 4.8V battery holder (connected to pins Vin and GND) it doesn't :(
Led on teensy is blinking, but my external leds are just in some fixed state. Except teensy power source nothing has changed.
Any ideas?

Main night is two days away :D
 
If you have another battery, maybe try running the Teensy from a different battery than the LEDs? Not an ideal solution, but if you're out in the field and under a tight deadline, best to get to something that just works!
 
I discovered that when I connect my battery-powered Teensy to Mac with usb cable WITH CUT RED WIRE, it works fine.

I commented out any calls to Serial and don't have infinite loop in setup() waiting for it.
It is hard to post my code right now, sorry.

Any fast ideas?
 
This is my code.

Code:
#include <Audio.h>
// GUItool: begin automatically generated code
AudioInputI2S            i2s1;           //xy=136,186
AudioAnalyzeFFT1024      fft1024_1;      //xy=364,229
AudioOutputI2S           i2s2;           //xy=425,139
AudioConnection          patchCord1(i2s1, 0, i2s2, 0);
AudioConnection          patchCord2(i2s1, 0, i2s2, 1);
AudioConnection          patchCord3(i2s1, 0, fft1024_1, 0);
AudioControlSGTL5000     sgtl5000_1;     //xy=235,338
// GUItool: end automatically generated code

#define FASTLED_FORCE_SOFTWARE_SPI
#include "FastLED.h"

#define NUM_LEDS 9

#define DATA_PIN 7
#define CLOCK_PIN 14

//#define DEBUG true
//#define SERIAL false

// Define the array of leds
CRGB leds[NUM_LEDS];

void setup() {
  //delay(3000); // power-up safety delay
  //reset_leds();
  FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN, BGR>(leds, NUM_LEDS);

  //if (SERIAL) Serial.begin(9600);

  AudioMemory(10);
  sgtl5000_1.enable();
  //sgtl5000_1.volume(0.5);
  sgtl5000_1.inputSelect(AUDIO_INPUT_MIC);
  sgtl5000_1.micGain(45); // 0 to 63
  // Uncomment one of these to try other window functions
  fft1024_1.windowFunction(NULL);
  //fft1024_1.windowFunction(AudioWindowBartlett1024);
  //fft1024_1.windowFunction(AudioWindowFlattop1024);
  delay(1000);
}

void test_all_leds_seq() {
  for (int led = NUM_LEDS - 1; led >= 0; led--) {
    //if (SERIAL && DEBUG) Serial.println(led);

    // Turn the LED on, then pause
    leds[led] = CRGB::Red;
    //if (SERIAL && DEBUG) Serial.println("Red");
    FastLED.show();
    delay(2000);

    leds[led] = CRGB::Green;
    //if (SERIAL && DEBUG) Serial.println("Green");
    FastLED.show();
    delay(2000);

    leds[led] = CRGB::Blue;
    //if (SERIAL && DEBUG) Serial.println("Blue");
    FastLED.show();
    delay(2000);
    
    // Now turn the LED off, then pause
    leds[led] = CRGB::Black;
    //if (SERIAL && DEBUG) Serial.println("Black");
    FastLED.show();
    delay(2000);
  }
}

void test_all_leds_par() {
  for (int led = NUM_LEDS - 1; led >= 0; led--) {
    // Turn the LED on, then pause
    leds[led] = CRGB::Red;
    //if (SERIAL && DEBUG) Serial.println("Red");
    FastLED.show();
  }
  delay(2000);

  for (int led = NUM_LEDS - 1; led >= 0; led--) {
    leds[led] = CRGB::Green;
    //if (SERIAL && DEBUG) Serial.println("Green");
    FastLED.show();
  }
  delay(2000);

  for (int led = NUM_LEDS - 1; led >= 0; led--) {
    leds[led] = CRGB::Blue;
    //if (SERIAL && DEBUG) Serial.println("Blue");
    FastLED.show();
  }
  delay(2000);

  for (int led = NUM_LEDS - 1; led >= 0; led--) {
    // Now turn the LED off, then pause
    leds[led] = CRGB::Black;
    //if (SERIAL && DEBUG) Serial.println("Black");
    FastLED.show();
  }
  delay(2000);
}

void _print_number(float n) {
  if (n >= 0.004) {
    //Serial.print(n, 3);
    //Serial.print(" ");
  } else {
    //Serial.print("   -  "); // don't print "0.00"
  }
}

#define BRIGHTNESS_BINS 7 // first 7*43Hz = 301Hz
#define MAX_BIN 512
// we are very unlikely to accumulate ~ 100% of all volume in one led and because of this we'll never see some colors
// so I'll power up things a bit
#define POWER_UP 1.2
void sound_react() {
  static elapsedMillis runtime_counter;
  static int zeroLedHue = 0;
  if (fft1024_1.available()) {
    float bassShare = 0.0, ledShare = 0.0;
    int currentLed = 0, nextLed = 0;
    for (int bin = 0; bin < MAX_BIN; bin++) {
      float v = fft1024_1.read(bin);
      if (bin < BRIGHTNESS_BINS) {
        bassShare += v;
      } else if (bin == BRIGHTNESS_BINS) {
        float brightness = bassShare * 255 * POWER_UP;
        //if (SERIAL && DEBUG) { Serial.print("Brigthness: "); Serial.println(brightness); }
        FastLED.setBrightness(brightness);
      }
      if (bin >= BRIGHTNESS_BINS) {
        ledShare += v;
        nextLed = map(bin, BRIGHTNESS_BINS, MAX_BIN, 1, NUM_LEDS);
        //if (SERIAL && DEBUG) { Serial.print(bin); Serial.print(" "); Serial.print(nextLed); Serial.println(); }
        // second condition is for the last led
        if (nextLed != currentLed || bin == MAX_BIN - 1) {
          float hue = ledShare * 255 * POWER_UP;
          leds[currentLed].setHue(hue); // saturation and value is 255
          //if (SERIAL && DEBUG) { Serial.print("Led "); Serial.print(currentLed); Serial.print(" hue: "); Serial.println(hue); }
          ledShare = 0.0;
          currentLed = nextLed;
        }
      }
      //if (SERIAL && DEBUG) _print_number(v);
    }
    //if (DEBUG) Serial.println();
    // zero (debug) led handling
    if (runtime_counter > 40) {
      zeroLedHue++;
      if (zeroLedHue > 255) zeroLedHue = 0;
      runtime_counter = 0;
    }
    leds[0].setHue(zeroLedHue);
    leds[0].maximizeBrightness();
  }
  // let's begin the show!
  FastLED.show();
}

void loop() {
  //test_all_leds_par();
  sound_react();
}

It works with usb cable no matter when it is inserted - before or after boot from batteries. I can remove cable during work - leds will stop changing. Then I can plug it back and leds will resume.
Cable must be connected to pc, it will not work plugged only on one end to Teensy or connected to Teensy and power bank.
 
Last edited:
I am having very similar problems (and I don't think it has anything to do with power). I suspect the FastLED SPI implementation.

Have you tried non-SPI pins?
 
Have you tried non-SPI pins?

Not sure which of my problems you are having, but try forcing software SPI implementation in FastLED. Using non-SPI pins should give same results.
 
This is my code.

It works with usb cable no matter when it is inserted - before or after boot from batteries. I can remove cable during work - leds will stop changing. Then I can plug it back and leds will resume.
Cable must be connected to pc, it will not work plugged only on one end to Teensy or connected to Teensy and power bank.

Have you modified your copy of the Audio library? Added Serial.print statements in there at all?

It certainly sounds like the Serial buffer is filled and blocking the Teensy from continuing normal operation..
 
The latest version of FastLED.h freezes in SPI mode

In maybe a week I'll try to downgrade my FastLED version. Maybe it will resolve problem with required usb connection. Don't know how but maybe)
 
FastLed library has a solution to this too.. and have a work around to use now. You were right, it is related to the USB connection.
Thanks, I wouldn't have stumbled upon this by myself probably.
Funny thing is that my program worked only WITH usb connection. Also I already have software SPI.
Still haven't looked further in my issue... In fact my Teensy is probably damaged anyway and won't allow to reprogram it %-]
 
I'm preparing to post Teensyuino 1.30-beta4. I've pulled in the latest FastLED branch with bug #339 fixed.

Could really use some feedback from anyone really using FastLED for a project. If you have a little time, please give 1.30-beta4 a try and let me know if the bundled copy of FastLED works for you? It'd be really nice to have a stable, working FastLED in the 1.30 release.
 
Hi Paul, have been trying to use Fastled with teensy 3.6 with indifferent results, is your version of fastLED in 1.30 working with SPI1 and/or the higher pin numbers if using the software SPI? built a project using clock on pin 32 and am currently using adafruits software only library because I haven't dug deep enough in fastLED to pin down what it's up to. SPI1 hardware not working wasn't a surprise at this point but softSPI not liking pin numbers somewhere above 12 was.
 
Status
Not open for further replies.
Back
Top