# Different arduino cycle time

Hello,

I would like to know why if I do a series of calculations in the void loop and calculate the time of each cycle with the millis function, results a different time in each cycle. I thought it should waste the same time to do the calculations in every cycle, because are always the same operations.

Thank you!

beamendezf:
Hello,

I would like to know why if I do a series of calculations in the void loop and calculate the time of each cycle with the millis function, results a different time in each cycle. I thought it should waste the same time to do the calculations in every cycle, because are always the same operations.

Thank you!

Well, obviously YOU are doing something wrong. If you don’t post your code, that is ALL that we can conclude.

This si the code, I print “Intervalo” to know the time of the cycle.

``````#include "EmonLib.h"             // Include Emon Library
EnergyMonitor emon1;             // Create an instance phase 1

double P_Total;
double S_Total;
double Q_Total;

unsigned long ms=0;
unsigned long ultimosms=0;
unsigned long Intervalo;

#include <SD.h>                 // Incluir librería para comunicación con SD
File logFile;                   // Fichero de lectura/escritura

void setup()
{

Serial.begin(9600);

emon1.voltage(0, 195, -1);  // Voltage: input pin, calibration, phase_shift
emon1.current(1, 60.05);      // Current: input pin, calibration

Serial.print(F("Iniciando SD ..."));
if (!SD.begin(53))
{
Serial.println(F("Error al iniciar"));
return;
}
}

void loop()
{
ultimosms = ms;                   //Cálculo de energía en kWh
ms = millis();
Intervalo = ms - ultimosms;

emon1.calcVI(20,2000);

logFile = SD.open("datos.txt", FILE_WRITE);   // Abrir archivo y escribir valor
if (logFile) {
emon1.serialprint();           // Print out all variables
logFile.print(", P=");
logFile.print(emon1.realPower);
logFile.print(", Vrms=");
logFile.print(emon1.Vrms);
logFile.print(", Irms=");
logFile.print(emon1.Irms);
logFile.print(", cosfi=");
logFile.print(emon1.powerFactor);
logFile.print(", P_Total=");
logFile.print(P_Total);
logFile.print(", Eacum=");

Serial.println(Intervalo);

logFile.close();

}
else {
Serial.println("Error al abrir el archivo");
}

}
``````

Foro.ino (1.81 KB)

On every pass through loop(), you open and close a file. You seem to assume that that will always take exactly the same length of time. I do not believe that that is a reasonable assumption.

``````  kWhAcumulado = kWhAcumulado + P_Total * Intervalo * (1/1000/3600);
``````

Let’s look at that statement. 1/1000 = 0. 0/3600 = 0. P_Total * Intervalo * 0 = 0. So, that statement boils down to

``````  kWhAcumulado = kWhAcumulado;
``````

Hardly useful.

What variation are you seeing in execution times?

Thank you.
Probably you are right about to open and close the file, I didn't think about that.
The maximum variation is about 2 seconds, so I consider that is quite high.

beamendezf:
Thank you.
Probably you are right about to open and close the file, I didn't think about that.
The maximum variation is about 2 seconds, so I consider that is quite high.

I don't.

If you need to log data more often, open the file, loop, logging data at whatever (reasonable) interval you want (using the blink without delay technique, NOT delay(), then close the file.