Hello everyone,
I'm hoping you all can help me with a problem I've been struggling with for the past couple of days. I'm trying to play a series of RAW files from the flash memory of the Prop Shield while driving a string of DOTSTAR LEDs. The code runs long enough for the first audio clip to play (.3s) and then hangs. While the audio is playing, the LEDs animate fine; then as it switches to the next file teensy hangs and windows makes a sound like Teensy has been unplugged. I read this thread on github, FastLED Issues 339 , and saw that the fix suggested there was being distributed with Teensyduino 1.30, so I took the opportunity to upgrade. It didn't affect the behavior. In the code below, if I comment out all of the lines associated with FastLED, the audio works correctly--and vice versa. I'm not sure what I'm missing--it seems like a problem with the SPI transaction, but I've gone as far as I can go. Any ideas? Thanks.
Here is my set up:
Arduino 1.6.11 and Teensyduino 1.30
FastLED v. 3.001.001
Teensy 3.2
Propshield LC w/ a speaker and
144 Dotstar Leds
I'm hoping you all can help me with a problem I've been struggling with for the past couple of days. I'm trying to play a series of RAW files from the flash memory of the Prop Shield while driving a string of DOTSTAR LEDs. The code runs long enough for the first audio clip to play (.3s) and then hangs. While the audio is playing, the LEDs animate fine; then as it switches to the next file teensy hangs and windows makes a sound like Teensy has been unplugged. I read this thread on github, FastLED Issues 339 , and saw that the fix suggested there was being distributed with Teensyduino 1.30, so I took the opportunity to upgrade. It didn't affect the behavior. In the code below, if I comment out all of the lines associated with FastLED, the audio works correctly--and vice versa. I'm not sure what I'm missing--it seems like a problem with the SPI transaction, but I've gone as far as I can go. Any ideas? Thanks.
Here is my set up:
Arduino 1.6.11 and Teensyduino 1.30
FastLED v. 3.001.001
Teensy 3.2
Propshield LC w/ a speaker and
144 Dotstar Leds
Code:
// Converted from the WavFilePlay from the Teensy release:
// hardware/teensy/avr/libraries/Audio/examples/WavFilePlayer/WavFilePlayer.ino
//
// Simple RAW file player example for the prop shield to use the Analog DAC
// and prop shield amplifier to play mono sounds.
// http://www.pjrc.com/teensy/gui/?info=AudioOutputAnalog
//
// On the prop shield, pin 6 selects the serial flash memory controller,
// and pin 5 enables the amplifier.
//
//Added code to test usage with FASTLED
//
// This example code is in the public domain.
//
//#define FASTLED_ALLOW_INTERRUPTS 0
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <FastLED.h>
//// GUItool: begin automatically generated code
AudioPlaySerialflashRaw playFlashRaw1; //xy=149,388
AudioMixer4 mixer1; //xy=445,386
AudioOutputAnalog dac1; //xy=591,379
AudioConnection patchCord1(playFlashRaw1, 0, mixer1, 0);
AudioConnection patchCord2(mixer1, dac1);
//// GUItool: end automatically generated code
//Audio Definitions
#define PROP_AMP_ENABLE 5
#define FLASH_CHIP_SELECT 6
#define VOLUME_POT A1
//LED Definitions
#define NUM_LEDS 144
#define DATA_PIN 11
#define CLOCK_PIN 13
#define FRAMERATE 300
//LED OBJECT and supporting variables
CRGB leds[NUM_LEDS];
const int ledDelay = (1000/FRAMERATE);
elapsedMillis frame = 0;
uint8_t gHue = 0; // rotating "base color" used by many of the patterns (from DemoReel100 example)
void setup() {
Serial.begin(9600);
// wait up to 3 seconds for the Serial device to become available
long unsigned debug_start = millis ();
while (!Serial && ((millis () - debug_start) <= 3000))
;
//Setup of LEDS
pinMode(7,OUTPUT); //LED ENABLE
digitalWrite(7,LOW);
FastLED.addLeds<DOTSTAR, DATA_PIN, CLOCK_PIN, RGB,DATA_RATE_MHZ(12)>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
LEDS.setBrightness(200);
delay(10);
Serial.println ("Start prop shield RAW player");
// Enable the amplifier on the prop shield
dac1.analogReference(EXTERNAL); // much louder!
pinMode(PROP_AMP_ENABLE, OUTPUT);
digitalWrite(PROP_AMP_ENABLE, HIGH);
// Audio connections require memory to work. For more
// detailed information, see the MemoryAndCpuUsage example
AudioMemory(60);
// Set initial volume
mixer1.gain(0, 1.0f);
// uncomment these lines if you have a potentiometer or trimpot
// on the pin A1 to control the volume, and comment out the
// above line
//float vol = analogRead(VOLUME_POT);
//mixer1.gain(0, vol / 1024.0f);
// Start SerialFlash
if (!SerialFlash.begin(FLASH_CHIP_SELECT)) {
while (1)
{
Serial.println ("Cannot access SPI Flash chip");
delay (1000);
}
}
}
int filenumber = 0; // while file to play
const char * filelist[7] = {
"s1.raw", "s2.raw", "s3.raw", "c1.raw", "m1.raw", "s4.raw", "b1.raw"
};
void playFile(const char *filename)
{
Serial.print("Playing file: ");
Serial.println(filename);
// Start playing the file. This sketch continues to
// run while the file plays.
playFlashRaw1.play(filename);
// A brief delay for the library read RAW info
delay(5);
// Simply wait for the file to finish playing.
while (playFlashRaw1.isPlaying()) {
// uncomment these lines if you have a potentiometer or trimpot
// on the pin A1 to control the volume
//float vol = analogRead(VOLUME_POT);
//mixer1.gain(0, vol / 1024.0f);
}
}
void playFromArray() {
if (!playFlashRaw1.isPlaying()) {
const char *filename = filelist[filenumber];
filenumber = filenumber + 1;
if (filenumber >= 7) filenumber = 0;
Serial.print("Start playing ");
Serial.println(filename);
playFlashRaw1.play(filename);
delay(30); // wait for library to parse WAV info
}
}
void ledUpdate(){
// beginTransaction prevents SPI bus conflicts
SPI.beginTransaction(SPISettings(12000000, MSBFIRST, SPI_MODE0));
digitalWrite(7, HIGH); // enable access to LEDs
FastLED.show();
digitalWrite(7, LOW);
//delayMicroseconds(5);
SPI.endTransaction(); // allow other libs to use SPI again
}
void sinelon()
{
// a colored dot sweeping back and forth, with fading trails
fadeToBlackBy( leds, NUM_LEDS, 20);
int pos = beatsin16(50,0,NUM_LEDS);
leds[pos] += CHSV( 96, 255, 192);
ledUpdate();
}
void loop() {
playFromArray();
if ( frame > ledDelay){
frame = 0;
sinelon();
}
}