1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
void setup()
{
// set up interupt 0 (which is pin 2) and turn on the pull-up resistor as we
// would if the pin had been set to be an input. This is so that when the
// switch on the pin is open, it is not a floatin signal and has 5V across it
// (albeit through a high-resitance resistor to heavily constricy the
// current).
attachInterrupt( 0, rpmCounter, RISING );
digitalWrite( 2, HIGH );
// init serial comms
Serial.begin( 9600 );
}
static unsigned long fan_rotations = 0;
void rpmCounter()
{
fan_rotations++;
}
void loop()
{
// get milliseconds
unsigned long start_millis = millis();
// display fan speed. We divide by 2 because the fan's "sense" pin actually
// pulses twice per revolution. We multiply by 60 because fan_rotations will
// have counted the number of rotations in a second and we want RPM.
// Serial.println( fan_rotations );
Serial.print( "RPM: " );
Serial.println( fan_rotations / 2 * 60 );
fan_rotations = 0;
// wait until the remainer of a second has elapsed
delay( start_millis + 1000 - millis() );
}
|