RPM Calculation

first version of the program which returns the value of the RPM of the
engine in Hertz(Hz) using an interrupt function
This commit is contained in:
Martin Girard committed 2026-09-15 10:19:47 +02:00
1 parent 5cc1052f95
commit a21bf160bc
1 file changed
+22 -4
+22 -4
View File
@@ -2,15 +2,33 @@
// Copyright (C) 2026 Association Exergie <association.exergie@gmail.com> // Copyright (C) 2026 Association Exergie <association.exergie@gmail.com>
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
#include <Arduino.h> #include <Arduino.h>
const byte interruptPin = 2;
volatile unsigned long timestamp1 = 0;
volatile unsigned long timestamp2 = 0;
volatile bool new_rpm = false;
void interrupt(){
unsigned long now = micros();
timestamp2 = timestamp1;
timestamp1 = now;
new_rpm = true;
}
void setup() { void setup() {
// put your setup code here, to run once: // put your setup code here, to run once:
Serial.begin(115200); Serial.begin(115200);
pinMode(interruptPin, INPUT);
attachInterrupt(digitalPinToInterrupt(interruptPin),interrupt,FALLING);
} }
void loop() { void loop() {
// put your main code here, to run repeatedly: // put your main code here, to run repeatedly:
Serial.println("test"); if (new_rpm){
int value = analogRead(A1); unsigned long deltat = timestamp1 - timestamp2;
Serial.println(value); float rpmhz = 500000.0 / deltat;
} new_rpm = false;
Serial.println(rpmhz);
}
}