H
I'm having a problem for a while but I am not able to find a solution. The problem is that when I try to animate the led strip (WS2812B) while playing a WAV file at the same time not all LEDs seem to respond to the program I wrote. The first 80 LEDs seem to do just fine while animating the strip, but the last 20 or so seem glitch and all turn on at the same time instead of animating up one by one. The same problem is when animating down, but then the first 20 LEDs seem to turn off at the same time and then the rest of the strip seems to do just what I programmed. I am using a teensy 3.2, teensy's micro SD card adaptor and a propshield where I have connected the LEDs to the LED driver and a 2W 8Ω speaker to the audio amplifier. I have run both codes individually and they seem to work perfectly, but when combined the led strip glitches.
If anyone could help me out that'd be great!
This is the code I used:
I'm having a problem for a while but I am not able to find a solution. The problem is that when I try to animate the led strip (WS2812B) while playing a WAV file at the same time not all LEDs seem to respond to the program I wrote. The first 80 LEDs seem to do just fine while animating the strip, but the last 20 or so seem glitch and all turn on at the same time instead of animating up one by one. The same problem is when animating down, but then the first 20 LEDs seem to turn off at the same time and then the rest of the strip seems to do just what I programmed. I am using a teensy 3.2, teensy's micro SD card adaptor and a propshield where I have connected the LEDs to the LED driver and a 2W 8Ω speaker to the audio amplifier. I have run both codes individually and they seem to work perfectly, but when combined the led strip glitches.
If anyone could help me out that'd be great!
This is the code I used:
Code:
#include <FastLED.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// GUItool: begin automatically generated code
//AudioPlaySdWav playSdWavHum; //xy=270,91.00000381469727// I will add this later
AudioPlaySdWav playSdWavOnOff; //xy=272,40.00000190734863
AudioMixer4 mixer1; //xy=621.0000076293945,98.00000190734863
AudioOutputAnalog dac1; //xy=791,95
//AudioConnection patchCord1(playSdWavHum, 0, mixer1, 1);// I will add this later
AudioConnection patchCord2(playSdWavOnOff, 0, mixer1, 0);
AudioConnection patchCord5(mixer1, dac1);
// GUItool: end automatically generated code
const int buttonPin = 17;
const int ledPin = 13;
const int pressLengthMin = 50;
const int pressLength1 = 500;
const int pressLength2 = 2000;
unsigned long previousMillis = 0;
int buttonState = 0;
int millisTimer1;
int millisTimer2;
bool buttonClicked = false;
bool saberOn = false;
#define SDCARD_CS_PIN 10
#define SDCARD_MOSI_PIN 11
#define SDCARD_SCK_PIN 13
#define DATA_PIN 20
#define NUM_LEDS 99
CRGB leds[NUM_LEDS];
void setup() {
Serial.begin(9600);
Serial.println("HELLO THERE!");
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
FastLED.setBrightness( 50 );
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
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);
}
}
AudioMemory(20);
dac1.analogReference(INTERNAL);
delay(50); // time for DAC voltage stable
pinMode(5, OUTPUT);
digitalWrite(5, HIGH); // turn on the amplifier
delay(10); // allow time to wake up
mixer1.gain(0, 1.0f); //on off sounds
//mixer1.gain(1, 0.5f); // I will add this later
pinMode(7, OUTPUT);
digitalWrite(7, HIGH);
}
void loop() {
buttonTimer();
}
void buttonTimer() {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW && buttonClicked == false) {
millisTimer1 = millis();
buttonClicked = true;
} else if (buttonState == HIGH && buttonClicked == true) {
millisTimer2 = millis();
buttonClicked = false;
}
buttonCall();
}
void buttonCall() {
if (millisTimer2 - millisTimer1 > pressLengthMin && millisTimer2 - millisTimer1 <= pressLength1 && saberOn == false){
saberOnCall();
millisTimer1 = 0;
millisTimer2 = 0;
}else if (millisTimer2 - millisTimer1 > pressLengthMin && millisTimer2 - millisTimer1 <= pressLength1 && saberOn == true){
saberOffCall();
millisTimer1 = 0;
millisTimer2 = 0;
}
}
void saberOnCall() {
playSdWavOnOff.play("in01.wav");
for(int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB(255,0,0);
FastLED.show();
delay(3);
}
saberOn = true;
}
void saberOffCall() {
saberOn = false;
playSdWavOnOff.play("in02.wav");
for (int i=NUM_LEDS; i>=0; i--){
leds[i]=CRGB(0, 0, 0);
FastLED.show();
delay(3);
}
}