Hello Forum.
I've been working on a project for my anniversary, I'm trying to make a music Box. This music box is supposed to be displaying a little animation while the music sounds. I have achieved to play music using the TMRpcm library. But i can't get to display the animation. The animation works independently when in it own sketch, but somehow is failing while trying to do it simultaneously. I think it could be something about the audio.isPlaying
function as the library example doesn't work either. Any suggestions on how to improve my code would be awesome as i'm a newbie. I'm using an Arduino Uno.
#include <SPI.h>
#include <SD.h>
#include <TMRpcm.h>
#include <LedControl.h>
LedControl lc = LedControl(11, 13, 8, 1);
#define SD_ChipSelectPin 10
TMRpcm audio;
int i = 0;
int numeroCancion;
char archivo[20];
unsigned long time = 0;
void setup() {
Serial.begin(9600);
audio.speakerPin = 9;
lc.shutdown(0, false);
lc.setIntensity(0, 8);
lc.clearDisplay(0);
randomSeed(analogRead(0));
numeroCancion = random(43);
sprintf(archivo, "%d.wav", numeroCancion);
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
return;
}
else {
Serial.println("SD ok");
Serial.println (archivo);
audio.setVolume(6);
audio.play(archivo);
}
}
void loop() {
unsigned long tiempovivo = millis();
byte a = B00011000;
byte b[3] = {B00111100, B00111100, B00111100};
byte c[6] = {B00100100, B01111110, B01111110, B01111110, B00111100, B00011000};
byte d[7] = {B01100110, B11111111, B11111111, B01111110, B01111110, B00111100, B00011000};
if ( audio.isPlaying() && tiempovivo - time > 250 ) {
switch (i) {
case 0:
lc.clearDisplay(0);
lc.setRow(0, 3, a);
i = 1;
break;
case 1:
lc.clearDisplay(0);
lc.setRow(0, 2, b[0]);
lc.setRow(0, 3, b[1]);
lc.setRow(0, 4, b[2]);
i = 2;
break;
case 2:
lc.clearDisplay(0);
lc.setRow(0, 1, c[0]);
lc.setRow(0, 2, c[1]);
lc.setRow(0, 3, c[2]);
lc.setRow(0, 4, c[3]);
lc.setRow(0, 5, c[4]);
lc.setRow(0, 6, c[5]);
i = 3;
break;
case 3:
lc.clearDisplay(0);
lc.setRow(0, 1, d[0]);
lc.setRow(0, 2, d[1]);
lc.setRow(0, 3, d[2]);
lc.setRow(0, 4, d[3]);
lc.setRow(0, 5, d[4]);
lc.setRow(0, 6, d[5]);
lc.setRow(0, 7, d[6]);
i = 4;
break;
case 4:
lc.clearDisplay(0);
lc.setRow(0, 1, c[0]);
lc.setRow(0, 2, c[1]);
lc.setRow(0, 3, c[2]);
lc.setRow(0, 4, c[3]);
lc.setRow(0, 5, c[4]);
lc.setRow(0, 6, c[5]);
i = 5;
break;
case 5:
lc.clearDisplay(0);
lc.setRow(0, 2, b[0]);
lc.setRow(0, 3, b[1]);
lc.setRow(0, 4, b[2]);
i = 6;
break;
case 6:
lc.clearDisplay(0);
lc.setRow(0, 3, a);
i = 7;
break;
case 7:
lc.clearDisplay(0);
i = 0;
break;
}
time = tiempovivo;
}
}