Need help with task synchronization, arduino UNO car kit

I am trying to develop a remote controlled car using a L298 motor driver, a HC-SR04 ultra sonic sensor and a TSOP3823 IR receiver, as well as an Arduino UNO. I have three tasks, one task for updating the distance using the ultra sonic sensor, one task that takes the commands from the IR receiver like drive forward or backward etc, and one task that executes the commands. My problem is that when all three tasks are created, none of them are executed, if I remove any task or comment away the task creation, the other remaining two tasks run as expected. I can for instance remove the distance sensor task and control the car just fine with the remote. I have tested creating just a third arbitrary task that share nothing with the other tasks to see if the problem persists and the result is the same. I'll share the tasks and my setup below, thanks in advance for any help.

//Ultra sonic sensor task
void distanceSensorTask(void* pvParameters)
{
  while(1)
  {
    xSemaphoreTake(distanceMutex, portMAX_DELAY);
    digitalWrite(TRIG_PIN, LOW);
    delayMicroseconds(10);
    digitalWrite(TRIG_PIN, HIGH);
    delayMicroseconds(10);
    digitalWrite(TRIG_PIN, LOW);
    duration = pulseIn(ECHO_PIN, HIGH);
    distance = (duration * 0.0340)/2; // pulse to cm
    Serial.println(distance);
    xSemaphoreGive(distanceMutex);
    delay(100);
  }
}
// ir sensor task
void IRsensorTask()
{
  while(1)
  {  
    if(IrReceiver.decode()) 
    {
      uint16_t command = IrReceiver.decodedIRData.command;
      switch (command) 
      { 
        case 0x44: 
          motorCommand = SPEED_LOW;
          break;
        case 0x40:
          motorCommand = SPEED_MEDIUM;
          break;
        case 0x43: 
          motorCommand = SPEED_HIGH; 
          break;
        case 0x0C:
          motorCommand = LEFT;
          break;	
        case 0x5E: 
          motorCommand = RIGHT;
          break;
        case 0x19:
          motorCommand = FORWARD;
          break;		
        case 0x1C:
          motorCommand = BACKWARD;
          break;	
        case 0x18:	
          motorCommand = STOP;
          break;
        default:
          break;
      } 	   
      xQueueSend(commandQueue, &motorCommand, portMAX_DELAY); // send command to motor control
      IrReceiver.resume();
    }
    delay(50);
  }
}
// motor control task
void motorControlTask()
{
  int receivedCommand;
  while(1)
  {
    xQueueReceive(commandQueue, &receivedCommand, portMAX_DELAY); // receive command from IR sensor
    
    xSemaphoreTake(distanceMutex, portMAX_DELAY);
    if(distance < 20)
    {
      motorStop();
    }
    xSemaphoreGive(distanceMutex);

    switch(receivedCommand)
    {
      case FORWARD:
        motorForward(speed);
        break;
      case BACKWARD:
          motorBackward(speed);
          break;
      case LEFT: 
        motorLeft(speed);
        break;
      case RIGHT:
        motorRight(speed);
        break;
      case STOP:
        motorStop();
        break;
       case SPEED_LOW:
        speed = 76;
        break;
       case SPEED_MEDIUM:
        speed = 153;
        break;
      case SPEED_HIGH:
        speed = 255;
        break;
      default:
        break;
     }
      delay(100);
  }
}
// setup
void setup() {
  Serial.begin(9600);

  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

  pinMode(LEFT_MOTOR_PWM, OUTPUT);
  pinMode(LEFT_MOTOR_DIR1, OUTPUT);
  pinMode(LEFT_MOTOR_DIR2, OUTPUT);
  pinMode(RIGHT_MOTOR_PWM, OUTPUT);
  pinMode(RIGHT_MOTOR_DIR1, OUTPUT);
  pinMode(RIGHT_MOTOR_DIR2, OUTPUT);

  distanceMutex = xSemaphoreCreateMutex();

  commandQueue = xQueueCreate(10, sizeof(int)); 
  
  xTaskCreate(distanceSensorTask, "distanceSensorTask", 128, NULL, 3, NULL);
  xTaskCreate(IRsensorTask, "IRsensorTask", 128, NULL, 2, NULL);
  xTaskCreate(motorControlTask, "motorControlTask", 128, NULL, 2, NULL);
  
  IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);  
  
  vTaskStartScheduler();
}
// IR and distance sensor pins
#define TRIG_PIN 3
#define ECHO_PIN 4
#define IR_RECEIVE_PIN 7

// Motor commands
#define FORWARD 1
#define BACKWARD 2
#define STOP 0
#define LEFT 3
#define RIGHT 4
#define SPEED_LOW 5
#define SPEED_MEDIUM 6
#define SPEED_HIGH 7

// Motor pins
#define LEFT_MOTOR_PWM 5
#define LEFT_MOTOR_DIR1 10 // FORWARD
#define LEFT_MOTOR_DIR2 9 // BACKWARD
#define RIGHT_MOTOR_PWM 6
#define RIGHT_MOTOR_DIR1 11
#define RIGHT_MOTOR_DIR2 12

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