Ciklusidő mérés

A kikommentelt "last" engedélyezését követően jól látható a különbég, ha az Arduino némi terhelést is kap.

unsigned long pause_mil, prev_mil, current_mil, gap_mil; 
unsigned long counter, last_counter, diff_counter;
float time_of_loop, test;

void setup() {
  Serial.begin(9600);
  counter = 0;
  last_counter = 0;
  pause_mil = 1000;    // 1 sec
}

void loop() {
    counter++;
    current_mil = millis();  // read time
    if (abs(current_mil - prev_mil) > pause_mil) {   // standby time is over
      diff_counter = abs(counter - last_counter);
      last_counter = counter;
      prev_mil = current_mil;
      time_of_loop = 1000.0 * pause_mil / diff_counter;
      Serial.print("loop's in one second: ");
      Serial.print(diff_counter);
      Serial.print(", time of a loop: ");
      Serial.print(time_of_loop);
      Serial.println(" usec");
    }  
    // last:
    // for (int i = 0; i < 100; i++) {
    //    test = cos(i); 
    // }
}