// Wire Bibliothek einbinden
#include <Wire.h>
// für Timings und Interrupts
#include <TimerOne.h>
// Vorher hinzugefügte LiquidCrystal_I2C Bibliothek einbinden
#include <LiquidCrystal_I2C.h>
//Hier wird festgelegt um was für einen Display es sich handelt. In diesem Fall eines mit 16 Zeichen in 2 Zeilen und der HEX-Adresse 0x27.
//Für ein vierzeiliges I2C-LCD verwendet man den Code "LiquidCrystal_I2C lcd(0x27, 20, 4)"
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define Reed contact pin
const int reedPin = 2;
// Define LED pins
const int redLED = 8;
const int yellowLED = 9;
const int greenLED = 10;
// Define potentiometer pins
const int lowerThresholdPot = A0;
const int upperThresholdPot = A1;
// Define thresholds
int lowerThreshold = 0;
int upperThreshold = 0;
// Define current RPM
int currentRPM = 0;
// Previous threshold values for LCD update
int prevLowerThreshold = 0;
int prevUpperThreshold = 0;
unsigned long lastTime = 0;
void setup() {
Timer1.initialize();
// Initialize serial communication
Serial.begin(9600);
pinMode(reedPin, INPUT);
pinMode(redLED, OUTPUT); // set pin #8 as OUTPUT pin
pinMode(yellowLED, OUTPUT); // set pin #9 as OUTPUT pin
pinMode(greenLED, OUTPUT); // set pin #10 as OUTPUT pin
// Initialize LCD
lcd.init();
lcd.backlight(); // Optionally, turn on backlight
checkThresholds();
// Print initial thresholds to LCD
updateLcdThreshold(lowerThreshold, upperThreshold);
printSerial("lower", lowerThreshold);
printSerial("upper", upperThreshold);
}
void loop() {
// Read Reed contact signal
int reedSignal = digitalRead(reedPin);
// Increment RPM counter if Reed contact is closed
if (reedSignal == HIGH) {
currentRPM++;
}
checkThresholds();
// Update RPM counter every second
unsigned long currentTime = millis();
if (currentTime - lastTime >= 1000) {
currentRPM = 0;
lastTime = currentTime;
}
// Update LCD display with thresholds
if (lowerThreshold != prevLowerThreshold || upperThreshold != prevUpperThreshold) {
// Print actual thresholds to LCD
updateLcdThreshold(lowerThreshold, upperThreshold);
prevLowerThreshold = lowerThreshold;
prevUpperThreshold = upperThreshold;
}
// Update LCD display with current RPM
updateLcdRpm(currentRPM);
// Check thresholds and update LEDs
if (currentRPM < lowerThreshold) {
redLedOn();
} else if (currentRPM < upperThreshold) {
yellowLedOn();
} else {
greenLedOn();
}
// Delay
delay(50);
}
void updateLcdThreshold( int lowerThreshold, int upperThreshold){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("U:");
lcd.print(lowerThreshold);
lcd.setCursor(8, 0);
lcd.print(" O:");
lcd.print(upperThreshold);
}
void updateLcdRpm( int currentRPM){
lcd.setCursor(0, 1);
lcd.print("RPM:");
lcd.print(currentRPM);
}
void printSerial( String valueName, int value){
Serial.print(valueName + " :");
Serial.println(value);
}
void checkThresholds(){
// Map potentiometer values to thresholds
lowerThreshold = map(analogRead(lowerThresholdPot), 0, 1023, 0, 6000);
upperThreshold = map(analogRead(upperThresholdPot), 0, 1023, 0, 6000);
}
void redLedOn(){
digitalWrite(redLED, HIGH);
digitalWrite(yellowLED, LOW);
digitalWrite(greenLED, LOW);
}
void yellowLedOn(){
digitalWrite(redLED, LOW);
digitalWrite(yellowLED, HIGH);
digitalWrite(greenLED, LOW);
}
void greenLedOn(){
digitalWrite(redLED, LOW);
digitalWrite(yellowLED, LOW);
digitalWrite(greenLED, HIGH);
}