If Statement problem

Practically, this delay is required in the empty loop() function for the tasks to get executed under Arduino_FreeRTOS.

Waiting for what? There is no reason that an empty loop would behave any differently.

a7

Please tell me where you learned this. It seems implausible, but I am always willing to be wrong, especially about things I am sure of.

a7

I know, and it could be coincidence, but I have experienced that code works better if the void loop() isn't empty. So since that project, I always add a small delay(1); at the end of my void loop() . Could be superstition, but it makes sense in my head if it really made a difference.

1 Like
  • Yes
  • Well, we need do to be comfortable don’t we :thinking:
1 Like

Below is my latest code.


// defines pins
#define dirPin 10
#define stepPin 11
bool rotateMotor = false;
int i = 1;

void setup() {
  // Sets the two pins as Outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  // Start the motor
  rotateMotor = true;
  Serial.begin(9600);
}

void loop() {
  if (rotateMotor = true; i <= 5) {
    i++;
    Serial.println(i);
    digitalWrite(dirPin, HIGH);  // Enables the motor to move in a par
    for (int x = 0; x < 1600; x++) {
      digitalWrite(stepPin, HIGH);
      // by changing the time delay between the steps below
      delayMicroseconds(100);
      digitalWrite(stepPin, LOW);
      // Do not mess with this delay as it will throw things off.
      delayMicroseconds(200);
    }
  }
}

and this is the serial monitor output -
22
3
4
5
6

As you might have guessed the code does not work as I need it too. How do i fix it and get the print out of 1,2,3,4,5?

  • Check the syntax for if(. . . )

Below is the latest code -


// defines pins
#define dirPin 10
#define stepPin 11
bool rotateMotor = false;
int i = 0;

void setup() {
  // Sets the two pins as Outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  // Start the motor
  rotateMotor = true;
  Serial.begin(9600);
}

void loop() {
  if (rotateMotor = true; i <= 4) {
    i++;
    Serial.println(i);
    digitalWrite(dirPin, HIGH);  // Enables the motor to move in a par
    for (int x = 0; x < 1600; x++) {
      digitalWrite(stepPin, HIGH);
      // by changing the time delay between the steps below
      delayMicroseconds(100);
      digitalWrite(stepPin, LOW);
      // Do not mess with this delay as it will throw things off.
      delayMicroseconds(200);
    }
  }
}

Ok so I fixed the numbers and now the serial monitor says this -
11
2
3
4
5

So whats causing the 11?

  • Please tell us what you think this line of code does ?

It checks to see if the condition is true. if it is then it sees if the variable i is less then or equal to 4

Read this, carefully (especially the Example Code and Notes & Warnings):

https://docs.arduino.cc/language-reference/en/structure/control-structure/if/

1 Like
  • Maybe you mean:
if (rotateMotor == true && i <= 4) 

@cyberman42, your are flailing.

To do something N times, use a for loop:

https://docs.arduino.cc/language-reference/en/structure/control-structure/for/

To do something conditionally as flow goes through your code, use an if statement:

https://docs.arduino.cc/language-reference/en/structure/control-structure/if/

There is no roll for your variable rotateMotor. After you set it true, nothing ever makes it false.

Real documentation may seem like a slog, but it's better to learn how to read it than to guess, post, tinker and struggle.

The nice thing about this stuff is that it is completely logical and well understood. Nothing soft about software...

a7

  • Doesn’t a well written piece of code give you that soft cuddly feeling ? :smiling_face_with_three_hearts:
1 Like

What was your observation of Post #14¿

@GolamMostafa - out of curiosity I went and looked at examples for that RTOS offered on its github repository.

After the fourth example that had this

void loop()
{
  // Empty. Things are done in Tasks.
}

I concluded that your assertion is nonsense. And now please don't tell me where you learned that and please stop learning things from wherever it was.

a7

1 Like

This one I have seen while praticing the Forum/IDE examples on multi-tasking using Arduino_FreeRTOS -- and yet I might be wrong. Please, let me find that example (s).

I am talking abut the following kind of example which contains a delay() function in the loop().

#include <Arduino_FreeRTOS.h>

void TaskBlink(void *pvParameters); // Blink task function prototype

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);

  // Create a FreeRTOS task
  xTaskCreate(
    TaskBlink,          // Task function
    "Blink",            // Name (for debugging)
    128,                // Stack size
    NULL,               // Parameter to pass
    1,                  // Priority
    NULL                // Task handle
  );
}

void loop() {
  // loop() runs in the default idle task context in FreeRTOS
  Serial.println("Hello from loop()");
  delay(1000);  // This only blocks the loop() task, not the Blink task
}

// A simple task that blinks the LED every 500 ms
void TaskBlink(void *pvParameters) {
  (void) pvParameters;  // Unused

  while (1) {
    digitalWrite(LED_BUILTIN, HIGH);
    vTaskDelay(500 / portTICK_PERIOD_MS);  // FreeRTOS delay
    digitalWrite(LED_BUILTIN, LOW);
    vTaskDelay(500 / portTICK_PERIOD_MS);
  }
}

:white_check_mark: Why it’s good to have delay(1) in loop()

1. Cooperative Multitasking in FreeRTOS

In FreeRTOS, each task should periodically yield control so other tasks can run.

  • If loop() contains an infinite loop with no delay, it may monopolize CPU time, starving lower-priority tasks or even preventing the idle task from cleaning up.
  • Adding delay(1) makes the current task block for 1 ms, allowing the scheduler to switch to other tasks.

:backhand_index_pointing_right: delay(1) internally calls vTaskDelay(1) when FreeRTOS is running.

@GolamMostafa, please tell us who your friend there is, and ask it

In FreeRTOS we often see an empty loop(). Doesn't it need to yield to allow other tasks to run?

a7

Is friend's opinion of post #38 out-of-sense? If yes, then which one or all?

I also don't recognize this syntax in C/C++. I think it would be: for (int i = 0; i <= 5; i++) {
...
}