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?

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.

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.

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.

Hope this helps- Imgur: The magic of the Internet

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);

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.

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.

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

Good call PaulS, I declared the single led as an output pin and got the led to light up bright. The LED matrix still isn't working however. I was only using the single LED as a test, i won't actually need to use that for the project, just the led matrix.

The only way I've been able to get the led matrix to work so far is by commenting out these lines:

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

If there a way to bascially undo what these lines are doing? I can't seem to find any documentation on the function calls for this library - init() and openRoot()..

It's time to post your code again. Along with links to all the hardware, if you haven't posted them before.

Parts:
Arduino uno
MAX7219 x2
8x8 LED matrix 2x http://www.ebay.com/itm/MAX7219-Dot-Led-Matrix-Module-MCU-LED-Display-Control-Module-Kit-For-Arduino-/141012140148?pt=LH_DefaultDomain_0&hash=item20d4fa8074

Wave Shield: Adafruit Wave Shield for Arduino Kit [v1.1] : ID 94 : $22.00 : Adafruit Industries, Unique & fun DIY electronics and kits
8 Ohm speaker

//#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;

int led = 9;

LedControl lc=LedControl(A0, A1, A2);

void setup() {
  
  
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(led, 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);


}


void loop() { 

playcomplete("hp1.WAV");

}


void playcomplete(char *name) {

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

delay(3000);
digitalWrite(led, HIGH);
lc.setLed(0,2,6,true); 
delay(3000);
digitalWrite(led, 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();
}

Why are there no Serial.print() statements to help debug the code?

How are you driving that speaker? Could that be dragging the power down?

The wave shield drives the speakers directly, though I'm not entirely sure how that works.. I plug in both cables from the speaker (vcc and ground) to my wave shield here: Imgur: The magic of the Internet

Why are there no Serial.print() statements to help debug the code?

What serial statements would you suggest? The default serial statements I had for debugging weren't helpful - It was printing all the correct information to the serial monitor.. Again the audio works without errors, just not along with the led matrix.

What serial statements would you suggest?

How about something like:

if (!card.init()) {return;}
Serial.print("Nospacesneededhere");
if (!vol.init(card)) {return;}
Serial.print("Letsputeverythingononelinetomakethecodehardtoread");
if (!root.openRoot(vol)) {return;}
Serial.print("root canal complete");

Then, you'd at least have a clue where the program is dying.

I added the code you suggested and I'm not getting anything when I open the serial monitor. Audio works, led matrix doesn't still.

partsofme:
I added the code you suggested and I'm not getting anything when I open the serial monitor. Audio works, led matrix doesn't still.

Did you also add a Serial.begin() statement?