Hi everyone
I' am currently making a project involving a Hallstate sensor to messure the RPM of a 4-stroke engine.
I think I am almost ther exept for one part. when I open the serial monitor I the RPM is quite accurate but how longer the program is running the worse the accuracy of the program. I think I have to use an Intterupt function but I dont know how and where. Can anyone help ith this?
Thanks a Lot.
unsigned long time;
const int buttonPin = 2;
const int ledPin = 13;
float RPM=0;
float SEC;
float MIN;
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
buttonState = digitalRead(buttonPin); // read the pushbutton input pin:
if (buttonState != lastButtonState) // compare the buttonState to its previous state
{if (buttonState == HIGH) // if the state has changed, increment the counter
{buttonPushCounter++; }} // if the current state is HIGH then the button went from off to on:
//Serial.println(buttonPushCounter);}}
lastButtonState = buttonState; // save the current state as the last state, for next time through the loop
time = millis();
SEC = (time/1000);
MIN = (SEC/60);
RPM = (buttonPushCounter/MIN);
Serial.println(RPM);
delay(25);
}