# Challenge 2 of the rtos course . What did I do wrong

**URL:** <https://forums.freertos.org/t/challenge-2-of-the-rtos-course-what-did-i-do-wrong/23781>\
**Category:** Kernel\
**Created:** [August 21, 2025, 6:22pm UTC](https://forums.freertos.org/t/challenge-2-of-the-rtos-course-what-did-i-do-wrong/23781 "2025-08-21T18:22:24Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![RoelofW](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@RoelofW](https://forums.freertos.org/u/RoelofW)\
**Post date:** [August 21, 2025, 6:22pm UTC](https://forums.freertos.org/t/challenge-2-of-the-rtos-course-what-did-i-do-wrong/23781/1 "2025-08-21T18:22:24Z")

</div>

Hello,

I have to make code that contains 2 tasks.  
One that blink the build\_in led with a delay  
One that reads a number from the serial monitor that can change the delaytime of the built in led

So I have this :

```auto
// my solution to lesson 3 

// Learned so far 
// - writing task 
// - task scheduling 

// Challenge 

// Write 2 tasks. 
// On to display a led and one where a user can enter a number which delays the blinking 

#if CONFIG_FREERTOS_UNICORE
static const BaseType_t app_cpu = 0 ; 
#else 
static const BaseType_t app_cpu = 1; 
#endif 

// Pins 
static const int led_pin = 2;

int delayTime = 500; 

//Our task blink a led 
void toggleLed(void * parameter) {
 while(1) {
   digitalWrite(led_pin, HIGH);
   vTaskDelay(delayTime / portTICK_PERIOD_MS ); 
   digitalWrite(led_pin, LOW); 
   vTaskDelay(delayTime / portTICK_PERIOD_MS);
 }
}

// Our task is to read in a number 
void changeBlinkRate(void * parameter) {
 char ch; 
 char buff[20];
 int idx; 

 //clear the buffer
 memset(buff, 0, 20);

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

   if (ch == '\n') {
     delayTime = atoi(buff);
     Serial.print("Updated LED delay to: ");
     Serial.println(delayTime);

     // clear buffer 
     memset(buff, 0, 20);
     idx = 0 ; 
   } else {
     if (idx < 19) {
       buff[idx] = ch; 
       idx++;
     }
   }
}}

void setup() {
 pinMode(led_pin, OUTPUT); 
 Serial.begin(9600); 
 Serial.println("RW"); 

 // Task to run forever 
  xTaskCreatePinnedToCore(
   toggleLed, // Function to be called
   "ToggleLed", // name of the function 
   1024, // stack size
   NULL, // parameter to pass to the function 
   1, // priority
   NULL , // task handle
   app_cpu
 );

 // Task to run forever 
  xTaskCreatePinnedToCore(
   changeBlinkRate, // Function to be called
   "ChangeBlinkRate", // name of the function 
   1024, // stack size
   NULL, // parameter to pass to the function 
   1, // priority
   NULL , // task handle
   app_cpu
 );
}

void loop() {

}

```

But on wokwiki I do not see the serial monitor appear and the led does not blink.  
So what I did not understand.

Till now I learned only about writing task and task priority

---

<div class="post-metadata">

**Author:** ![tony-josi-aws](https://avatars.discourse-cdn.com/v4/letter/t/a6a055/32.png) [@tony-josi-aws](https://forums.freertos.org/u/tony-josi-aws)\
**Post date:** [August 22, 2025, 3:42am UTC](https://forums.freertos.org/t/challenge-2-of-the-rtos-course-what-did-i-do-wrong/23781/2 "2025-08-22T03:42:28Z")

</div>

The `changeBlinkRate` task’s logic is incorrect; it’s not structured to continuously read from the serial port. It just runs once and returns.

[Refer](https://www.freertos.org/Documentation/02-Kernel/04-API-references/01-Task-creation/01-xTaskCreate):

> Tasks are normally [implemented as an infinite loop](https://www.freertos.org/Documentation/02-Kernel/02-Kernel-features/01-Tasks-and-co-routines/05-Implementing-a-task); the function which implements the task must never attempt to return or exit.

Instead of the current logic, it should be an infinite loop similar to the LED task, where it checks for serial data and processes its once its available.

---

<div class="post-metadata">

**Author:** ![RoelofW](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@RoelofW](https://forums.freertos.org/u/RoelofW)\
**Post date:** [August 22, 2025, 5:24am UTC](https://forums.freertos.org/t/challenge-2-of-the-rtos-course-what-did-i-do-wrong/23781/3 "2025-08-22T05:24:40Z")

</div>

Still no luck with this change

```auto
// my solution to lesson 3

// Learned so far
// - writing task
// - task scheduling

// Challenge

// Write 2 tasks.
// On to display a led and one where a user can enter a number which delays the blinking

#if CONFIG_FREERTOS_UNICORE
static const BaseType_t app_cpu = 0 ;
#else
static const BaseType_t app_cpu = 1;
#endif

// Pins
static const int led_pin = 2;

int delayTime = 500;

//Our task blink a led
void toggleLed(void * parameter) {
  while (1) {
    digitalWrite(led_pin, HIGH);
    vTaskDelay(delayTime / portTICK_PERIOD_MS );
    digitalWrite(led_pin, LOW);
    vTaskDelay(delayTime / portTICK_PERIOD_MS);
  }
}

// Our task is to read in a number
void changeBlinkRate(void * parameter) {
  char ch;
  char buff[20];
  int idx;

  while (1) {
    //clear the buffer
    memset(buff, 0, 20);

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

      if (ch == '\n') {
        delayTime = atoi(buff);
        Serial.print("Updated LED delay to: ");
        Serial.println(delayTime);

        // clear buffer
        memset(buff, 0, 20);
        idx = 0 ;
      } else {
        if (idx < 19) {
          buff[idx] = ch;
          idx++;
        }
      }
    }
  }
}

void setup() {
  pinMode(led_pin, OUTPUT);
  Serial.begin(9600);
  Serial.println("RW");

  // Task to run forever
  xTaskCreatePinnedToCore(
    toggleLed, // Function to be called
    "ToggleLed", // name of the function
    1024, // stack size
    NULL, // parameter to pass to the function
    1, // priority
    NULL, // task handle
    app_cpu
  );

  // Task to run forever
  xTaskCreatePinnedToCore(
    changeBlinkRate, // Function to be called
    "ChangeBlinkRate", // name of the function
    1024, // stack size
    NULL, // parameter to pass to the function
    1, // priority
    NULL, // task handle
    app_cpu
  );
}

void loop() {

}

```

Still no blinking and the serial monitor is not visible

> **[RTOS\_lesson3 - Wokwi ESP32, STM32, Arduino Simulator](https://wokwi.com/projects/439915821422488577)**
>
> Run IoT and embedded projects in your browser: ESP32, STM32, Arduino, Pi Pico, and more. No installation required!

---

<div class="post-metadata">

**Author:** ![tony-josi-aws](https://avatars.discourse-cdn.com/v4/letter/t/a6a055/32.png) [@tony-josi-aws](https://forums.freertos.org/u/tony-josi-aws)\
**Post date:** [August 22, 2025, 7:56am UTC](https://forums.freertos.org/t/challenge-2-of-the-rtos-course-what-did-i-do-wrong/23781/4 "2025-08-22T07:56:07Z")

</div>

I can see the LED blinking in the default scenario, how are you providing the serial input?

---

<div class="post-metadata">

**Author:** ![RoelofW](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@RoelofW](https://forums.freertos.org/u/RoelofW)\
**Post date:** [August 22, 2025, 9:14am UTC](https://forums.freertos.org/t/challenge-2-of-the-rtos-course-what-did-i-do-wrong/23781/5 "2025-08-22T09:14:15Z")

</div>

you are right.

I was assuming that this

```auto
Serial.begin(9600);
  Serial.println("RW");

```

take care that the serial monitor will pop up.

And if you mean the reading that will be this code :

```auto
if (Serial.available() > 0) {
      ch = Serial.read();

```

Which can be found in changeBlinkRate at the beginning

---

<div class="post-metadata">

**Author:** ![tony-josi-aws](https://avatars.discourse-cdn.com/v4/letter/t/a6a055/32.png) [@tony-josi-aws](https://forums.freertos.org/u/tony-josi-aws)\
**Post date:** [August 22, 2025, 12:18pm UTC](https://forums.freertos.org/t/challenge-2-of-the-rtos-course-what-did-i-do-wrong/23781/6 "2025-08-22T12:18:38Z")

</div>

> [@RoelofW](#):
>
> I was assuming that this
> 
> ```auto
> Serial.begin(9600);
> Serial.println("RW");
> 
> ```
> 
> take care that the serial monitor will pop up.

I don’t think it will automatically create a serial monitor, intead I think a serial monitro needs to be attached to your ESP32 via wokwi UI, I might be wrong here as I haven’t used wokwi before.

---

<div class="post-metadata">

**Author:** ![RoelofW](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@RoelofW](https://forums.freertos.org/u/RoelofW)\
**Post date:** [August 22, 2025, 12:22pm UTC](https://forums.freertos.org/t/challenge-2-of-the-rtos-course-what-did-i-do-wrong/23781/7 "2025-08-22T12:22:12Z")

</div>

According to this it schould work :

> **[ESP32 - Serial Monitor | ESP32 Tutorial](https://esp32io.com/tutorials/esp32-serial-monitor)**
>
> Learn: how to program ESP32 step by step. The detail instruction, code, wiring diagram, video tutorial, line-by-line code explanation are provided to help you quickly get started with ESP32. Find this and other ESP32 tutorials on esp32io.com.

---

<div class="post-metadata">

**Author:** ![RoelofW](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@RoelofW](https://forums.freertos.org/u/RoelofW)\
**Post date:** [August 22, 2025, 12:54pm UTC](https://forums.freertos.org/t/challenge-2-of-the-rtos-course-what-did-i-do-wrong/23781/8 "2025-08-22T12:54:54Z")

</div>

oke

I asked also the wokwiki people for help and it seems that you have to change the connection string to

```auto
  "connections": [[ "esp:TX0", "$serialMonitor:RX", ""], ["esp:RX0", "$serialMonitor:TX", ""] ]

```

---

<div class="post-metadata">

**Author:** ![RoelofW](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@RoelofW](https://forums.freertos.org/u/RoelofW)\
**Post date:** [August 22, 2025, 1:57pm UTC](https://forums.freertos.org/t/challenge-2-of-the-rtos-course-what-did-i-do-wrong/23781/9 "2025-08-22T13:57:34Z")

</div>

Still something wrong with the code  
I now can enter some number but I cannot see that the blinkRate task is executed.

What is more wrong here in my code ?

---

<div class="post-metadata">

**Author:** ![RoelofW](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@RoelofW](https://forums.freertos.org/u/RoelofW)\
**Post date:** [August 22, 2025, 3:30pm UTC](https://forums.freertos.org/t/challenge-2-of-the-rtos-course-what-did-i-do-wrong/23781/10 "2025-08-22T15:30:05Z")

</div>

oke

That one is solved but still delaytime is zero whatever I fill in in the serial monitor

Can someone give me a hint what is still wrong at my code

> **[RTOS\_lesson3 - Wokwi ESP32, STM32, Arduino Simulator](https://wokwi.com/projects/439915821422488577)**
>
> Run IoT and embedded projects in your browser: ESP32, STM32, Arduino, Pi Pico, and more. No installation required!

---

<div class="post-metadata">

**Author:** ![tony-josi-aws](https://avatars.discourse-cdn.com/v4/letter/t/a6a055/32.png) [@tony-josi-aws](https://forums.freertos.org/u/tony-josi-aws)\
**Post date:** [August 24, 2025, 7:12am UTC](https://forums.freertos.org/t/challenge-2-of-the-rtos-course-what-did-i-do-wrong/23781/11 "2025-08-24T07:12:33Z")

</div>

Your code seems to be working and it’s updating the `delayTime`, however wokwi seems to not be refreshing the LED status fast enough to see the blinks for higher blink frequencies (lower delayTime).

I can see the delay getting doubled when `delayTime` is set to 1000.

---

<div class="post-metadata">

**Author:** ![RoelofW](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@RoelofW](https://forums.freertos.org/u/RoelofW)\
**Post date:** [August 24, 2025, 9:59am UTC](https://forums.freertos.org/t/challenge-2-of-the-rtos-course-what-did-i-do-wrong/23781/12 "2025-08-24T09:59:23Z")

</div>

oke

and I found out that the delayTime was always zero because I did not give delayTime a value when I declared it.

For me this one is solved
