#include <LiquidCrystal.h>
// Pinbelegung für den Taster und das LCD
const int buttonPin = 2; // Pin für den Taster
const int lcdPin_RS = 12;
const int lcdPin_EN = 11;
const int lcdPin_D4 = 5;
const int lcdPin_D5 = 4;
const int lcdPin_D6 = 3;
const int lcdPin_D7 = 2;
volatile unsigned long pulseCount = 0; // Anzahl der Impulse
unsigned long lastTime = 0; // Zeitpunkt der letzten Messung
unsigned long lastPulseCount = 0; // Anzahl der Impulse beim letzten Messzeitpunkt
LiquidCrystal lcd(lcdPin_RS, lcdPin_EN, lcdPin_D4, lcdPin_D5, lcdPin_D6, lcdPin_D7);
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Taster-Pin als Eingang mit Pull-Up-Widerstand konfigurieren
attachInterrupt(digitalPinToInterrupt(buttonPin), countPulse, FALLING); // Interrupt auf fallende Flanke des Tasters konfigurieren
lcd.begin(16, 2); // Initialisierung des LCD-Displays
}
void loop() {
unsigned long currentTime = millis();
// Messung alle zwei Sekunden
if (currentTime - lastTime >= 2000) {
// Drehzahl berechnen
unsigned long elapsedTime = currentTime - lastTime; // Zeit seit der letzten Messung in Millisekunden
unsigned long pulseDiff = pulseCount - lastPulseCount; // Differenz der Impulse seit der letzten Messung
float rpm = (pulseDiff / (float)elapsedTime) * 60000.0; // Drehzahl in U/min berechnen
// Drehzahl auf dem LCD anzeigen
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("RPM: ");
lcd.print(rpm);
// Aktualisierung der Variablen für die nächste Messung
lastTime = currentTime;
lastPulseCount = pulseCount;
}
}
void countPulse() {
pulseCount++; // Impuls zählen
}