# Help with multitask

**URL:** <https://forum.arduino.cc/t/help-with-multitask/1064508>\
**Category:** Programming\
**Created:** [December 12, 2022, 8:41am UTC](https://forum.arduino.cc/t/help-with-multitask/1064508 "2022-12-12T08:41:37Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alxxbm](https://avatars.discourse-cdn.com/v4/letter/a/9f8e36/32.png) [@Alxxbm](https://forum.arduino.cc/u/Alxxbm)\
**Post date:** [December 12, 2022, 8:41am UTC](https://forum.arduino.cc/t/help-with-multitask/1064508/1 "2022-12-12T08:41:37Z")

</div>

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

}

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [December 12, 2022, 8:42am UTC](https://forum.arduino.cc/t/help-with-multitask/1064508/2 "2022-12-12T08:42:27Z")

</div>

Welcome to the forum

> [@Alxxbm](#):
>
> this code

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'  
  
Use [color = red] **code tags** [/color] (the \</\> icon above the compose window)&nbsp;to make it easier to read and copy for examination

> [@How to get the best out of this forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966):
>
> See also [FAQ - Arduino Forum](https://forum.arduino.cc/faq) for general rules on forum behavior and etiquette. Hello, Welcome to the Arduino Forum. This guide explains how to get the best out of this forum. Please read and follow the instructions below. Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is frus…

---

<div class="post-metadata">

**Author:** ![red\_car](https://avatars.discourse-cdn.com/v4/letter/r/b77776/32.png) [@red\_car](https://forum.arduino.cc/u/red_car)\
**Post date:** [December 12, 2022, 9:01am UTC](https://forum.arduino.cc/t/help-with-multitask/1064508/3 "2022-12-12T09:01:10Z")

</div>

> [@Alxxbm](#):
>
> I need help

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

> [@Alxxbm](#):
>
> able to run simultaneously

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

Take time to understand this...  
[https://www.arduino.cc/en/Tutorial/BuiltInExamples/BlinkWithoutDelay](https://www.arduino.cc/en/Tutorial/BuiltInExamples/BlinkWithoutDelay)

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [December 12, 2022, 9:02am UTC](https://forum.arduino.cc/t/help-with-multitask/1064508/4 "2022-12-12T09:02:22Z")

</div>

See [Using millis() for timing. A beginners guide](https://forum.arduino.cc/index.php?topic=503368.0), [Several things at the same time](https://forum.arduino.cc/index.php?topic=223286.0) and the BlinkWithoutDelay example in the IDE

---

<div class="post-metadata">

**Author:** ![Alxxbm](https://avatars.discourse-cdn.com/v4/letter/a/9f8e36/32.png) [@Alxxbm](https://forum.arduino.cc/u/Alxxbm)\
**Post date:** [December 12, 2022, 11:00am UTC](https://forum.arduino.cc/t/help-with-multitask/1064508/5 "2022-12-12T11:00:48Z")

</div>

Thanks, will read

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [December 12, 2022, 2:03pm UTC](https://forum.arduino.cc/t/help-with-multitask/1064508/6 "2022-12-12T14:03:21Z")

</div>

> [@Alxxbm](#):
>
> I need help to convert this code for it to be able to run simultaneously.

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

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [December 12, 2022, 7:47pm UTC](https://forum.arduino.cc/t/help-with-multitask/1064508/7 "2022-12-12T19:47:47Z")

</div>

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

> [@Example-code for timing based on millis() easier to understand through the use of example-numbers / avoiding delay()](https://forum.arduino.cc/t/example-code-for-timing-based-on-millis-easier-to-understand-through-the-use-of-example-numbers-avoiding-delay/974017):
>
> non-blocking timing. Execute code only from time to time. There are a lot of different ways to learn programming. This thread wants to add another approach that is different to the yet existing ones. UPDATE 06.01.2023 if you are mainly interested in applying non-blocking timing you can do a quick read of this short tutorial / demonstration If you are intersted in understanding the details how it works go on reading here. I use an everyday analogon in this post to explain the basic principle …

**RE** -edit your first post this way

> [@Make experienced users help: two ways to quickly post code as a code-section](https://forum.arduino.cc/t/make-experienced-users-help-two-ways-to-quickly-post-code-as-a-code-section/937298):
>
> Hi newcomer, This thread wants to show you how you can speed-up getting helpful answers with small effort. If you manage at least to post this way. You have the sympathy of the experienced users. If you are willing to invest another 20 minutes these 20 minutes extra-time will save you a minimum of 200 minutes to finish your project. always post your full sketch. As a code-section from the very first to the very last line A code-section has a small height =\> keeps your posting "short" but e…

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [December 12, 2022, 8:05pm UTC](https://forum.arduino.cc/t/help-with-multitask/1064508/8 "2022-12-12T20:05:09Z")

</div>

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

```cpp
music.nevergonnagiveyouup();

```

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

If the function-call

```cpp
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

```cpp
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

---

<div class="post-metadata">

**Author:** ![Alxxbm](https://avatars.discourse-cdn.com/v4/letter/a/9f8e36/32.png) [@Alxxbm](https://forum.arduino.cc/u/Alxxbm)\
**Post date:** [December 12, 2022, 8:20pm UTC](https://forum.arduino.cc/t/help-with-multitask/1064508/9 "2022-12-12T20:20:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 10, 2023, 8:21pm UTC](https://forum.arduino.cc/t/help-with-multitask/1064508/10 "2023-06-10T20:21:36Z")

</div>

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