#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int potentiometerPin = A0;
const int displayAddress = 0x27;
const int displayCols = 16;
const int buzzerPin = 7;
LiquidCrystal_I2C lcd(displayAddress, displayCols, 2);
void setup() {
lcd.begin(displayCols, 2);
lcd.clear();
}
void loop() {
int sensorValue = analogRead(potentiometerPin);
int rpm = map(sensorValue, 0, 1023, 0, 6000);
displayRPM(rpm);
if (rpm > 0) {
int baseFrequency = map(rpm, 0, 6000, 50, 150);
// Create a pattern for the engine sound
for (int i = 0; i < 5; i++) {
int frequency = baseFrequency + i * 20;
int duration = map(rpm, 0, 6000, 60, 15);
tone(buzzerPin, frequency, duration);
delay(duration);
}
} else {
noTone(buzzerPin);
}
delay(100);
}
void displayRPM(int rpm) {
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("RPM: ");
lcd.print(rpm);
}