In this code how to do that when I stop the engine the rpm reading will return to 0..
Otherwise when with this if I stop the engine the last shown rpm stay on the screen..
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 9, 8);
const int analoginput = 0;
float vout = 0.0;
int value = 0;
float R1 = 1000.0; // !! resistance of R1 !!
float R2 = 470.0; // !! resistance of R2 !!
float vin = 0.0;
const int ignitionPin = 2;
const int ignitionInterrupt = 0;
const unsigned int pulsesPerRev = 1;
long VpreviousMillis = 0;
long Vinterval = 1400;
long RPMpreviousMillis = 0;
long RPMinterval = 200;
unsigned long lastPulseTime = 0;
unsigned long rpm = 0;
void ignitionIsr()
{
unsigned long now = micros();
unsigned long interval = now - lastPulseTime;
if (interval > 2000)
{
rpm = 60000000UL/(interval * pulsesPerRev);
lastPulseTime = now;
}
}
void setup()
{
lcd.begin(16,2);
lcd.setCursor(3, 0);
lcd.print("Royal");
lcd.setCursor(7, 1);
lcd.print("Enfield");
delay(3000);
lcd.clear();
pinMode(analoginput, INPUT);
pinMode(ignitionPin, INPUT);
attachInterrupt(ignitionInterrupt, &ignitionIsr, FALLING);
}
void loop()
{
/***************THIS PART IS FOR VOLTAGE*********************/
unsigned long VcurrentMillis = millis();
if(VcurrentMillis - VpreviousMillis > Vinterval) {
VpreviousMillis = VcurrentMillis;
// read the value on analog input
value = analogRead(analoginput);
vout= (value * 5.0)/1024.0; //voltage coming out of the voltage divider
vin = vout / (R2/(R1+R2)); //voltage to display
lcd.setCursor(0,0);
lcd.print(vin, 1); //Print float "vin" with 1 decimal
lcd.print("V");
}
/****************THIS PART IS FOR RPM*************************/
unsigned long RPMcurrentMillis = millis();
if(RPMcurrentMillis - RPMpreviousMillis > RPMinterval) {
RPMpreviousMillis = RPMcurrentMillis;
noInterrupts();
unsigned long rpmToDisplay = rpm; // take a copy with interrupts disabled in case it changes
interrupts();
if (rpmToDisplay >= 1000)
{
lcd.setCursor(7, 0);
lcd.print("RPM");
lcd.print(rpmToDisplay/1000);
lcd.print(',');
lcd.print((char)(((rpmToDisplay/100) % 10) + '0'));
lcd.print((char)(((rpmToDisplay/10) % 10) + '0'));
lcd.print((char)((rpmToDisplay %10) + '0'));
}
else
{
lcd.setCursor(7, 0);
lcd.print("RPM");
lcd.print(rpmToDisplay);
lcd.print(" ");
}
}
}