Pages: [1]   Go Down
Author Topic: Serial.print and interrupts problem  (Read 1806 times)
0 Members and 1 Guest are viewing this topic.
Argentina
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Code:
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:

Code:
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!
« Last Edit: November 25, 2012, 09:34:53 pm by monti73 » Logged

Western Australia
Offline Offline
Jr. Member
**
Karma: 1
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Basic rule with serial:
Divide BPS by 10 gives how bytes per sec, divide this into 1 and that is how long it takes to send a byte...
EG 9600bps = 960 into 1 = approx 0.001sec to send 1 byte
Your "tick" is 7200/60 into 1 = 0.009sec
Suggest to start with, change serial to as high as you can go (115k?) this will give you more time between data being sent to next tick
Logged

Argentina
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Basic rule with serial:
Divide BPS by 10 gives how bytes per sec, divide this into 1 and that is how long it takes to send a byte...
EG 9600bps = 960 into 1 = approx 0.001sec to send 1 byte
Your "tick" is 7200/60 into 1 = 0.009sec
Suggest to start with, change serial to as high as you can go (115k?) this will give you more time between data being sent to next tick


Hey thanks for answering! I really didn't know that about Serial.println, definitely I'm going to check it out and give it a try.

Thanks again!
Logged

Pages: [1]   Go Up
Jump to: