This morning I just plucked up with my heart and connected the output wire of my impeller's IR sensor to D2 pin of Arduino Nano '328. Before this I had changed the IR-emitter' resistor so consumption current has rised from 200 uA up to nominal 20 mA, so the output signal's amplitude has rised from 700 mV up to a bit less than 5V. This signal's level allowed me to engage the logical pin instead of analog comparator (it was made in order to simplify the first steps).
The sketch has been found here on the Forum, successfully compiled and loaded:
http://forum.arduino.cc/index.php/topic,75571.0.html
Here is the code itself:
/*
size = 3276
Version 1.0 B
To measure a frequency pulse and display in the serial monitor.
Connect the pulse source to pin 2.
The source have to be a +5 V to Gnd signal.
In this experiment, I use a second Arduino to generated
a pulse. A 555 in astable Mode can be use.
Just connect pin 3 of the 555 to pin 2 of the Arduino.
Program by Serge J Desjardins aka techone
Toronto, Ontario, Canada
Compile and Tested.
*/
byte freqpin = 2;
unsigned long timeone;
unsigned int frequency;
unsigned int timediff;
unsigned int tcount=0;
// sample time of 1 second or 1000 ms.
unsigned int sampletime=1000;
unsigned long sampletimetwo=1000000;
unsigned long j;
volatile unsigned int count=0;
void setup()
{
pinMode(freqpin, INPUT);
// set interrupt pin 2 and use rising ( from gnd to +5 )
attachInterrupt(0,freqinput,RISING);
Serial.begin(9600);
}
void loop()
{
/* Use while() loop and use millis().
Calculate the time difference and compare the
value with the sample time.
*/
// activate the interrupt
tcount=0;
count=0;
timediff=0;
attachInterrupt(0,freqinput,RISING);
//interrupts();
timeone=millis();
while(timediff<sampletime)
{
tcount=count;
timediff=millis()-timeone;
}
// de-activated the interrupt
detachInterrupt(0);
//noInterrupts();
// Calculated frequency
frequency=tcount;
// Display frequency
Serial.print("The difference of the delay is : ");
Serial.println(timediff,DEC);
Serial.print("Test while - ");
Serial.print("Frequency : ");
Serial.println(frequency,DEC);
delay(3000);
count=0;
tcount=0;
/* Using a for() loop design. no need for millis() but
you need to fine tune the constant sampletime to set
the proper time for sampling.
*/
// activate the interrupts
// interrupts();
attachInterrupt(0,freqinput,RISING);
timeone=millis();
for (j=0;j<sampletimetwo;j++)
{
tcount=count;
}
timediff=millis()-timeone;
// de-activated the interrupt
//noInterrupts();
detachInterrupt(0);
// Calculated frequency
frequency=tcount;
// Display frequency
Serial.print("The time difference is : ");
Serial.println(timediff,DEC);
Serial.print("Test Loop - ");
Serial.print("Frequency : ");
Serial.println(frequency,DEC);
Serial.println("\n");
delay(3000);
}
// The interrupt routine
void freqinput ()
{
count++;
}
Well, the difference between 'test 'while' and 'test 'for' is like 30 and 27 respectively, in average, i.e. about 3 .. 5 pps (Hz), as represented on screenshot below. What does it mean and what should I do next? Which result is true (or just more accurate)?
