How can i do something like multi threathing in arduino?

I check in ways to implement multi threathing in arduino, i also try using the library TimerOne but it doesn't seem to work, what i'm trying to make is an arduino aplication than, every time a certain ammount of time pass, it pop an item from the stack, and, between that times, the aplication allow me to add items to the stack, both process working in paralel of course.

The items poped and pushed are numbers.

Here is the code:

#include <TimerOne.h>
#include <QueueList.h>
#include <StackList.h>

const uint16_t  NOTE_B0     =   31;
const uint16_t  NOTE_C1     =   33;
const uint16_t  NOTE_CS1    =   35;
const uint16_t  NOTE_D1     =   37;
const uint16_t  NOTE_DS1    =   39;
const uint16_t  NOTE_E1     =   41;
const uint16_t  NOTE_F1     =   44;
const uint16_t  NOTE_FS1    =   46;
const uint16_t  NOTE_G1     =   49;
const uint16_t  NOTE_GS1    =   52;
const uint16_t  NOTE_A1     =   55;
const uint16_t  NOTE_AS1    =   58;
const uint16_t  NOTE_B1     =   62;
const uint16_t  NOTE_C2     =   65;
const uint16_t  NOTE_CS2    =   69;
const uint16_t  NOTE_D2     =   73;
const uint16_t  NOTE_DS2    =   78;
const uint16_t  NOTE_E2     =   82;
const uint16_t  NOTE_F2     =   87;
const uint16_t  NOTE_FS2    =   93;
const uint16_t  NOTE_G2     =   98;
const uint16_t  NOTE_GS2    =  104;
const uint16_t  NOTE_A2     =  110;
const uint16_t  NOTE_AS2    =  117;
const uint16_t  NOTE_B2     =  123;
const uint16_t  NOTE_C3     =  131;
const uint16_t  NOTE_CS3    =  139;
const uint16_t  NOTE_D3     =  147;
const uint16_t  NOTE_DS3    =  156;
const uint16_t  NOTE_E3     =  165;
const uint16_t  NOTE_F3     =  175;
const uint16_t  NOTE_FS3    =  185;
const uint16_t  NOTE_G3     =  196;
const uint16_t  NOTE_GS3    =  208;
const uint16_t  NOTE_A3     =  220;
const uint16_t  NOTE_AS3    =  233;
const uint16_t  NOTE_B3     =  247;
const uint16_t  NOTE_C4     =  262;
const uint16_t  NOTE_CS4    =  277;
const uint16_t  NOTE_D4     =  294;
const uint16_t  NOTE_DS4    =  311;
const uint16_t  NOTE_E4     =  330;
const uint16_t  NOTE_F4     =  349;
const uint16_t  NOTE_FS4    =  370;
const uint16_t  NOTE_G4     =  392;
const uint16_t  NOTE_GS4    =  415;
const uint16_t  NOTE_A4     =  440;
const uint16_t  NOTE_AS4    =  466;
const uint16_t  NOTE_B4     =  494;
const uint16_t  NOTE_C5     =  523;
const uint16_t  NOTE_CS5    =  554;
const uint16_t  NOTE_D5     =  587;
const uint16_t  NOTE_DS5    =  622;
const uint16_t  NOTE_E5     =  659;
const uint16_t  NOTE_F5     =  698;
const uint16_t  NOTE_FS5    =  740;
const uint16_t  NOTE_G5     =  784;
const uint16_t  NOTE_GS5    =  831;
const uint16_t  NOTE_A5     =  880;
const uint16_t  NOTE_AS5    =  932;
const uint16_t  NOTE_B5     =  988;
const uint16_t  NOTE_C6     = 1047;
const uint16_t  NOTE_CS6    = 1109;
const uint16_t  NOTE_D6     = 1175;
const uint16_t  NOTE_DS6    = 1245;
const uint16_t  NOTE_E6     = 1319;
const uint16_t  NOTE_F6     = 1397;
const uint16_t  NOTE_FS6    = 1480;
const uint16_t  NOTE_G6     = 1568;
const uint16_t  NOTE_GS6    = 1661;
const uint16_t  NOTE_A6     = 1760;
const uint16_t  NOTE_AS6    = 1865;
const uint16_t  NOTE_B6     = 1976;
const uint16_t  NOTE_C7     = 2093;
const uint16_t  NOTE_CS7    = 2217;
const uint16_t  NOTE_D7     = 2349;
const uint16_t  NOTE_DS7    = 2489;
const uint16_t  NOTE_E7     = 2637;
const uint16_t  NOTE_F7     = 2794;
const uint16_t  NOTE_FS7    = 2960;
const uint16_t  NOTE_G7     = 3136;
const uint16_t  NOTE_GS7    = 3322;
const uint16_t  NOTE_A7     = 3520;
const uint16_t  NOTE_AS7    = 3729;
const uint16_t  NOTE_B7     = 3951;
const uint16_t  NOTE_C8     = 4186;
const uint16_t  NOTE_CS8    = 4435;
const uint16_t  NOTE_D8     = 4699;
const uint16_t  NOTE_DS8    = 4978;


uint16_t Notas[] =
{
      NOTE_B0   
    , NOTE_C1   
    , NOTE_CS1
    , NOTE_D1   
    , NOTE_DS1
    , NOTE_E1   
    , NOTE_F1   
    , NOTE_FS1
    , NOTE_G1   
    , NOTE_GS1
    , NOTE_A1   
    , NOTE_AS1
    , NOTE_B1   
    , NOTE_C2   
    , NOTE_CS2
    , NOTE_D2   
    , NOTE_DS2
    , NOTE_E2   
    , NOTE_F2   
    , NOTE_FS2
    , NOTE_G2   
    , NOTE_GS2
    , NOTE_A2   
    , NOTE_AS2
    , NOTE_B2   
    , NOTE_C3   
    , NOTE_CS3
    , NOTE_D3   
    , NOTE_DS3
    , NOTE_E3   
    , NOTE_F3   
    , NOTE_FS3
    , NOTE_G3   
    , NOTE_GS3
    , NOTE_A3   
    , NOTE_AS3
    , NOTE_B3   
    , NOTE_C4   
    , NOTE_CS4
    , NOTE_D4   
    , NOTE_DS4
    , NOTE_E4   
    , NOTE_F4   
    , NOTE_FS4
    , NOTE_G4   
    , NOTE_GS4
    , NOTE_A4   
    , NOTE_AS4
    , NOTE_B4   
    , NOTE_C5   
    , NOTE_CS5
    , NOTE_D5   
    , NOTE_DS5
    , NOTE_E5   
    , NOTE_F5   
    , NOTE_FS5
    , NOTE_G5   
    , NOTE_GS5
    , NOTE_A5   
    , NOTE_AS5
    , NOTE_B5   
    , NOTE_C6   
    , NOTE_CS6
    , NOTE_D6   
    , NOTE_DS6
    , NOTE_E6   
    , NOTE_F6   
    , NOTE_FS6
    , NOTE_G6   
    , NOTE_GS6
    , NOTE_A6   
    , NOTE_AS6
    , NOTE_B6   
    , NOTE_C7   
    , NOTE_CS7
    , NOTE_D7   
    , NOTE_DS7
    , NOTE_E7   
    , NOTE_F7   
    , NOTE_FS7
    , NOTE_G7   
    , NOTE_GS7
    , NOTE_A7   
    , NOTE_AS7
    , NOTE_B7   
    , NOTE_C8   
    , NOTE_CS8
    , NOTE_D8
    , NOTE_DS8
};

QueueList<int> KeyNotesNode = QueueList<int>();
StackList<int> StackNote = StackList<int>();

char receivedChar1 = '<';
char receivedChar2 = '<';

char receivedChar[] = {receivedChar1, receivedChar2};

int TempInt = 0;
bool newData = false;

bool itPopUp = false;

int modality = 0;

void setup() {                
  Serial.begin(9600);

  if(modality == 0){
    Serial.println("Ingrese el tipo de funcionalidad deseada");
    Serial.println("1.- Constructor de canciones en tonos");
    Serial.println("2.- Juego de imposicion musical");
  }

  Timer1.initialize(150000);
  Timer1.attachInterrupt( setForPop );
}

void loop() {
  //Checa y controla la recepcion de los char a traves de la consola de arduino
    
  if (Serial.available()) 
  {
    receiveChar();
    showReceivedChar();
  }
      
//   . . .  Here is some code to process the reception of data from serialData  . . .

 
// From somewhere here start the loop

// . . . LOOPED . . .

    //Here is used the data
    if(modality == 1){
       //This option doesn't matter
    }
    else if(modality == 2){
      StackNote.push( (((int)(receivedChar[0]-'0'))*10)+((int)receivedChar[1]-'0') );
      Serial.println("Agregado receivedChar exitosamente a la Pila");
      Serial.print("Tamano Total Lista: ");
      Serial.println(StackNote.count());
      Serial.print("Dato:");
      Serial.println( (((int)(receivedChar[0]-'0'))*10)+((int)receivedChar[1]-'0') );
      Serial.print("Dato(get):");
      receivedChar[0] = '<';
      receivedChar[1] = '<';
    }
}

    //Run and show the data on the stack
     if(modality == 1)
     {
           //This option doesn't matter
      }
      else if(modality == 2)
      {

        if(itPopUp){   //With this variable i want to control when it is show, this code is inside loop()
          Serial.print("Reproducido con exito la nota: "); //se escribe 'éxito' pero la consola no acepta tildes, que se arrepienta :P
          TempInt = StackNote.pop();
          Serial.println(TempInt);
          tone(9,Notas[((int)TempInt)],500);
          delay(500);
        }
      }
      
    
  } //Here the loop() end

//This function i want to run it asyncronously
 void setForPop(){
    if(itPopUp){
      itPopUp = !itPopUp;
    }
    else{
      itPopUp = !itPopUp;
    }
 }

Please, feel free to ask anything

Thanks in advance

You can manipulate the same data structures ( in this case your stack) pseudo-asynchronously in both the loop() and in the interrupt service routine called by the timer.
You should declare any shared variables as volatile and should suspend interrupts in the loop for the statement where you are using such a shared variable.

Hey!

I agree with @6v6gt, interrupts should be more than enough....
you mentioned you had issues using the timer library, perhaps implement it manually - it's really simple.

If that doesnt satisfy you you can try to download an 'OS'on the Arduino which provides (to some extent) parallel processing.

Best of luck!!

Keep it simple. Have a look at the demo Several Things at a Time

...R

Robin2:
Keep it simple. Have a look at the demo Several Things at a Time

Or you can do it the complicated way, which turns out to be simpler in the long run.

I like to organise my code into objects, each of which has a setup() and loop() method. Pin assignments are done in the constructor, and the main sketch setup and loop calls the setup and loop of each object (several ways of organising this). Each object does its own thing with the pins it owns an its own internal state. Communication between objects can be done statically or by references that are passed into the constructor.

i also try using the library TimerOne but it doesn't seem to work,

It certainly toggles itPopUp every 150ms. I don't see how this can work in conjunction with a conditional statement with a 500 ms delay.

if(itPopUp){   //With this variable i want to control when it is show, this code is inside loop()
          Serial.print("Reproducido con exito la nota: "); //se escribe 'éxito' pero la consola no acepta tildes, que            se arrepienta :P
          TempInt = StackNote.pop();
          Serial.println(TempInt);
          tone(9,Notas[((int)TempInt)],500);
          delay(500);
        }

PaulMurrayCbr:
Or you can do it the complicated way, which turns out to be simpler in the long run.

Are you a marathon runner ?

I am content with a 5 metre stroll.

...R