SOLVED! - Frequency execution change with no sense. Please Help

Hi,

When I try to create a simple delay using Arduino Due with "while" and "for" instructions, with no "delay" or "millis" functions, I have observed the time varies with no sense...
I have modified the blink example with the "millis" function to obtain the OFF time and a "while" loop creating the ON time. With a charge value of 17000 I can see a 1ms ON delay but if I put this part inside a for loop to create a 5ms ON delay, the time is reduced up to 5us... Not ms but microseconds...
Can someone help me to understand this phenomenon?
Thank you very much!!!

This is the code:

unsigned long cont;
unsigned int i;
 
void setup() {
 
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, LOW);  // turn the LED on (HIGH is the voltage level)
  cont = millis();
  while (millis()<cont+7)
  {}    
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED off by making the voltage LOW
   for (i=0; i<5; i++)
  {
    cont = 0;
    while (cont < 17000)
    {
      cont++;
    }
  }
}  

How do you know it was reduced by 5us?

Declare cont as 'volatile' and you will see a difference.

The compiler is very good at optimizing code by removing code that is not doing anything useful. It can detect that cont is not needed (you create cont, save millis() to it, increment it, but never read the value back so therefore never actually use it). The entire for loop will be completely removed.

Thank you very much for your fast answer Delta_G.
I agree with you about the non ideality of this method... But this is not the point of this question for me. The real strange thing is: The first while loop functions ok (of course with no consistency about time as you suggested). But when I add the second for loop, the program doesn't work propperly; the On time of the signal is the same as with no delay loops. I have also observed that if I add a digitalWrite (to other pin not involved as showed) the program runs perfectly. Can you help me with that?

Thank you very much again

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(9, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, LOW);  // turn the LED on (HIGH is the voltage level)
  cont = millis();
  while (millis()<cont+7)
  {}    
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED off by making the voltage LOW
   for (i=0; i<5; i++)
  {
    digitalWrite(9, HIGH);
    cont = 0;
    while (cont < 85000)
    {
      cont++;
    }
  }
}

Hi jim-p. I used an oscilloscope to see that the time changes from 1ms to 5us.

Thank you for your fast response

Thank you very much David_2018 for your fast answer. You're completely right!!! When I changed the variables declaration all changed. Thank you very much.

Remember that cont is defined as an unsigned integer and so 85000 is way too big to fit that number.

However, as said counting loops is a rubbish way to count time. This method would only work in machine code, we are coding in C/CPP here and you have a compiler in the way of what you write and what you get.

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