0

Playing a sound is interfering with led blink
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Playing a sound is interfering with led blink

by chipnod2020 on Thu Jan 14, 2021 4:26 pm

I currently have an Arduino Nano running the code below with a Adafruit Audio FX Sound Board + 2x2W Amp.

I have created and collaborated on 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)

Code: Select all | TOGGLE FULL SIZE
// 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
  }
}

chipnod2020
 
Posts: 17
Joined: Wed Jul 15, 2020 9:59 am

Re: Playing a sound is interfering with led blink

by mikeysklar on Fri Jan 15, 2021 3:13 pm

Cool Project.

I'm wondering if you might need to switch over to out an audio board that supports background playback and interrupts so you can have more control of playing these sounds and not have to rely on loops that can interrupt your LED patterns.

https://www.adafruit.com/product/3436

Your serial question is easier. Yes, you can use RX only and when setting up software serial just give it a bogus GPIO pin number for the TX line like '99'.

mikeysklar
 
Posts: 3415
Joined: Mon Aug 01, 2016 8:10 pm

Re: Playing a sound is interfering with led blink

by chipnod2020 on Fri Jan 15, 2021 3:53 pm

mikeysklar wrote:I'm wondering if you might need to switch over to out an audio board that supports background playback and interrupts


Can one still have serial communications with this 3W board? I cannot see anything that suggests that. I do like that it would be louder as the 2W is just a tad low.

I had simply though of triggering the sound via the same switch used to trigger the LEDs and just short the pins. I do not like the thought of adding more wires though.

chipnod2020
 
Posts: 17
Joined: Wed Jul 15, 2020 9:59 am

Re: Playing a sound is interfering with led blink

by mikeysklar on Fri Jan 15, 2021 4:27 pm

@chipnod2020,

Depending on your goals the 3W might make more sense. This is a stackable Featherwing module so no wires need to be run if you use a compatible Feather and drop the Nano. I would use at least a Feather M0 Express which will also give a huge speed increase and memory bump.

https://www.adafruit.com/feather

mikeysklar
 
Posts: 3415
Joined: Mon Aug 01, 2016 8:10 pm

Re: Playing a sound is interfering with led blink

by chipnod2020 on Tue Feb 02, 2021 11:42 pm

So I have been researching quite a bit on this topic. I have purchased a music maker 3w feather.

What i am really struggling with is to find an example or tutorials on how to control what is played on the mm feather via a piece of code in a microcontroller. For example, I press the button on pin 1. The code instructs the mm to play track001.mp3.

Any suggestions?

chipnod2020
 
Posts: 17
Joined: Wed Jul 15, 2020 9:59 am

Re: Playing a sound is interfering with led blink

by mikeysklar on Wed Feb 03, 2021 4:17 pm

@chipnod2020,

Sure three chunks of code come to mind:

1) Serial control of the Music Maker FeatherWing:

https://github.com/adafruit/Adafruit_VS ... player.ino

2) GPIO test for the player:

https://github.com/adafruit/Adafruit_VS ... iotest.ino

3) interrupts with player:

https://github.com/adafruit/Adafruit_VS ... rrupts.ino

mikeysklar
 
Posts: 3415
Joined: Mon Aug 01, 2016 8:10 pm

Re: Playing a sound is interfering with led blink

by chipnod2020 on Wed Feb 03, 2021 4:26 pm

AWESOME! Thank You! I'll look these over.

chipnod2020
 
Posts: 17
Joined: Wed Jul 15, 2020 9:59 am

Please be positive and constructive with your questions and comments.