#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int sensorPin = A0;
const int ledPin = 13;
const int greenLedPin = 9;
const int redLedPin = 10;
const int rpmBarLength = 12;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
lcd.begin(16, 2);
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(sensorPin);
int rpm = sensorValue * 60 / 5;
// Limit RPM to 8500
if (rpm > 8500) {
rpm = 8500;
}
// Display RPM
Serial.print("RPM: ");
Serial.println(rpm);
// Display RPM Bar
int rpmBar = map(rpm, 0, 8000, 0, rpmBarLength);
Serial.print("RPM Bar: ");
for (int i = 0; i < rpmBar; i++) {
Serial.print("#");
lcd.setCursor(i, 0);
lcd.write(255);
}
for (int i = rpmBar; i < rpmBarLength; i++) {
lcd.setCursor(i, 0);
lcd.write(' ');
}
Serial.println();
// Display Economy/Power Mode and control LEDs
if (rpm < 4000) {
digitalWrite(greenLedPin, HIGH);
digitalWrite(redLedPin, LOW);
Serial.println("Economy Mode");
lcd.setCursor(0, 1);
lcd.print("RPM:");
lcd.print(rpm);
lcd.print(" ECO ");
} else {
digitalWrite(greenLedPin, LOW);
digitalWrite(redLedPin, HIGH);
Serial.println("Power Mode");
lcd.setCursor(0, 1);
lcd.print("RPM:");
lcd.print(rpm);
lcd.print(" POWER");
}
// Blink LED
digitalWrite(ledPin, HIGH);
delay(50);
digitalWrite(ledPin, LOW);
delay(50);
}