Reading the frequency of 3 x Square wave outputs from a tacho (DC Axial Fan)

Hello I am trying to read the output of 3 x dc axial fan outputs, I am using a nano (to keep size as small as possible). I am converting the rpm into hertz using basic calculations within the program. I am using an LCD to display the outputs and an I2C driver.

Currently I have the project working as intended but as time passes the frequencies displayed seem to increase yet I know the fan speed does not. After some reading I think I need to attach interrupts within my program but I am unsure of how to do this. I am new to arduino and programming so any other advice on maybe neatening up my current program or ways to improve it that would be greatly appreciated.

Tom Linaker

#include <LiquidCrystal_I2C.h>, <Wire.h> //Telling the prgram which libraries to include.
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, HIGH); //Defining the LCD pins making use of an I2C adapter.
int ledpin2 = 4, ledpin1 = 5, ledpin = 6, pulsepin2 = 7, pulsepin1 = 8, pulsepin = 9; //Defining certain pins with actaul names.
float value, value1, value2, answer, answer1, answer2; //Creating variables with specific names.
void setup() {
lcd.begin(16,2); //Telling the LCD to initiate.
lcd.backlight(); //Turning the LCD backlight on.
lcd.print(" Initialising"), delay(1000); //Delay to allow the fans to run up to their max RPM.
lcd.print(" Initialising"), delay(1000);
pinMode(pulsepin, INPUT), (pulsepin1, INPUT), (pulsepin2, INPUT);} //Defining pins as input or output.
void loop() {
lcd.home(); //Sets the cursor bac to default position row 0, column 1.
value2 = pulseIn(pulsepin2, HIGH); //Create a variable which calculates the time taken for the input to go from high to low
value1 = pulseIn(pulsepin1, HIGH);
value = pulseIn(pulsepin, HIGH);
answer2 = (value2/2)*0.1667; //Taking the above cretaed value and carrying out a calulation to convert RPM to Hz and making the answer into another variable
answer1 = (value1/2)*0.1667;
answer = (value/2)*0.1667;
if (answer > 0){lcd.print(answer);lcd.print(“Hz Fan 1”);delay(4000);lcd.clear();} //prints the answer to the screen showing the frequency of each fan
if (answer1 >= 70 and answer <= 150){digitalWrite(ledpin1, HIGH);} else {digitalWrite(ledpin1, LOW);}
if (answer1 > 0){lcd.print(answer1);lcd.print(“Hz Fan 2”);delay(4000);lcd.clear();}
if (answer2 >= 70 and answer <= 150){digitalWrite(ledpin2, HIGH);} else {digitalWrite(ledpin2, LOW);}
if (answer2 > 0){lcd.print(answer2);lcd.print(“Hz Fan 3”);delay(4000);lcd.clear();}

Your code makes it very hard to determine where setup() and loop() end.

The accepted styles ALL call for the } to be on a line BY ITSELF.

ONE statement per line.

pulseIn() does NOT return a float. If you need to manipulate the unsigned long returned by pulseIn() as a float, use a cast.

value2 = pulseIn(pulsepin2, HIGH);
value1 = pulseIn(pulsepin1, HIGH);
value = pulseIn(pulsepin, HIGH);

Do you count "uh-huh, two, three, ..."? Or do you count "1, 2, 3..."? Number ALL related variables, or, even better, use an array.

answer, answer1, and answer2 seem like dumb names for fan speeds.

Thank you I will make the changes now I appreciate the help