Hello everyone.
I am more of a hardware dude, so software always makes me stay up all nights while trying to understand why it doesn't work and why DOES it work.
Anyway, below is the code I am using. It works, kind of, but there is a problem.
If I decrease delay, resolution goes down pretty bad, with 300ms it is 100RPM. If I Increase, of course it gets slow, and that is not ok in my application. As I am a total noob, I hope someone here might give me advice, what can I possibly do.
Regards, Arch.
volatile unsigned int rpmcount;
float rpm;
float timeold;
const int carbswitch = P2_2; //switch from carburator, when accelerator is not pushed, switch is connected to GND. When accelerator is getting floored, disconnects.
const int outputswitch = P2_4; //enables/disables gas valve, HIGH at >1400 RPM and carbswitch HIGH.
const int shiftlight = P2_5; //enabled when RPM > 5000.
const int engineok = P2_3; //enabled when RPM > 500.
int carbswitchstate = LOW;
int valvestate = LOW;
int mymilis = 300;
String ds;
#include <LiquidCrystal.h>
LiquidCrystal lcd(P1_2, P1_1, P1_4, P1_5, P1_6, P1_7);
void rpm_fun()
{
rpmcount++;
}
//INPUT from ignition coil's switching GND is wired to P1_3, with protection from high voltage spikes.
void setup()
{
lcd.begin(16, 2);
attachInterrupt(P1_3, rpm_fun, FALLING);
rpmcount = 0.0;
rpm = 0.0;
timeold = 0.0;
pinMode (outputswitch, OUTPUT);
pinMode (shiftlight, OUTPUT);
pinMode (engineok, OUTPUT);
pinMode(carbswitch, INPUT_PULLUP);
}
void loop()
{
if(millis()-timeold>=mymilis){
detachInterrupt(P1_3);
const float freqHz = ((float) rpmcount / (float) (millis() - timeold)) * 1000.0;
const float rpm = freqHz * 30.0;
rpmcount = 0;
carbswitchstate = digitalRead(carbswitch);
if (rpm >= 1400.0 && carbswitchstate == HIGH) {
digitalWrite(outputswitch, HIGH);
}
else {
digitalWrite(outputswitch, LOW);
}
valvestate = digitalRead(outputswitch);
if (valvestate == HIGH) {
ds = ("ON");
}
else {
ds = ("OFF");
}
if (rpm >= 5000.0) {
digitalWrite(shiftlight, HIGH);
}
else {
digitalWrite(shiftlight, LOW);
}
if (rpm >= 500.0) {
digitalWrite(engineok, HIGH);
}
else {
digitalWrite(engineok, LOW);
}
lcd.clear();
lcd.print("RPM=");
lcd.print(rpm);
lcd.setCursor(0, 1);
lcd.print ("ECON=");
lcd.print(ds);
attachInterrupt(P1_3, rpm_fun, FALLING);
timeold = millis();
}
}