#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
// Setup for LCD display (I2C)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Setup for Servo
Servo speedometerServo;
int servoPin = 9;
const int potentiometerPin = A0; // Potentiometer connected to analog pin A0
int potValue = 0; // Variable to store potentiometer value
int speedValue = 0; // Mapped speed value for the display or servo
void setup() {
// Start LCD
lcd.begin(16, 2);
lcd.print("Speed: ");
// Setup Servo
speedometerServo.attach(servoPin);
// Start Serial for debugging (optional)
Serial.begin(9600);
}
void loop() {
// Read the potentiometer value (0 to 1023)
potValue = analogRead(potentiometerPin);
// Map the potentiometer value to a speed range (0-100 for display)
speedValue = map(potValue, 0, 1023, 0, 100); // You can adjust this range
// Display speed value on the LCD
lcd.setCursor(0, 1);
lcd.print("Speed: ");
lcd.print(speedValue);
lcd.print(" km/h"); // Adjust to your units
// Alternatively, you can use the potentiometer value to control a Servo
// Map the potentiometer to servo angle (0 to 180 degrees)
int servoAngle = map(potValue, 0, 1023, 0, 180);
speedometerServo.write(servoAngle); // Set servo position
// Delay for a bit to make the speed changes visible
delay(100);
}