¿How does arduino UNO runs code?

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 :slight_smile:

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

I am afraid, this code won't work at all. You start a playing song again and again every 0.7 second.
Instead of a song, you will have a continuous stutter.
Before starting a new song, you should check if the previous one has ended with the function

if (tmrpcm.isPlaying()) 

In general, there are a lot of things to improve in the code - for example, it will play the same melody endlessly, it is unlikely to be very interesting

void loop() {
corazon();
sonido();
}

first it will process corazon() then sonido()
afterwards: corazon() then sonido()
afterwards: corazon() then sonido()
..

if you want to do tasks in "near parallel" - you should not block your code with delays.
See Example "Blink Without Delay" how this is done.

You actually need to remove delays not add them.

This is very good advice.

We get this sort of question nearly every day on this forum, and every day the answer is the same. So much so that there are many many posts and websites that try and guide a beginner to do this. Here are two:-
See my
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Or Robin2's several things at once
http://forum.arduino.cc/index.php?topic=223286.0

By the way welcome to the forum and thanks for learning how to post your code correctly.

Then you think wrong.

Most types of arduino have only one CPU core and no operating system to allow that core to run multiple tasks at the same time.

However, with the correct coding techniques, even a single core can appear to humans to be performing 2 or more tasks at the same time.

But your code does not use these coding techniques. It uses a very simple "beginner" coding style including use of delay().

In this case, you are not quite right.
TMRpcm.h library allows to play music asynchronous with main loop code. But OP use it incorrectly...

Yeah, I know it will play the same song over and over. The idea is to reset the Arduino with a lever switch attached to the box's lid.
So, including the blink without delay, how to avoid it replaying every 0.7s ? How do I use the library correctly?

See the post #2 - take attention on isPlaying() method