Pages: [1]   Go Down
Author Topic: Measure time accurately  (Read 710 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
The Netherlands
Offline Offline
Sr. Member
*****
Karma: 1
Posts: 287
don't panic...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

First a code snippet;
Code:
   
analogWrite(fan_pin, 0);                           // fan full throttle
oldHallRpmTime = micros();                     // store start time
attachInterrupt(0, hallInterrupt, RISING);  // analog2 = interrupt 0 on mega
while (hallCount < hallcounter)  {    }       // while we have not yet counted the desired hall interrupts, do nothing
detachInterrupt(0);                                  // analog2 = interrupt 0 on mega    
unsigned long TimeuS = micros();            // store stop time
analogWrite(fan_pin, reqSpeed);              // fan back to setted pwm speed
 

This part of code gets executed once every couple of seconds.
What it does is;
Set a pwm-controlled pc-fan full speed (otherwise the hallsensor in the fan won't work)
Store time in microseconds. (i know it has a resolution of four microseconds -> reference)
Attach an interrupt to count hall-sensor ticks. (isr is just a 'hallCount++' nothing more)
When there are enough (on top of sketch #define hallcounter 4) hall interrupts counted
Detach the hall-sensor interrupt
Store end time in microseconds
Set the original desired speed.

After that the counts and time are used to calculate rotations per minute. That works fairly well when the program collects 20 or so hall interrupts. Doing so will speed up the fan abviously.

To limit the time spend at full throttle i've let the program count to 3 (this will be 1.5 turn of the fan)instead of 20 hall-interrupts. The problem starts. In this situation the rotations per minute are not correct anymore. They are about 10% too high.

Could this be that the time the program uses is not accurate enough? (multiples of 4 microseconds) or do the surrounding functions (attach- ,detach interrupt and maybe storing the time) take relative too long?

regards,

Jeroen
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why are you attaching and detatching the interrupt, instead of simply resetting a variable?

What sort of intervals are being measured?
Logged

Per Arduino ad Astra

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A different approach that may be simpler is to do some pulseIn measurements to get an average time between pulses. If you know the distance between hall pulses you can calculate the speed using the results from the pulseIn calls, without the complexity of the interrupt handling.  
« Last Edit: April 06, 2010, 09:46:16 am by mem » Logged

Global Moderator
The Netherlands
Offline Offline
Sr. Member
*****
Karma: 1
Posts: 287
don't panic...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your time and sorry for the missing info. smiley

The hardware;
pc-fan (3 wires, + - and hall-sensor) speed controlled by transistor and mosfet/ arduino pwm.
hall sensor connected to external interrupt 0 (pwm pin 2 on a mega)

hmmmm. why did i attach- and detach the interrupt? Because it isn't needed somewhere else.  Your question led me to make a small change to the code which gave slightly better results.
Code:
int hallTicks = 0;
attachInterrupt(0, hallInterrupt, RISING);  // analog2 = interrupt 0 on mega
analogWrite(fan_pin, 0);                           // fan full throttle
oldHallRpmTime = micros();                     // store start time
hallcount = 0;                                         // reset interrupt counter
while (hallCount < hallcounter)  {    }       // while we have not yet counted the desired hall interrupts, do nothing
hallTicks = hallCount;
unsigned long TimeuS = micros();            // store stop time
analogWrite(fan_pin, reqSpeed);              // fan back to setted pwm speed
detachInterrupt(0);                                  // analog2 = interrupt 0 on mega    

The intervals measured are those in between every hall-sensor signal. One full rotation gives 2 'ticks'

@ mem
Will look into the pulseIn-way. The playground example used interrupts so my mind was in that box. smiley

Thanks so far all,
Jeroen

edit:  sorry for the forum messing with the spacing for the comments.
« Last Edit: April 06, 2010, 10:29:06 am by Yot » Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would expect pulsIn to be simpler and at least as accurate.
Logged

Pages: [1]   Go Up
Jump to: