Leonardo: Counter++ "hickups"? Different results for 1 Second

Hi there,

currently I get used to Arduino and do small steps on my way to understand how Arduino works.
I am now playing with interrupts. For this I just loop a counter and set it back to "0" when the interrupt occurs.
So Ardunio Leo 1 does the "Blink" example only with changed delay times.
Arduino Leo 2 has this interrupt code.

unsigned long counter1 = 0;
unsigned long time1 = 0;
unsigned long time2 = 0;

void setup() {
  // put your setup code here, to run once:
  attachInterrupt(0, ausgabe, RISING);
}

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

void ausgabe()
{
  time2 = micros();
  Serial.print(counter1);
  Serial.print(":");
  Serial.println(time2-time1);
  counter1 = 0;
  time1 = micros();
}

So I just check how long the delay(1000) is in counter1 values. So 80% of all results are about the same with only slight variations. But the rest is twice or even 3 times as much as before. How are those "hickups" generated?
I checked the delay with micros to be sure it's not another reasons, but this seems to be okay.

What I try to do with the counter is to have a better time resolution than micros() can provide it. But those strange over the top results are an obstacle here.

Thank your for each clue :).

Andreas

You need to start over with your understanding of interrupts. Interrupt handlers must be fast. Serial printing is not. In fact, serial printing relies on interrupts being enabled, which they are not when an interrupt service routine is called.

time1 and time2 are pretty dumb names. They represent the time that some event occurred, presumably. Name the variable to reflect the event of interest.

Finally, variables that are to be used in ISRs and in other functions must be volatile. counter is not.

PaulS:
You need to start over with your understanding of interrupts. Interrupt handlers must be fast. Serial printing is not. In fact, serial printing relies on interrupts being enabled, which they are not when an interrupt service routine is called.

time1 and time2 are pretty dumb names. They represent the time that some event occurred, presumably. Name the variable to reflect the event of interest.

Finally, variables that are to be used in ISRs and in other functions must be volatile. counter is not.

And finally finally, variables that are referenced outside an ISR that are not atomic operations should be encased in a critical section, so the variable doesn't get modified half way through an operation.