#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display
// Define the analog input pins for the four-point measurement
const int IR_SENSOR_PIN = PB3;
// Define the wheel circumference (in meters)
const float WHEEL_CIRCUMFERENCE = 2.07; // adjust as needed
// Define variables for speed and distance calculation
unsigned long lastTime = 0;
unsigned long currentTime = 0;
float lastDistance = 0;
float currentDistance = 0;
float Kecepatan = 0;
float JarakTmph = 0;
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("SELAMAT_BERSPEDA");
delay(5000);
lcd.setCursor(0, 1);
lcd.print("TETAP_BRSEMANGAT");
delay(5000);
lcd.clear();
}
void loop() {
// Read the analog values at each measurement point
int irSensor = analogRead(IR_SENSOR_PIN);
// Convert the analog readings to actual values
float irSensorValue = map(irSensor, 0, 1023, 0, 100);
// Calculate the speed and distance
currentTime = millis();
float elapsedTime = (currentTime - lastTime) / 1000.0; // Convert to seconds
lastTime = currentTime;
lastDistance = currentDistance;
currentDistance = (irSensorValue / 100.0) * WHEEL_CIRCUMFERENCE; // Distance traveled since last measurement
Kecepatan = (currentDistance - lastDistance) / elapsedTime * 3.6; // Convert to km/h
JarakTmph += currentDistance / 1000.0; // Increment the trip distance (convert to km)
lcd.setCursor(0, 0);
lcd.print("Kecepatan");
lcd.setCursor(11, 0); // Set cursor to bottom left of LCD
lcd.print(Kecepatan, 0); // Display speed with one decimal point
lcd.print("Kmh");
lcd.setCursor(0, 1);
lcd.print("JarakTemph");
lcd.setCursor(11, 1); // Set cursor to bottom right of LCD
lcd.print(JarakTmph, 1); // Display trip distance with two decimal points
lcd.print("Km");
// Wait for a short time before taking the next measurement
delay(100);
}