Help with multitask

Hello, I need help to convert this code for it to be able to run simultaneously.

void loop(){

//Hacer color rojo
digitalWrite(ledRojo,255);
digitalWrite(ledVerde,0);
digitalWrite(ledAzul,0);
delay(700);

//Hacer color verde
digitalWrite(ledRojo,0);
digitalWrite(ledVerde,255);
digitalWrite(ledAzul,0);
delay(700);

//Hacer color azul
digitalWrite(ledRojo,0);
digitalWrite(ledVerde,0);
digitalWrite(ledAzul,255);
delay(700);

//Hacer color blanco
digitalWrite(ledRojo,255);
digitalWrite(ledVerde,255);
digitalWrite(ledAzul,255);
delay(700);

//Hacer color amarillo
digitalWrite(ledRojo,255);
digitalWrite(ledVerde,255);
digitalWrite(ledAzul,0);
delay(700);

//Hacer color magenta
digitalWrite(ledRojo,255);
digitalWrite(ledVerde,0);
digitalWrite(ledAzul,255);
delay(700);

//Hacer color cian
digitalWrite(ledRojo,0);
digitalWrite(ledVerde,255);
digitalWrite(ledAzul,255);
delay(700);

music.nevergonnagiveyouup(); //YOU'VE BEEN RICK ROLLED

//Hacer color rosa
analogWrite(ledRojo,255);
analogWrite(ledVerde,0);
analogWrite(ledAzul,128);
delay(700);

}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

As Bob said... learn how to post your code properly.

You can't when you use delay() statements.

Take time to understand this...

See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

Thanks, will read

what parts of the code do you want to run simultaneously? looks like your want a changing light sequence and play some music.

presumably you're using an esp32 to play the music. esp32 has an rtos that can support multiple tasks. not sure an arduino can play music and do other things at the same time

easier to understand non-blocking timing based on millis()

RE-edit your first post this way

You haven't posted a complete sketch
consequence: important information was left out which you have to deliver now.
And the code below is not yet a full sketch

Depending on the function-call

music.nevergonnagiveyouup();

beeing blocking or non-blocking the code at the end works if the function-call is non-blocking.

If the function-call

music.nevergonnagiveyouup();

IS blocking a completely different way of programming is needed.
which different way will really work depends on how the music-playing is done
and to decide this you have to provide all the information described in the tutorial

how to get the best out of this forum

Example for using a state-machine for non-blocking timing

const byte sm_color_rojo      = 0;
const byte sm_color_verde     = 1;
const byte sm_color_azul      = 2;
const byte sm_color_blanco    = 3;
const byte sm_color_amarillo  = 4;
const byte sm_color_magenta   = 5;
const byte sm_color_cian      = 6;
const byte sm_color_rosa      = 7;

byte myColorState = sm_color_rojo;

unsigned long WaitTimer;

// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - startOfPeriod >= TimePeriod ) {
    // more time than TimePeriod has elapsed since last time if-condition was true
    startOfPeriod = currentMillis; // a new period starts right here so set new starttime
    return true;
  }
  else return false;            // actual TimePeriod is NOT yet over
}


void myColorStateMachine() {
  
  switch (myColorState) {
    case sm_color_rojo:
      if ( TimePeriodIsOver(WaitTimer,700) ) {
        myColorState = sm_color_verde;
      }
      break;

    case sm_color_verde:
      if ( TimePeriodIsOver(WaitTimer,700) ) {
        myColorState = sm_color_azul;
      }
      break;

    case sm_color_azul:
      if ( TimePeriodIsOver(WaitTimer,700) ) {
        myColorState = sm_color_blanco;
      }
      break;

    case sm_color_blanco:
      if ( TimePeriodIsOver(WaitTimer,700) ) {
        myColorState = sm_color_amarillo;
      }
      break;

    case sm_color_amarillo:
      if ( TimePeriodIsOver(WaitTimer,700) ) {
        myColorState = sm_color_magenta;
      }
      break;

    case sm_color_magenta:
      if ( TimePeriodIsOver(WaitTimer,700) ) {
        myColorState = sm_color_cian;
      }
      break;

    case sm_color_cian:
      if ( TimePeriodIsOver(WaitTimer,700) ) {
        myColorState = sm_color_rosa;
      }
      break;


    case sm_color_rosa:
      if ( TimePeriodIsOver(WaitTimer,700) ) {
        myColorState = sm_color_rojo;
      }
      break;  
  }
}


void loop(){
  myColorStateMachine();
  music.nevergonnagiveyouup(); //YOU'VE BEEN RICK ROLLED
}

best regards Stefan

Thanks, I think i've got the hang of it in general thanks to all of you for your time

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.