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
+21 -3
+21 -3
View File
@@ -2,15 +2,33 @@
// Copyright (C) 2026 Association Exergie <association.exergie@gmail.com>
// SPDX-License-Identifier: GPL-3.0-or-later
#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() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(interruptPin, INPUT);
attachInterrupt(digitalPinToInterrupt(interruptPin),interrupt,FALLING);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("test");
int value = analogRead(A1);
Serial.println(value);
if (new_rpm){
unsigned long deltat = timestamp1 - timestamp2;
float rpmhz = 500000.0 / deltat;
new_rpm = false;
Serial.println(rpmhz);
}
}