Hi to everyone.
I am making a project (Hard Drive POV clock) in wich I need to read te RPM (or the time each revolution takes) of a hard drive using a magnet and a hall effect sensor. I have the hardware and the code up and running but I noticed that when I use Serial.println to print values, others values changes... and I don't know why. Here is the code and an example to explain it better
volatile unsigned long vueltas = 0;
float tiempoVuelta = 0;
unsigned long tiempoAnterior = 0;
void setup()
{
Serial.begin(9600);
attachInterrupt(0, rpm_fun, FALLING); //interrupt 0 -> pin 2
}
void rpm_fun() // INTERRUPTION
{
vueltas++;
}
void cuentavueltas() // tiempoVuelta = time of each revolution
{
if(vueltas>=1)
{
tiempoVuelta = (micros()- tiempoAnterior)/ vueltas;
tiempoAnterior = micros();
vueltas=0;
Serial.print("Total: ");
Serial.println(tiempoVuelta/12);
Serial.print("Division: ");
Serial.println((tiempoVuelta/12)*0.02);
Serial.print("Espacio: ");
Serial.println((tiempoVuelta/12)*0.98);
}
return;
}
I read:
Total: 707.17
Division: 14.14
Espacio: 693.02
But if I comment some Serial.print lines:
if(vueltas>=1)
{
tiempoVuelta = (micros()- tiempoAnterior)/ vueltas;
tiempoAnterior = micros();
vueltas=0;
Serial.print("Total: ");
Serial.println(tiempoVuelta/12);
// Serial.print("Division: ");
// Serial.println((tiempoVuelta/12)*0.02);
// Serial.print("Espacio: ");
// Serial.println((tiempoVuelta/12)*0.98);
}
I read Total: 648.33
Wich is the correct value for tiempoVuelta/12 ?? And why I am getting that difference? Is it because of the time it takes to print the values to the serial monitor? Waht would be a better way to do this? The disc is spinning at 7200 rpm.
Thank you very much!