Question

Okay so I have just recently dived into programming an Arduino, Currently I have the basic blink function along with a RGB LED program that changes an LED to blue, green and red in fading colors. I have 2 LEDS a simple and basic yellow LED that's supposed to function as an indicator for a "working status". And a LED that is RGB. Now I want the RGB one to transition through it's colors normally although I want to keep the Yellow LED constantly flashing. How hould I make my code so that two processes can run at the same time?

Was it helpful?

Solution

Something like:

int timekeeper=0;
while (1) 
{
    do_fade(timekeeper);
    if (timekeeper%100==0) {
      do_blink_off();
    }
    if (timekeeper%100==50) {
      do_blink_on();
    }
    delay(10);
    timekeeper++;
}

This is done from memory, so your mileage may vary. I've passed timekeeper to do_fade(), so you can figure out how far along the fade you are. do_fade() would update the fade, then immediately return. do_blink_on() and do_blink_off() would be similar - change what you need to change, then return. In this example, do_fade() would be called every 10 milliseconds, do_blink_off() once per second, with do_blink_on() 1/2 a second after (so on, 1/2 second, off, 1/2 second, on, 1/2 second...)

OTHER TIPS

AMADANON's answer will work, however keep in mind the preferred way to do multiple tasks like this is with timer interrupts. For example, if you wanted your code to do something else after it fades, the timing of those other functions will interfere with your LED blinking. To solve this, you use timers that are built into the Arduino.

In the background, a timer is counting up, and when it hits a certain value, it resets it's counter and triggers the Interrupt Service Routine, which is where you would turn the LED on/off.

Here's a tutorial on blinking an LED with timer interrupts: http://www.engblaze.com/microcontroller-tutorial-avr-and-arduino-timer-interrupts/

Try RTOS for Arduino.

You create tasks which are separate loops. I use it and it works fine.

https://create.arduino.cc/projecthub/feilipu/using-freertos-multi-tasking-in-arduino-ebc3cc

Also, I recommend using PlatformIO with the Arduino environment. Then you can also import RTOS via the library.

https://platformio.org/

Example code snippets:

In the setup:

void TaskMotion( void *pvParameters ); // Senses input from the motion sensor

and

  xTaskCreate( // Create task
    TaskMotion
    ,  "Motion"   // A name just for humans
    ,  12800  // Stack size
    ,  NULL
    ,  1  // priority
    ,  NULL );

... below the Arduino loop (having nothing but a delay(1000); in):

//  ╔╦╗╔═╗╔╦╗╦╔═╗╔╗╔  ╔═╗╔═╗╔╗╔╔═╗╔═╗╦═╗
//  ║║║║ ║ ║ ║║ ║║║║  ╚═╗║╣ ║║║╚═╗║ ║╠╦╝
//  ╩ ╩╚═╝ ╩ ╩╚═╝╝╚╝  ╚═╝╚═╝╝╚╝╚═╝╚═╝╩╚═
void TaskMotion(void *pvParameters)  // This is a task.
{
  (void) pvParameters;

  // initialize stuff.


  for (;;) // A Task shall never return or exit.
  {
    Serial.println("TEST MOTION");
    delay(10000);

  }

}

Copy paste and change "TaskMotion" to "LED something". You can create as many tasks as you want. The RTOS manages each task. Like if one task has a delay(10), then the next 10 ms are used for another task.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top