Problem with the tachometer

Hi,

i wrote the code for the tachometer and works fine, now i am try to implement is with another code that includes a menu, after that the tachometer is not working correct, can you help me with that ? i attache my code

thanks

sketch_mar31a.ino (3.37 KB)

i attache my code

Please can you read this before posting a programming question and post the code here using [ code ] tags

Hi, i read that , now i am also adding the code as you mention

thanks
L

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//FOR CHANGING THE MENU-------------------------------------------
int inPin = 6;  
int buttonState = 0;
int lastButtonState = 0;
int page = 0;

//FOR THE DIFFERENCE OUTPUT-------------------------------------
int inPinOut = 9;

//for tacho ----------------------------------------------------
const int sensorPin = 10;
const int sensorInterrupt = 0;
const int timeoutValue = 5;

volatile unsigned long lastPulseTime;
volatile unsigned long interval = 0;
volatile int timeoutCounter;


void setup()
{{
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  Serial.begin(9600);
  // declare pushbutton as input
  pinMode(inPin, INPUT);  
  pinMode (inPinOut, OUTPUT);
}

//for tacho---------------------------------------------------

 pinMode(sensorPin, INPUT);
 digitalWrite(sensorPin, HIGH);    // enable internal pullup (if Hall sensor needs it)
 attachInterrupt(sensorInterrupt, sensorIsr, RISING);
 Serial.begin(9600);
 lastPulseTime = micros();
 timeoutCounter = 0;
}

void sensorIsr()
{
 unsigned long now = micros();
 interval = now - lastPulseTime;
 lastPulseTime = now;
 timeoutCounter = timeoutValue;
}




void loop() {

  buttonState = digitalRead(inPin); 
  lcd.setCursor(0,0);


 Serial.print(" RPM ");
 if (timeoutCounter != 0)
 
   --timeoutCounter;
   float rpm = 60e6/(float)interval;
   Serial.print(rpm, 1);
 
 Serial.println();
 delay(500);
 
//PRESSURE SENSORS----------------------------------------------------------
float sensorVoltage = analogRead(0);   // read the sensor voltage from analog pin 0 pump pressure
int Ppsi = ((sensorVoltage-95)/204)*50/4;

float sensorVoltage2 = analogRead(1);   // read the sensor voltage from analog pin 1 pressure from the filter
int Fpsi = ((sensorVoltage2-95)/204)*50/4;


int DpsiBar = (Ppsi-Fpsi);
int Dpsi = ((DpsiBar*100)/Ppsi);

float sensorVoltage3 = analogRead(2);   // read the sensor voltage from analog pin 2 for Max Pressure Difference
int PDL = ((sensorVoltage3)/1023)*10;


//Pressure comparator 

if (DpsiBar >= PDL)
{
  digitalWrite(inPinOut, HIGH); 
}
else
{
  digitalWrite(inPinOut, LOW); 
}


  switch (page) {
    case 0:
lcd.print ("Pump Pressure"); 
lcd.setCursor (0,1);
lcd.print (Ppsi);
lcd.print (" BAR");
Serial.println (Ppsi);
delay (500);
      break;




      
    case 1:
lcd.print ("Filter Pressure"); 
lcd.setCursor (0,1);
lcd.print (Fpsi);
lcd.print (" BAR");
delay (500);
      break;



      
    case 2:
lcd.print ("Filter Pres Diff"); 
lcd.setCursor (0,1);
lcd.print (Dpsi);
lcd.print (" %    ");
lcd.print (DpsiBar);
lcd.print (" BAR");
delay (500);
      break;


    case 3:
lcd.print ("Pressure Dif Limit"); 
lcd.setCursor (0,1);
lcd.print (PDL);
lcd.print (" BAR");
delay (500);
      break;


       
    case 4:
lcd.print ("Speed"); 
lcd.setCursor (0,1);
lcd.print (rpm);
lcd.print (" RPM");
delay (100);
      break;


    case 5:
lcd.print ("rilter Pressure"); 
lcd.setCursor (0,1);
lcd.print (Fpsi);
lcd.print (" BAR");
delay (500);
      break;


  }
 if (buttonState != lastButtonState)     // compare the buttonState to its previous state
  { 
    delay(800);               // if the state has changed, increment the counter
    if (buttonState == HIGH)                                    
    {
    lcd.clear();
      page++;     // if the current state is HIGH then the button changed
      if(page==6)
      {
        page=0;
      }
  }
   
}
}

quite difficult to understand the issue :frowning:

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);The LCD uses pin 2

const int sensorInterrupt = 0;

  attachInterrupt(sensorInterrupt, sensorIsr, RISING);So does the interrupt

UKHELIBOB,

what do you mean ? i did not get you :frowning:

thanks
Lazaros

The LCD uses pin 2

You are using interrupt 0, which on most Arduinos (you don't say which you are using), uses pin 2

Which board are you using and which pin is the tachometer connected to ?

i am using Arduino Uno R3 and the tachometer is connected on pin 10

thanks
Lazaros

the tachometer is connected on pin 10

Then it will never cause interrupt 0 to be triggered.

i also add it as

//for tacho ---------------------------------------------
const float sensorInterrupt = 10;
const int timeoutValue = 5;
volatile unsigned long lastPulseTime;
volatile unsigned long interval = 0;
volatile int timeoutCounter;

but did not work

here is my last attempt that work but only on pin 1 and 0 , i have to move the lcd pins on other pins

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12,11,7,6,5,4);

//FOR CHANGING THE MENU-------------------------------------------
int inPin = 8;  
int buttonState = 0;
int lastButtonState = 0;
int page = 0;

//FOR THE DIFFERENCE OUTPUT-------------------------------------
int inPinOut = 9;

//for tacho ---------------------------------------------
const float sensorInterrupt = 1;
const int timeoutValue = 5;
volatile unsigned long lastPulseTime;
volatile unsigned long interval = 0;
volatile int timeoutCounter;


void setup()
{{
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  Serial.begin(9600);
  // declare pushbutton as input
  pinMode (inPinOut, OUTPUT);
}

//for tacho---------------------------------------------------


 attachInterrupt(sensorInterrupt, sensorIsr, RISING);
 Serial.begin(9600);
 lastPulseTime = micros();
 timeoutCounter = 0;
}

void sensorIsr()
{
 unsigned long now = micros();
 interval = now - lastPulseTime;
 lastPulseTime = now;
 timeoutCounter = timeoutValue;
}




void loop() {

  buttonState = digitalRead(inPin); 
  lcd.setCursor(0,0);


 Serial.print(" RPM ");
 if (timeoutCounter != 0)
 
   --timeoutCounter;
   float rpm = 60e6/(float)interval;
   Serial.print(rpm, 1);
 
 Serial.println();
 delay(500);
 
//PRESSURE SENSORS----------------------------------------------------------
float sensorVoltage = analogRead(0);   // read the sensor voltage from analog pin 0 pump pressure
float Ppsi = ((sensorVoltage-105)/204)*12.5;

float sensorVoltage2 = analogRead(1);   // read the sensor voltage from analog pin 1 pressure from the filter
float Fpsi = ((sensorVoltage2-115)/204)*12.5;


float DpsiBar = (Ppsi-Fpsi);
int Dpsi = ((DpsiBar*100)/Ppsi);

float sensorVoltage3 = analogRead(2);   // read the sensor voltage from analog pin 2 for Max Pressure Difference
float PDL = ((sensorVoltage3)/1023)*10;


//Pressure comparator 

if (DpsiBar >= PDL)
{
  digitalWrite(inPinOut, HIGH); 
}
else
{
  digitalWrite(inPinOut, LOW); 
}


//------------------------------------------------Fuel Level 

float sensorVoltage4 = analogRead(3);   // read the sensor voltage from analog pin 2 for Max Pressure Difference
float FLEV = ((sensorVoltage4)/1023)*10;


  switch (page) {

       
    case 0:
lcd.print ("Speed"); 
lcd.setCursor (0,1);
lcd.print (rpm, 1);
lcd.print (" RPM");
delay (1000);

      break;
    
    case 1:
lcd.print ("Pump Pressure"); 
lcd.setCursor (0,1);
lcd.print (Ppsi,1);
lcd.print (" BAR");
Serial.println (Ppsi);
delay (500);
      break;




      
    case 2:
lcd.print ("Filter Pressure"); 
lcd.setCursor (0,1);
lcd.print (Fpsi,1);
lcd.print (" BAR");
delay (500);
      break;



      
    case 3:
lcd.print ("Filter Pres Diff"); 
lcd.setCursor (0,1);
lcd.print (Dpsi);
lcd.print (" % ");
lcd.print (DpsiBar);
lcd.print (" BAR");
delay (500);
      break;


    case 4:
lcd.print ("Pressure Dif Limit"); 
lcd.setCursor (0,1);
lcd.print (PDL,1);
lcd.print (" BAR");
delay (500);
      break;




    case 5:
lcd.print ("Fuel Level"); 
lcd.setCursor (0,1);
lcd.print (FLEV,1);
lcd.print (" BAR");
delay (100);
      break;


  }
 if (buttonState != lastButtonState)     // compare the buttonState to its previous state
  { 
    delay(500);               // if the state has changed, increment the counter
    if (buttonState == HIGH)                                    
    {
    lcd.clear();
      page++;     // if the current state is HIGH then the button changed
      if(page==6)
      {
        page=0;
      }
  }
   
}
}

i can not understand why it is not working on pin 10

also the tachometer in case there is no pulse it does not goes to 0 but still showing the last value and waiting for a pulse, any idea how can i move the value to zero in case there is no pulse?

i can not understand why it is not working on pin 10

Please read what attachInterrupt() has to say about which pins are used by which interrupts on a Uno

You cannot use pin 10 on a Uno as an external interrupt pin

also the tachometer in case there is no pulse it does not goes to 0 but still showing the last value and waiting for a pulse, any idea how can i move the value to zero in case there is no pulse?

Save the time from millis() when the tacho interrupt occurs. Then, each time through loop() test how long has elapsed since the last saved time. If it exceeds say 1 second (but you decide) then write zero to the display

UKHELIBOB

you are very polite and thanks for let me know , i just read that i can not use that pin, thanks again.

can you please let me know how can i count the time and write zero on the screen ???

thank you veyr much, i really appreciate your help

can you please let me know how can i count the time and write zero on the screen

Declare 3 new global unsigned long variables
startTime, currentTime and period. Give period a value of 1000 for now. The others don't need to be initialised with a value.

In the ISR put

startTime = millis();  //save the time the tacho was read

Somewhere in loop() put

currentTime = millis();
if (currentTime - startTime >= period)  //test whether the tacho value was read recently
{
  //code here to write 0 to the display
}

Change the value of period (milliseconds) to change how soon the zero should be displayed

ukhelibob,

i did that but i get an error

//for tacho---------------------------------------------------


 attachInterrupt(sensorInterrupt, sensorIsr, RISING);
 Serial.begin(9600);
 lastPulseTime = micros();
 timeoutCounter = 0;
}

void sensorIsr()
 
{
 unsigned long now = micros();
 interval = now - lastPulseTime;
 lastPulseTime = now;
 timeoutCounter = timeoutValue;
 startTime = millis();  //save the time the tacho was read
}




void loop() {

  buttonState = digitalRead(inPin); 
  lcd.setCursor(0,0);


 Serial.print(" RPM ");
 if (timeoutCounter != 0)
 
   --timeoutCounter;
   float rpm = 60e6/(float)interval;
   Serial.print(rpm, 1);


   
currentTime = millis();

if (currentTime - startTime >= 1500)  //test whether the tacho value was read recently
{
  lcd.print("0");
}


 
 Serial.println();
 delay(500);

error

DigitalReadSerial:71: error: 'currentTime' was not declared in this scope

currentTime = millis();

^

exit status 1
'startTime' was not declared in this scope

Read post # 13

Did you

Declare 3 new global unsigned long variables
startTime, currentTime and period.

UKHlebob

as i wrote them below??? it does not work like that :frowning:

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12,11,7,6,5,4);

//FOR CHANGING THE MENU-------------------------------------------
int inPin = 8;  
int buttonState = 0;
int lastButtonState = 0;
int page = 0;

//FOR THE DIFFERENCE OUTPUT-------------------------------------
int inPinOut = 9;

//for tacho ---------------------------------------------
const float sensorInterrupt = 1;
const int timeoutValue = 5;
volatile unsigned long lastPulseTime;
volatile unsigned long interval = 0;
volatile int timeoutCounter;
unsigned long startTime;
unsigned long currentTime;
unsigned long period = (1000);



void setup()
{{
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  Serial.begin(9600);
  // declare pushbutton as input
  pinMode (inPinOut, OUTPUT);
}

//for tacho---------------------------------------------------
 attachInterrupt(sensorInterrupt, sensorIsr, RISING);
 Serial.begin(9600);
 lastPulseTime = micros();
 timeoutCounter = 0;
}

void sensorIsr()
 
{
 unsigned long now = micros();
 interval = now - lastPulseTime;
 lastPulseTime = now;
 timeoutCounter = timeoutValue;
 startTime = micros();  //save the time the tacho was read
}




void loop() {

  buttonState = digitalRead(inPin); 
  lcd.setCursor(0,0);


 Serial.print(" RPM ");
 if (timeoutCounter != 0)
 
   --timeoutCounter;
   float rpm = 60e6/(float)interval;
   Serial.print(rpm, 1);

currentTime = millis();
if (currentTime - startTime >= period)  //test whether the tacho value was read recently
{
  int rpm = 0;
}

If you Auto Format the code in the IDE you will (may) spot that there is no closing brace on the loop() function.

Also, remove the brackets around the 1000 in the declaration of period. I only used them in my post to clue you into the fact that the value for period was in milliseconds.

i did that but still did not works

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);

//FOR CHANGING THE MENU-------------------------------------------
int inPin = 8;
int buttonState = 0;
int lastButtonState = 0;
int page = 0;

//FOR THE DIFFERENCE OUTPUT-------------------------------------
int inPinOut = 9;

//for tacho ---------------------------------------------
const float sensorInterrupt = 1;
const int timeoutValue = 5;
volatile unsigned long lastPulseTime;
volatile unsigned long interval = 0;
volatile int timeoutCounter;
unsigned long startTime;
unsigned long currentTime;
unsigned long period = 1000;



void setup()
{{
    // set up the LCD's number of columns and rows:
    lcd.begin(16, 2);
    Serial.begin(9600);
    // declare pushbutton as input
    pinMode (inPinOut, OUTPUT);
  }

  //for tacho---------------------------------------------------
  attachInterrupt(sensorInterrupt, sensorIsr, RISING);
  Serial.begin(9600);
  lastPulseTime = micros();
  timeoutCounter = 0;
}

void sensorIsr()

{
  unsigned long now = micros();
  interval = now - lastPulseTime;
  lastPulseTime = now;
  timeoutCounter = timeoutValue;
  startTime = micros();  //save the time the tacho was read
}




void loop() {

  buttonState = digitalRead(inPin);
  lcd.setCursor(0, 0);


  Serial.print(" RPM ");
  if (timeoutCounter != 0)

    --timeoutCounter;
  float rpm = 60e6 / (float)interval;
  Serial.print(rpm, 1);

  currentTime = millis();
  if (currentTime - startTime >= period)  //test whether the tacho value was read recently
  {
    float rpm = 0;
  }

  Serial.println();
  delay(500);

  //PRESSURE SENSORS----------------------------------------------------------
  float sensorVoltage = analogRead(0);   // read the sensor voltage from analog pin 0 pump pressure
  float Ppsi = ((sensorVoltage - 105) / 204) * 12.5;

  float sensorVoltage2 = analogRead(1);   // read the sensor voltage from analog pin 1 pressure from the filter
  float Fpsi = ((sensorVoltage2 - 115) / 204) * 12.5;


  float DpsiBar = (Ppsi - Fpsi);
  int Dpsi = ((DpsiBar * 100) / Ppsi);

  float sensorVoltage3 = analogRead(2);   // read the sensor voltage from analog pin 2 for Max Pressure Difference
  float PDL = ((sensorVoltage3) / 1023) * 10;


  //Pressure comparator

  if (DpsiBar >= PDL)
  {
    digitalWrite(inPinOut, HIGH);
  }
  else
  {
    digitalWrite(inPinOut, LOW);
  }


  //------------------------------------------------Fuel Level

  float sensorVoltage4 = analogRead(3);   // read the sensor voltage from analog pin 2 for Max Pressure Difference
  float FLEV = ((sensorVoltage4) / 1023) * 10;


  switch (page) {


    case 0:
      lcd.print ("Speed");
      lcd.setCursor (0, 1);
      lcd.print (rpm, 1);
      lcd.print (" RPM");
      delay (1000);

      break;

    case 1:
      lcd.print ("Pump Pressure");
      lcd.setCursor (0, 1);
      lcd.print (Ppsi, 1);
      lcd.print (" BAR");
      Serial.println (Ppsi);
      delay (500);
      break;





    case 2:
      lcd.print ("Filter Pressure");
      lcd.setCursor (0, 1);
      lcd.print (Fpsi, 1);
      lcd.print (" BAR");
      delay (500);
      break;




    case 3:
      lcd.print ("Filter Pres Diff");
      lcd.setCursor (0, 1);
      lcd.print (Dpsi);
      lcd.print (" % ");
      lcd.print (DpsiBar);
      lcd.print (" BAR");
      delay (500);
      break;


    case 4:
      lcd.print ("Pressure Dif Limit");
      lcd.setCursor (0, 1);
      lcd.print (PDL, 1);
      lcd.print (" BAR");
      delay (500);
      break;




    case 5:
      lcd.print ("Fuel Level");
      lcd.setCursor (0, 1);
      lcd.print (FLEV, 1);
      lcd.print (" BAR");
      delay (100);
      break;


  }
  if (buttonState != lastButtonState)     // compare the buttonState to its previous state
  {
    delay(500);               // if the state has changed, increment the counter
    if (buttonState == HIGH)
    {
      lcd.clear();
      page++;     // if the current state is HIGH then the button changed
      if (page == 6)
      {
        page = 0;
      }
    }

  }
}

Does it compile ? What does it do when it runs ?

const float sensorInterrupt = 1;
  attachInterrupt(sensorInterrupt, sensorIsr, RISING);

Which pin is the tacho attached to ?

void setup()
{{

Why double braces ?

  lastPulseTime = micros();Did you mean to use millis() ?

    // declare pushbutton as input
    pinMode (inPinOut, OUTPUT);

input or output ?