Hello Forum
I'm kind of a beginner in Arduino. I have had worked with an Arduino when I was at school, and I also have some very basic knowledge of programming and electronics. Now I'm at Uni studying something completely unrelated to STEM. However I want to make a nice gift for my GF and I thought of a "Music Box". Instead of a Ballerina I want some LED Matrix displaying a heart animation while playing music from an SD card.
I had written some code to do this, but here comes my question. In what order does the Arduino runs the code? I think is quite obvious that it could (and probably will) perform those two tasks at the same time in this code. I know it may need a delay somewhere.
I think its a pretty dumb question but, wouldn't it just display the animation an then play the sound?
Also I would love having feedback in my code, I had not written any code in a long time, and didn't was the most brilliant student in IT class.
Thanks ![]()
#include <SPI.h>
#include <SD.h>
#include <TMRpcm.h>
#include <pcmRF.h>
#include <LedControl.h>
LedControl lc=LedControl(12,11,8,1);
#define SD_ChipSelectPin 10
unsigned long delaytime=100;
TMRpcm tmrpcm;
int numeroCancion;
char archivo[20];
void sonido();
void setup() {
int numeroCancion;
char archivo[20];
tmrpcm.speakerPin=9;
Serial.begin(9600);
randomSeed(analogRead(0));
numeroCancion = random(36);
sprintf(archivo, "%d.wav", numeroCancion);
lc.shutdown(0,false);
lc.setIntensity(0,8);
lc.clearDisplay(0);
}
void loop() {
corazon();
sonido();
}
void sonido() {
if(!SD.begin(SD_ChipSelectPin))
{
Serial.println("SD not found");
return;
}
tmrpcm.setVolume(6);
tmrpcm.play(archivo);
}
void corazon() {
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};
lc.setRow(0,3,a);
delay(delaytime);
lc.setRow(0,2,b[0]);
lc.setRow(0,3,b[1]);
lc.setRow(0,4,b[2]);
delay(delaytime);
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]);
delay(delaytime);
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]);
delay(delaytime);
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]);
delay(delaytime);
lc.setRow(0,2,b[0]);
lc.setRow(0,3,b[1]);
lc.setRow(0,4,b[2]);
delay(delaytime);
lc.setRow(0,3,a);
delay(delaytime);
lc.clearDisplay(0);
delay(delaytime);
}