A good routine provided by mem, for counting pulses:
#include <DateTime.h>
#include <LiquidCrystal.h>
int resetPin = 3; // pin 3 resets the time
int pinTachoInHz = 2 ; // D2 for Tacho Input - Measuring the Hz //mem changed 6 to 2
volatile byte TachoInCount = 0; // declare this as a byte if the count is always less than 255
// so you don't have to disable interrupts when using in loop
long intTachoInHz_Millis = 0;
//create object to control an LCD GMD1602K.
LiquidCrystal lcd(12, 11, 6, 7, 8, 9, 10); // mem changed 2 to 6 (why?)
void setup(){
pinMode(13,OUTPUT); // flash the LED each second
pinMode(resetPin, INPUT); // a button on this pin resets the time
digitalWrite(resetPin,HIGH); // this line enables pull-up
DateTime.sync( 0 ); // set time to zero
//Configure pin modes
pinMode(pinTachoInHz, INPUT);
digitalWrite(pinTachoInHz, HIGH); // Turn on pullup resistor
attachInterrupt(0, count, HIGH); // call count when pin goes high // changed 6 to 0 (see http://www.arduino.cc/en/Reference/AttachInterrupt
}
void loop(){
float actualSpeed;
float x;
if(digitalRead(resetPin) == LOW)
DateTime.sync( 0 ); // reset time to zero if button pressed
TachoInCount= 0; // reset the count
digitalWrite(13, LOW); // lights the LED
delay (900);
digitalWrite(13, HIGH);
delay (100);
/* ------------ Count pulses ---------------*/
actualSpeed = TachoInCount * 4; // you may not need the .98 factor
lcd.setCursor(4,1);
lcd.print("Hz");
lcd.print(" ");
x=actualSpeed;
lcdPrintFloat(x,2); // this prints with two decimal places
}
/* --------------- Function count ----------------------- */
void count(){
TachoInCount++;
}
/* -------------- Function LcdPrintFloat ---------------- */
void lcdPrintFloat( float x, byte precision){
// prints val on a ver 0012 text lcd with number of decimal places determine by precision
// precision is a number from 0 to 6 indicating the desired decimal places
if(x < 0.0){
lcd.print('-');
x = -x;
}
lcd.print ((long)x); //prints the integer part
if( precision > 0) {
lcd.print("."); //prints decimal point
unsigned long frac;
unsigned long mult = 1;
byte padding = precision -1;
while(precision--)
mult *=10; //"amplifies" decimal values
if(x >= 0)
frac = (x - int(x)) * mult;
else
frac = (int(x)- x) * mult;
unsigned long frac1 = frac;
while( frac1 /= 10 )
padding--;
while( padding--)
lcd.print("0");
lcd.print(frac,DEC) ;
}
}
It works. Fantastic. At last!
One question: as I have to read several pulse signals simultaneously, would Arduino be able to do that with this routine?
I read that there are only two interrupts available in my version of Arduino card (diecimila).
In my present project I need to read and process 4 pulse signals.
Thanks for the kind help.
OldBeaver