Hey!
I have made an artwork with seven 8x8 matrix and a serial mp3 player.
For this I am using three different LED-libraries: Ledcontroller, Parola and MD_MAX72xx.
SoftwareSerial.h is used for communication with MP3-player.
I am playing animations on led matrix with sound and it works great
until 10-20 minutes. Then Arduino freeze and reset button are not working.
Is it possible for anyone to see any overflow, memory leak or other possible problems in my code?
Thanks for any help!
#include "LedControl.h"
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SoftwareSerial.h>
#define ARDUINO_RX 3//should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX 2//connect to RX of the module
SoftwareSerial mySerial(ARDUINO_RX, ARDUINO_TX);
static int8_t Send_buf[8] = {0} ;
#define CMD_PLAY_W_INDEX 0X03
#define CMD_SET_VOLUME 0X06
#define CMD_SEL_DEV 0X09
#define DEV_TF 0X02
#define CMD_PLAY 0X0D
#define CMD_PAUSE 0X0E
#define CMD_STOP 0X16
#define CMD_SINGLE_CYCLE 0X19
#define SINGLE_CYCLE_ON 0X00
#define SINGLE_CYCLE_OFF 0X01
#define CMD_PLAY_W_VOL 0X22
/*
Now we need a LedControl to work with.
***** These pin numbers will probably not work with your hardware *****
pin 12 is connected to the DataIn
pin 11 is connected to the CLK
pin 10 is connected to LOAD
We have only a single MAX72XX.
*/
LedControl lc=LedControl(12,11,10,3);
/* we always wait a bit between updates of the display */
unsigned long delaytime=200;
#define MAX_DEVICES 4
#define CLK_PIN 5
#define DATA_PIN 7
#define CS_PIN 6
MD_Parola P = MD_Parola(DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
// Global variables
typedef struct
{
uint8_t spacing; // character spacing
char * msg; // message to display
} msgDef_t;
msgDef_t M[] =
{
{ 1, "TEXT1"},
{ 1, "TEXT2"},
{ 1, "TEXT3"},
{ 1, "TEXT4"},
{ 1, "TEXT5"},
{ 1, "TEXT6"} };
#define MAX_STRINGS (sizeof(M)/sizeof(M[0]))
byte aniDown[][17] = {
{
B00010000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000
},{
B00111000,
B00010000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000
},{
B01111100,
B00111000,
B00010000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000
},{
B11111110,
B01111100,
B00111000,
B00010000,
B00000000,
B00000000,
B00000000,
B00000000
},{
B00111000,
B11111110,
B01111100,
B00111000,
B00010000,
B00000000,
B00000000,
B00000000
},{
B00111000,
B00111000,
B11111110,
B01111100,
B00111000,
B00010000,
B00000000,
B00000000
},{
B00111000,
B00111000,
B00111000,
B11111110,
B01111100,
B00111000,
B00010000,
B00000000
},{
B00111000,
B00111000,
B00111000,
B00111000,
B11111110,
B01111100,
B00111000,
B00010000
},{
B00000000,
B00111000,
B00111000,
B00111000,
B00111000,
B11111110,
B01111100,
B00111000
},{
B00000000,
B00000000,
B00111000,
B00111000,
B00111000,
B00111000,
B11111110,
B01111100
},{
B00000000,
B00000000,
B00000000,
B00111000,
B00111000,
B00111000,
B00111000,
B11111110
},{
B00000000,
B00000000,
B00000000,
B00000000,
B00111000,
B00111000,
B00111000,
B00111000
},{
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00111000,
B00111000,
B00111000
},{
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00111000,
B00111000
},{
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00111000
},{
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000
},{
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000
}};
int n = 1;
int prev = 1;
boolean isLast = false;
static uint8_t ansbuf[10] = {0};
void setup() {
mySerial.begin(9600);
delay(500);//Wait chip initialization is complete
sendCommand(CMD_SEL_DEV, DEV_TF);//select the TF card
delay(500);//Wait chip initialization is complete
P.begin();
P.setCharSpacing(M[0].spacing);
P.displayText(M[0].msg, CENTER, 5, 20, SCAN_HORIZ, SCAN_HORIZ );
sendCommand(CMD_PLAY_W_VOL, 0X1E01);
/*
The MAX72XX is in power-saving mode on startup,
we have to do a wakeup call
*/
for(int index=0;index<lc.getDeviceCount();index++) {
lc.shutdown(index,false);
lc.setIntensity(index,8);
}
/* Set the brightness to a medium values */
/* and clear the display */
lc.clearDisplay(0);
}
void playArrow(String toShow) {
for (int b =0; b < 3; b++) {
for (int i =0; i < 17; i++) {
for (int j =0; j < 8; j++) {
if(toShow == "right_ani") {
lc.setRow(2,j,aniDown[i][j]);
}
if(toShow == "left_ani") {
lc.setRow(0,j,aniDown[i][j]);
}
if(toShow == "middle_ani") {
lc.setRow(1,j,aniDown[i][j]);
}
}
delay(40);
}
}
}
void serialFlush(){
while(Serial.available() > 0) {
char t = Serial.read();
}
while(mySerial.available() > 0) {
char t = mySerial.read();
}
}
void loop() {
serialFlush();
if (P.displayAnimate() )
{
P.setTextBuffer(M[n].msg);
//P.setCharSpacing(M[n].spacing);
P.displayReset();
if(prev == 1) {
delay(500);
playArrow("left_ani");
delay(100);
sendCommand(CMD_PLAY_W_VOL, 0X1E01);
delay(250);
}
else if(prev == 3) {
delay(500);
playArrow("right_ani");
delay(100);
sendCommand(CMD_PLAY_W_VOL, 0X1E02);
delay(250);
}
else if(prev == 5) {
delay(500);
playArrow("middle_ani");
delay(100);
sendCommand(CMD_PLAY_W_VOL, 0X1E03);
delay(250);
isLast = true;
}
if(!isLast) {
prev = n;
n = (n + 1) % MAX_STRINGS;
}
else {
prev = 0;
n = 1;
P.setTextBuffer(M[0].msg);
P.setCharSpacing(M[0].spacing);
isLast = false;
}
}
}
void sendCommand(int8_t command, int16_t dat)
{
delay(20);
Send_buf[0] = 0x7e; //starting byte
Send_buf[1] = 0xff; //version
Send_buf[2] = 0x06; //the number of bytes of the command without starting byte and ending byte
Send_buf[3] = command; //
Send_buf[4] = 0x00;//0x00 = no feedback, 0x01 = feedback
Send_buf[5] = (int8_t)(dat >> 8);//datah
Send_buf[6] = (int8_t)(dat); //datal
Send_buf[7] = 0xef; //ending byte
for(uint8_t i=0; i<8; i++)//
{
mySerial.write(Send_buf[i]) ;
}
}