Playing a sound is interfering with led blink

I have created a script for a Star Trek model I am working on. I'm trying to tie blinking LEDs with a sound file when a button is pressed. All is working as planned with the exception of when I call the sound file, the LEDs do not act as planned and blink at a weird interval. If I remove the sound call (sfx.playTrack), the LEDs go back to expected behavior. Please zero in on the // Pulse Cannons sections. I'm sure that since this is within the loop, it is causing an issue with the execution of the led pulses.

2 questions I need help with please

  • What is the best way to call sound over serial in code without interfering with blinking leds? (Line 108 in my code)
  • Can one setup the Software Serial section to only use one variable? For example, if I don't want to use a TX or RST option, can I setup RX only? (Line 49 in my code)
// Notes
// - All time is in milliseconds

// Global Variables and library loads
unsigned long now;    //time from millis()
#include <ezButton.h> // ezButton library
#include <ezOutput.h> // ezOutput library

// Navigation Lights - NAV prefix
#define NAV_LED_PIN A0
#define NAV_LED_ON 150    
#define NAV_LED_OFF 1000
unsigned long NAV_msLast;  // last time the LED changed state
boolean NAV_ledState;    // current LED state

// Pulse Cannons - PC prefix
const int PC_BUTTON = 10; // The number of the pushbutton pin
const int PC_PIN = 11;  // The number of the LED pin
int PC_blinkF = 250; // How long between flashes
int PC_blinkT = 10;    // How many flashes do you want times 2: 5 flashes = 10
ezButton buttonPC(PC_BUTTON); // create ezButton object that attach to pin 13;
ezOutput led(PC_PIN);    // create ezOutput object that attach to pin 9;

// Torpedo Common Settings - TC prefix
int TC_COLOR1 = 3;  // the number of the LED pin
int TC_COLOR2 = 5;  // the number of the LED pin
#define TC_EFFECT_NONE 0
#define TC_EFFECT_BLAST 1
#define TC_EFFECT_FULL 2
#define TC_EFFECT_DIM  3
#define TC_TIME_X 500 // 300 ms. YOU CAN CHANGE IT
#define TC_TIME_Y 300  // 100 ms. YOU CAN CHANGE IT
#define TC_TIME_Z 350 // 100 ms. YOU CAN CHANGE IT
#define TC_BRIGHTNESS_A  150 // YOU CAN CHANGE IT
#define TC_BRIGHTNESS_FULL 255
#define TC_BRIGHTNESS_LOW 0
unsigned long TC_mslast;
unsigned int ledEffectState = TC_EFFECT_NONE;
int TC_pattern[3]; // Initialize the color pattern for torpedoes

// Photon Torpedoes - PT prefix
const int PT_BUTTON  = 8; // the number of the pushbutton pin
ezButton buttonPT(PT_BUTTON); // create ezButton object that attach to pin 7;

// Quantum Torpedoes - QT prefix
const int QT_BUTTON  = 9; // the number of the pushbutton pin
ezButton buttonQT(QT_BUTTON); // create ezButton object that attach to pin 7;

// Sound Board Settings
#include <SoftwareSerial.h>
#include "Adafruit_Soundboard.h"
#define SFX_RX 12
#define SFX_TX A7
#define SFX_RST A6
SoftwareSerial ss = SoftwareSerial(SFX_TX, SFX_RX);
Adafruit_Soundboard sfx = Adafruit_Soundboard(&ss, NULL, SFX_RST);

// Power Up
#define PU_TIME   3000 // 1000 ms. YOU CAN CHANGE IT
#define PU_START_BRIGHT 0    // YOU CAN CHANGE IT
#define PU_END_BRIGHT   150  // YOU CAN CHANGE IT
const int PU_PIN   = 6;    // the LED pin, change as you want
unsigned long PU_START_TIME;
unsigned long PU_PROGRESS;
unsigned long PU_BRIGHTNESS;

void setup(void)
{
  //Serial.begin(9600);
  ss.begin(9600);
  pinMode(NAV_LED_PIN, OUTPUT);
  pinMode(TC_COLOR1, OUTPUT);
  pinMode(TC_COLOR2, OUTPUT);
  buttonPC.setDebounceTime(50); // set debounce time to 50 milliseconds
  buttonQT.setDebounceTime(50); // set debounce time to 50 milliseconds
  buttonPT.setDebounceTime(50); // set debounce time to 50 milliseconds

  // Starship Power up
  delay(3000);
  pinMode(PU_PIN, OUTPUT); // Add more pins here for more effects
  PU_START_TIME = millis();
  do {
    PU_PROGRESS = millis() - PU_START_TIME;
    PU_BRIGHTNESS = map(PU_PROGRESS, 0, PU_TIME, PU_START_BRIGHT, PU_END_BRIGHT);
    analogWrite(PU_PIN, PU_BRIGHTNESS);
  } while (PU_PROGRESS < PU_TIME);
}

void loop(void)
{
  // Normal Stuff
  now = millis();
  buttonPC.loop(); // MUST call the button.loop() function in loop()
  buttonQT.loop(); // MUST call the button.loop() function in loop()
  buttonPT.loop(); // MUST call the button.loop() function in loop()  
  led.loop();  // MUST call the led.loop() function in loop()
  
  // Navigation Lights
  if (now - NAV_msLast > (NAV_ledState ? NAV_LED_ON : NAV_LED_OFF)) {
    digitalWrite(NAV_LED_PIN, NAV_ledState = !NAV_ledState);
    NAV_msLast = now;
  }

  // Pulse Cannons
  if(buttonPC.isPressed())
  {
    //Serial.println("The pulse phaser button is pressed.");
    sfx.playTrack("TEST    WAV");
    led.blink(PC_blinkF, PC_blinkF, 0, PC_blinkT); // ON, OFF times. 0 means blink immediately, blink off/on X times
  }

  // Quantum Torpedoes Button
  if(buttonQT.isPressed()) {
    //Serial.println("The quantum torpedo button is pressed.");
    ledEffectState = TC_EFFECT_BLAST;
    TC_mslast = millis();
  TC_pattern[0] = TC_COLOR1;
  TC_pattern[1] = TC_COLOR1;
  TC_pattern[2] = TC_COLOR1;
  }

  // Photon Torpedoes Button
  if(buttonPT.isPressed()) {
    //Serial.println("The photon torpedo button is pressed.");
    ledEffectState = TC_EFFECT_BLAST;
    TC_mslast = millis();
  TC_pattern[0] = TC_COLOR2;
  TC_pattern[1] = TC_COLOR1;
  TC_pattern[2] = TC_COLOR2;
  }

  long PU_PROGRESS;
  long PU_BRIGHTNESS;

  switch(ledEffectState)
  {
    case TC_EFFECT_BLAST:
    PU_PROGRESS = millis() - TC_mslast;
    PU_BRIGHTNESS = map(PU_PROGRESS, 0, TC_TIME_X, TC_BRIGHTNESS_LOW, TC_BRIGHTNESS_A);

    if(PU_BRIGHTNESS <= TC_BRIGHTNESS_A) {
      analogWrite(TC_pattern[0], PU_BRIGHTNESS);
    } else {
      ledEffectState = TC_EFFECT_FULL;
      TC_mslast = millis();
      analogWrite(TC_pattern[0],  TC_BRIGHTNESS_LOW);  // led is OFF immediately
      analogWrite(TC_pattern[1], TC_BRIGHTNESS_FULL); // led is fult bright immediately
    }
    break;

    case TC_EFFECT_FULL:
    PU_PROGRESS = millis() - TC_mslast;
    if(PU_PROGRESS > TC_TIME_Y)
    {
      ledEffectState = TC_EFFECT_DIM;
      TC_mslast = millis();
      analogWrite(TC_pattern[1], TC_BRIGHTNESS_LOW);  // led is OFF immediately
    }
    break;

    case TC_EFFECT_DIM:
    PU_PROGRESS = millis() - TC_mslast;
    PU_BRIGHTNESS = map(PU_PROGRESS, 0, TC_TIME_Z, TC_BRIGHTNESS_A, TC_BRIGHTNESS_LOW);

    if(PU_BRIGHTNESS >= TC_BRIGHTNESS_LOW)
    analogWrite(TC_pattern[2], PU_BRIGHTNESS);
    else // led is OFF
    ledEffectState = TC_EFFECT_NONE; 
    break;

    default:
    digitalWrite(TC_pattern[0], LOW); // turn off LED
    digitalWrite(TC_pattern[1], LOW); // turn off LED
    digitalWrite(TC_pattern[2], LOW); // turn off LED
  } 
}

All I can think is that the soundboard is rather slow to respond to its serial commands - from
looking at the library it seems playTrack() shouldn't block for longer than it takes the soundboard
to respond to a serial command.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.