Arduino Due FreeRTOS Question

I am looking to create my first project using FreeRTOS. The system will have six different buttons each starting a different mode. The objective is that if a task is currently running and a button is pressed then the arduino will stop that task and start a new one which will run indefinitely until another button is pressed. I was thinking of the following basic design. Using an int variable which will indicate which mode is active.

volatile int _activeMode = 0; //Mode1 = 0, Mode2 = 1, Mode3 = 2, Mode4 = 3, Mode 5 = 4, Mode6 = 5

I was going to then create a task for each mode and do the following inside each of those tasks

//This is inside Mode2 task
while(_activeMode == 1)
{
     DosomethingForMode2();
}

I was going to have a high priority task which monitors for the button presses and changes the _activeMode variable based upon which button was pressed.

All the mode tasks are going to be created with the same priority.

Is there a better way to accomplish what I want or should my concept work safely?

A state machine would fit best for your requirement. Pseudo code:

enum class ButtonIdentity : byte
{
RedButton,
BlueButton,
GreenButton,
}

ButtonIdentity LastPressedButton;

void ButtonTask()
{
ButtonIdentity PressedButton = GetPressedButton();
switch (PressedButton)
{
case ButtonIdentity::RedButton :
//do things
break;
//more cases here
}
LastPressedButton = PressedButton;
}

ButtonIdentity GetPressedButton()
{
if (Red Button is pressed)
{
return ButtonIdentity::RedButton;
} else if (Blue Button is pressed)
{
return ButtonIdentity::BlueButton;
} //more cases here
return LastPressedButton;
}

Thank you for your response. I understand with what I am about to say it will illicit a negative response based on not providing enough details(rightfully so) but here it goes.

The reason this won't work for me (I think) is because I need to monitor 16 limit switches to check if I need to stop a motor or not. This needs to be almost realtime to not damage mechanicals. Inside the mode tasks I will have the control for the motors. I start different motors in different directions based on mode selected. I will have a seperate task which will monitor the limit switches and kill the motor based upon a limit switch being pressed. This will be my highest priority task. This is why I was thinking of using an RTOS. I basically need to be doing two tasks at at the same time at certain times. I already created a sketch which does what I want without using an RTOS but it is very clunky and buggy. At many points in my code I have to call functions to read buttons and limit switches. I thought it would be cleaner using an RTOS.

pleslie:
Thank you for your response. I understand with what I am about to say it will illicit a negative response based on not providing enough details(rightfully so) but here it goes.

The reason this won't work for me (I think) is because I need to monitor 16 limit switches to check if I need to stop a motor or not. This needs to be almost realtime to not damage mechanicals. Inside the mode tasks I will have the control for the motors. I start different motors in different directions based on mode selected. I will have a seperate task which will monitor the limit switches and kill the motor based upon a limit switch being pressed. This will be my highest priority task. This is why I was thinking of using an RTOS. I basically need to be doing two tasks at at the same time at certain times. I already created a sketch which does what I want without using an RTOS but it is very clunky and buggy. At many points in my code I have to call functions to read buttons and limit switches. I thought it would be cleaner using an RTOS.

Certainly, it is a valid approach. But you need to also take mutex locking and releasing into account when you do the timing.

I think you need to compare two approaches to figure out the best one to use.

I agree. This is why I posted my question. I am very unfamiliar with freeRTOS and wanted to see if there were issues with my strategy to help in my decision on switching to an RTOS or sticking with my current implementation and trying to improve.