can't get speaker and led matrix to work at the same time - coding issue.

Hi, I’m new, please let me know if there’s a better section to ask this question…

I’ve got an issue with an alarm i’m trying to make. It’s using an arduino uno with a wave shield to play audio. Also hooked up to the Uno are two MAX7219 chips that are driving 2 8x8 LED matrix’s. I got the wave shield working, audio plays through my speakers just fine. I also have both LED matrix’s working perfectly. The only problem, they won’t work at the same time.

This is some example code I’m using below, look for the "*"s.

//#include <FatReader.h>
//#include <SdReader.h>
//#include <avr/pgmspace.h>
//#include "WaveUtil.h"
#include "WaveHC.h"
#include "LedControl.h"

SdReader card;    
FatVolume vol;    
FatReader root;   
FatReader f;      

WaveHC wave;

LedControl lc=LedControl(8,7,6);
LedControl lc2=LedControl(13,12,11);

void setup() {
  
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);

//*********************************************************  
// the following 3 if statements is the code giving me issues. The only way to get the LED matrix's working is to comment out these three lines, and the only way to get the speaker working is to leave these lines how they are. 
if (!card.init()) {return;}
if (!vol.init(card)) {return;}
if (!root.openRoot(vol)) {return;}
//*********************************************************
  
  lc.shutdown(0,false);
  lc.setIntensity(0,8);
  lc.clearDisplay(0);
  lc2.shutdown(0,false);
  lc2.setIntensity(0,8);
  lc2.clearDisplay(0);
}


void loop() { 

playcomplete("hp1.WAV");

lc.setLed(0,2,3,true);
lc2.setLed(0,4,5,true);

}

void playcomplete(char *name) { 
if (wave.isplaying) {wave.stop();}
if (!f.open(root, name)) {return;}  
if (!wave.create(f)) {return;}   
wave.play();
while (wave.isplaying) { }}

If that code is absolutely necessary, is there another way to get the LED matrix’s working?

This line at the end of your playcomplete function:

while (wave.isplaying) { }

literally says while the wave is playing do absolutely nothing. If you want your LED code to run while the wav is playing...

The reason it works when you comment out those lines is because the file doesn't get opened so this line in playcomplete:

if (!f.open(root, name)) {return;}

causes playcomplete to return without starting the wav or getting to that silly while loop so control returns to loop and your led code gets called.

When you're running the code you have, are you letting the wav play all the way through, or are you stopping it because the lights didn't light up? I'm betting that immediately after the wav ends the LEDs will start to do their thing.

I have "playcomplete("hp1.WAV");" in my loop so it's saying the audio over and over without stopping. I just tried moving "playcomplete("hp1.WAV");" to another function so it wouldn't keep repeating:

void loop() { 

temp();

lc.setLed(0,2,3,true);
lc2.setLed(0,4,5,true);

}

void temp() {

if (b == 1) {playcomplete("hp1.WAV"); }

b = 0;
return;
}

And b is set to 1 globally. So with that It only plays the audio one time then stops. Still no lights however.

It's not about making that function run once. Go read the first reply again. Does your playcomplete function still say to do nothing while the wav is playing. And it sounds like that is exactly what is happening, it is playing the wav and doing nothing while the wav is playing.

Maybe instead of doing nothing while the wav isPlaying, you could do the lights.

LedControl lc2=LedControl(13,12,11);

If you have this device connected to pins 11, 12, and 13, you are not going to be able to use SPI to read from/write to the SD card.

PaulS: LedControl lc2=LedControl(13,12,11);

If you have this device connected to pins 11, 12, and 13, you are not going to be able to use SPI to read from/write to the SD card.

True if this is written for an UNO. Not a problem for a MEGA. But the OP has chosen to keep the board they are using a secret, which really hinders anyone's ability to give help on anything other than the code itself.

Delta_G: True if this is written for an UNO. Not a problem for a MEGA. But the OP has chosen to keep the board they are using a secret, which really hinders anyone's ability to give help on anything other than the code itself.

From the original post:

It's using an arduino uno

Thanks for the reply’s guys…

//#include <FatReader.h>
//#include <SdReader.h>
//#include <avr/pgmspace.h>
//#include "WaveUtil.h"
#include "WaveHC.h"
#include "LedControl.h"

SdReader card;    
FatVolume vol;    
FatReader root;   
FatReader f;      

WaveHC wave;

LedControl lc=LedControl(13,12,11);

void setup() {
  
  
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);

if (!card.init()) {return;}
if (!vol.init(card)) {return;}
if (!root.openRoot(vol)) {return;}

 
  lc.shutdown(0,false);
  lc.setIntensity(0,8);
  lc.clearDisplay(0);
  //lc2.shutdown(0,false);
  //lc2.setIntensity(0,8);
  //lc2.clearDisplay(0);

}


void loop() { 

playcomplete("hp1.WAV");

}




void playcomplete(char *name) {

playfile(name);
while (wave.isplaying) {

delay(3000);
digitalWrite(7, HIGH);
lc.setLed(0,2,7,true); 
delay(3000);
digitalWrite(7, LOW);
lc.clearDisplay(0);

}}
 
void playfile(char *name) {

if (wave.isplaying) {wave.stop();}
if (!f.open(root, name)) {return;}
if (!wave.create(f)) {return;}
wave.play();
}

I updated my code so that the lights should be turned on from within the while (wave.isplaying) loop and I added a normal LED along with the 8x8 led matrix. Now when the audio is playing the LED lights up a small amount (should be bright…) and all the lights turn on on the led matrix instead of just 1…

I’m using an Uno.

If you have this device connected to pins 11, 12, and 13, you are not going to be able to use SPI to read from/write to the SD card.

I’m not sure I understand this, what is SPI and why can’t I use it if I’m using pins 11, 12 and 13? Should I be using 3 other pins instead?

what is SPI and why can't I use it if I'm using pins 11, 12 and 13? Should I be using 3 other pins instead?

SPI is the Serial Peripheral Interface. It is the process that the Arduino uses to talk to serial peripheral devices, like the Ethernet shield, the WiFi shield, and SD cards. It is hardwired to pins 11, 12, and 13. So, yes, you should be using 3 other pins, instead.

Unfortunately I'm not having much luck with that either. I've trying hooking LedControl lc=LedControl up to 3 different digital pins and even 3 analog pins but I'm not getting any lights to come on on the led matrix. The normal LED blinks fine while audio plays but it's a faint light.

You can't hook a command to a pin. You can hook an LED to a pin. You can send a pin number as an argument to a command. But you cannot hook words up to a pin. Please let us know what you mean here.

Can you post a schematic of what you have wired up?

Hope this helps- http://imgur.com/heoySCs

I have them in analog pins right now.

white (CS) goes to A5 green (CLK) goes to A4 orange (DIN) goes to A3 Red goes to v5 black to ground and the led is on A0 ignore that random black wire under my arduino, it's not hooked up to anything..

This is the code I'm using to declare these pins: LedControl lc=LedControl(A3,A4,A5); digitalWrite(A0, HIGH);

Is that an LED hooked directly from 5V to Ground with no resistor?

yeah lol... that shouldn't affect the led matrix or audio would it? .. ill put a resister there just in case.

I put a 150 ohm resister in, not noticing any changes. LED matrix doesn't work and the LED is still faint.

It would draw an awful lot of current and it might fry the arduino. That would definitely affect the usefulness of anything connected to that board.

alright. Well I've got a resistor there now.. I guess I'll play around with the code some more?

So it works but it is dim. That sounds more like a hardware problem than code.

I notice you are powering the LED matrix off the Arduino. I don't know much about that matrix. Is that safe to do? How much current does that matrix draw? Can the Arduino supply it?

So it works but it is dim. That sounds more like a hardware problem than code.

Actually, it sounds like the pin is an INPUT pin, which they are by default, that OP is using as an OUTPUT pin.