Threading in embeded devices for example an Arduino is quite difficult to code. The code is getting more and more unreadable. FreeRTOS is an operating System for embedded devices. Threads are clearly separated from each other. The code is entered in this article.

/*
 * Example to demonstrate thread definition with two LED's.
 */
#include <FreeRTOS_ARM.h>
// Redefine AVR Flash string macro as nop for ARM
#undef F
#define F(str) str
// The LED is attached to pin 12 and 13 on Arduino.
const uint8_t LED_PIN_12 = 12;
const uint8_t LED_PIN_13 = 13;
//------------------------------------------------------------------------------
/*
 * Thread 1
 */
// Declare the thread function for thread 1.
static void Thread1(void* arg) {
  while (1) {
    
    digitalWrite(LED_PIN_12, HIGH);
    double  t = (500L * configTICK_RATE_HZ) / 1000L;
    vTaskDelay(t);
    digitalWrite(LED_PIN_12, LOW);
    double t2 = (500L * configTICK_RATE_HZ) / 1000L;
    vTaskDelay(t2);
  }
}
/*
 * Thread 1
 */
// Declare the thread function for thread 1.
static void Thread2(void* arg) {
  while (1) {
    digitalWrite(LED_PIN_13, HIGH);
    double  t = (200L * configTICK_RATE_HZ) / 1000L;
    vTaskDelay(t);
    digitalWrite(LED_PIN_13, LOW);
    double t2 = (200L * configTICK_RATE_HZ) / 1000L;
    vTaskDelay(t2);
  }
}
//------------------------------------------------------------------------------
void setup() {
  portBASE_TYPE s1,s2;
  // Useable for logging
  Serial.begin(9600);
  // create thread 1
  s1 = xTaskCreate(Thread1, NULL, configMINIMAL_STACK_SIZE, NULL, 1, NULL);
  // create thread 2
  s2 = xTaskCreate(Thread2, NULL, configMINIMAL_STACK_SIZE, NULL, 2, NULL);
  // check for creation errors
  if (s1 != pdPASS  || 21 != pdPASS ) {
    Serial.println(F("Creation problem"));
    while(1);
  }
  // start scheduler, start of threads
  vTaskStartScheduler();
  Serial.println(F("Insufficient RAM"));
  while(1);
}
//------------------------------------------------------------------------------
// loop must never block
void loop() {
  // Not used.
}