Freertos for esp32 in arduino ide

Hello, I have an problem statement. I have an three tasks. Task:- when someone hold down the push button then turn on the led, when someone released the push button then turn off the led, after releasing the push button, Task2:- blinks the same led continuously. Task3:- input from the user and prints on the terminal.
so, when i push the button and released the button then only blinks the same led continuously, otherwise not. i have tried alot, but there is watchdog errror occur, rst error occcur, Please help, how i can do this ?
i am sharing my code

const int buttonPin = 18;  // Replace with your actual button pin
const int ledPin = 19;     // Replace with your actual LED pin
static bool buttonPressed = false;




void task1(void* pvParameters);
void task2(void* pvParameters);
void task3(void* pvParameters)

  void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(1000);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);

  xTaskCreate(task1, "task1", 2048, NULL, 1, NULL);
  xTaskCreate(task3, "task3", 2048, NULL, 1, NULL);
}

void loop() {
  // put your main code here, to run repeatedly:
}


void task1(void* pvParameters) {

  while (1) {

    bool buttonState = digitalRead(buttonPin);

    if (buttonState == LOW && !buttonPressed) {
      // Button pressed
      digitalWrite(ledPin, HIGH);
      buttonPressed = true;
      Serial.println("Button pressed!");
    } else if (buttonState == HIGH && buttonPressed) {
      // Button released
      digitalWrite(ledPin, LOW);
      buttonPressed = false;
      Serial.println("Button released!");
      vTaskDelay(pdMS_TO_TICKS(2000));  // Wait for 1 second
      xTaskCreate(task2, "task2", 2048, NULL, 1, NULL);
      vTaskDelete(NULL);
    }
  }
}


void task2(void* pvParameters) {
  while (1) {
    digitalWrite(ledPin, HIGH);
    vTaskDelay(pdMS_TO_TICKS(500));  // Blink for 500 ms
    digitalWrite(ledPin, LOW);
    vTaskDelay(pdMS_TO_TICKS(500));  // Wait for 500 ms
  }
}


void task3(void* pvParameters) {
  while (1) {
    static String inputMessage;
    char inputChar;

    if (Serial.available() > 0) {
      inputChar = Serial.read();

      if (inputChar == '\n' || inputChar == '\r') {
        Serial.print("You typed: ");
        Serial.println(inputMessage);

        // Reset the message buffer
        inputMessage = "";
      } else {
        // Append the character to the message
        inputMessage += inputChar;
      }
    }
  }
}

Hi @harsh_09
Welcome to FreeRTOS Community Forums.

xTaskCreate(task2, "task2", 2048, NULL, 1, NULL); is called within the Task 1 , when the push button is released. Since the Task 2 is created only when you are releasing the push button, the led starts blinking when you push and release the button.

In order to achieve the blinking led continuously, you should put the task 2 creation in the setup() function:

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(1000);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);

xTaskCreate(task1, “task1”, 2048, NULL, 1, NULL);
xTaskCreate(task2, “task2”, 2048, NULL, 1, NULL);
xTaskCreate(task3, “task3”, 2048, NULL, 1, NULL);
}

Since you are creating all 3 tasks at the same priority, make sure to define configUSE_PREEMPTION and configUSE_TIME_SLICING in FreeRTOSConfig.h to allow the scheduler to pre-empt or use portYIELD() within the task definition.

Hi @karahulx Thanks for the replying. Let me first explain briefly. I have an one push button and one led and one esp32. I want to create an 3 tasks. When the program start, Task1:- I want, when user hold the push button, then turn on the led , when user released the button, then turn off the led. After releasing the push button, after few milliseconds. Task2:- blinking the same led continuously. Task3:- taking the Input from the user in terminal when available and print the user input on the terminal.
This is the first time, now led blink going on. At that time, if user again hold the push button then again turn on the led, or blinking the led stop. And when user released the button, then again start the blinking led continuous. This is the problem statement. I am new in FREERTOS. So i have no too much knowledge. Can you please guide me ? If within two days, if i have no complete this, then where i am Pursuing my training, they will kick out me, and select the another person. Please can you help me ? How i can achieved this?

Are you periodically signalling the watchdog timer to prevent the watchdog timer from expiring? If so, where is it done?

Did you try disabling the watchdog timer and see if you are getting expected results?

I’d use just 1 task controlling the LED. If the button is down switch the LED on. If the button is released, blink the LED e.g. starting with the OFF period.
I’d set the LED task to a lower priority and let it poll the button state and use RTOS - xTaskCheckForTimeOut() to handle the blink period, but a simple vTaskDelay would also work.
Depending on how fast you’ve to react on a button press you could use smaller delays and count them to achieve the desired blink period.
Good luck !

Hi @hs2 actually i also want this similar things. But i am a newbie in this field, can you please guide me personally? Really it would great help for me​:sob::sob:

I don’t know, how to disable the watchdog. I do ,let see what’s happens. By the way thank you so much

This is just C-code.
If button down → LED on. If button up → toggle LED + delay in your task loop as a simple approach. That should be doable :+1:

@harsh_09 Are you able to solve your problem? One way to achieve it, as described by @hs2, is the following:

void task1(void* pvParameters) {

  bool prevButtonState = HIGH; // not pressed.
  bool curButtonState = HIGH; // not pressed.
  bool startBlinking = false;

  while (1) {
    // Only monitor button if we have not started blinking yet.
    if( startBlinking == false )
    {
      curButtonState = digitalRead(buttonPin);

      if( ( curButtonState == LOW ) && ( prevButtonState == HIGH ) ) {
        // Button Pressed. Turn on LED.
        digitalWrite(ledPin, HIGH);
      } else if( ( curButtonState == HIGH ) && ( prevButtonState == LOW ) ) {
        // Button Released. Turn off LED.
        digitalWrite(ledPin, LOW);
        // Start blinking now, but after a long delay.
        startBlinking == true;
        vTaskDelay(pdMS_TO_TICKS(2000));
      }

      prevButtonState = curButtonState;
      vTaskDelay(pdMS_TO_TICKS(200));
    }
    else
    {
      digitalWrite(ledPin, HIGH);
      vTaskDelay(pdMS_TO_TICKS(500));
      digitalWrite(ledPin, LOW);
      vTaskDelay(pdMS_TO_TICKS(500));
    }
  }
}

Note that you no longer need task2.

@aggarg yes thank you, i am able to solve the problem. Thank youu tooo @hs2 brother, thank you very much :heart:

Thank you for reporting back!