zero inconsistent timing

running simple code turning on pin 2 and turning it off in the loop. Using scope, pin is high for 1.2 used but sometimes 3.2 used. loop time is 5.2 used but sometimes 6.9 us.

Very big problem as need consistent workings, not erratic. What is going on here?

const int statusPin = 2; // status pin

void setup()
{
pinMode(statusPin,OUTPUT);
}

void loop()
{
digitalWrite(statusPin,HIGH);
digitalWrite(statusPin,LOW);
}

Timer interrupts.

Please get into the habit of using code tags when posting code.

I don't know why it's inconsistent, but that's a lousy way to do timing. I assume there is some stuff running on the processor that you don't know about. As you probably know, the processor isn't running your C/C++ code... It's running the compiled machine language. Maybe it's related to the bootloader "staying alive" in the background?

You might have to use a timer library or write some assembly code to take total-control over the processor. (With assembly/machine language you can look-up the number of processor cycles required for each particular instruction.)

With longer time periods (where the time to perform each instruction doesn't slow you down too much) you can use millis() and micros() for more accurate/consistent timing.

But... Thanks for doing that experiment! I've sometimes wondered how fast you could loop.

Maybe it's related to the bootloader "staying alive" in the background?

Nope.

I am not timing anything. Just want to see if the Arduino make sense cycle wise.

What does 'nope' mean?

This is compiled code so timing would be consistent. Does Arduino take time outs from the code for whatever?

For 48MHz Arm processor, this is a slow machine.

Hmm...this is interesting. The reason you're seeing variation in your pulses is because interrupts are firing and the Arduino is servicing their ISRs. One common ISR you'd encounter is the one used for micros/millis. Apparently the Arduino includes that stuff even if your sketch doesn't use it.

You will find this code gives consistent pulses because it doesn't allow the Arduino to initialize any of its own features:

const int statusPin = 2; // status pin

int main()
{
  pinMode(statusPin,OUTPUT);
  while(1)
  {
    digitalWrite(statusPin,HIGH);
    digitalWrite(statusPin,LOW);
  }

  return 0;
}

That said, I agree with the other people that this is not the right way to get consistent pulses out of your Arduino.

AWOL is correct ... try turning off the timer interrupt.

TIMSK0 = 0;

You should use direct port manipulation instead of digitalWrite() if you want speed.

BigBobby: that explains it. Where do I find docs that describe these nuances not included in the Learning Reference? I can live with the sneaks, just wanted to know about them once I found them.

Dlloyd: Where do I find the notes on manipulating the timer interrupt function? Are the processor registers mapped into the arduino code and where do I find them?

kitch:
BigBobby: that explains it. Where do I find docs that describe these nuances not included in the Learning Reference? I can live with the sneaks, just wanted to know about them once I found them.

Well I've been in the Arduino for a couple months now, and I just learned in your thread that the timer ISR gets created even if you aren't using it :confused:

To be fair the Arduino isn't trying to be sneaky, but trying to make it easy for new programmers to do things like measure time and write to the serial port. If you don't want it to do that stuff you can use main() like in my example. You'd then handle things like your timers directly, using the #defines and functions available from avr-libc -> http://www.atmel.com/webdoc/AVRLibcReferenceManual/group__avr__io.html

In the standard installation of the Arduino IDE on Windows 7, you'll find these header files in this directory -> C:\Program Files (x86)\Arduino\hardware\tools\avr\avr\include\avr

For example, you have millis begining updated every 1ms.

.

kitch:
Dlloyd: Where do I find the notes on manipulating the timer interrupt function? Are the processor registers mapped into the arduino code and where do I find them?

Quite new at this myself ... all source code is provided with the IDE, however I find GitHub quite handy. Also, of course, the datasheet for your board's MCU is needed.

Some useful links: pins_arduino.h more... Atmel