#include <LiquidCrystal.h>

// Define the pins for the IR sensor and LCD display
const int irPin = 1;
const int rsPin = 12;
const int enPin = 11;
const int d4Pin = 5;
const int d5Pin = 4;
const int d6Pin = 3;
const int d7Pin = 2;

// Define variables for storing RPM, battery voltage, and battery percentage
volatile int rpm;
volatile int kmph;
float voltage;
float wheelDiameter;
int percentage;

// Define objects for the LCD display and timer
LiquidCrystal lcd(rsPin, enPin, d4Pin, d5Pin, d6Pin, d7Pin);
unsigned long previousMillis = 0;
const long interval = 1000;

void setup() {
  // Set up the IR sensor pin as an input
  pinMode(irPin, INPUT);

  // Set up the LCD display
  lcd.begin(16, 2);

  // Initialize the variables
  rpm = 0;
  kmph = 0;
  wheelDiameter = 0.568;     // Diameter of Wheel in Meter [cm/100 = m] (0.568m accouding to CD 70 tyre)
  voltage = 0.0;
  percentage = 0;

  // Set up the timer for updating the LCD display
  previousMillis = millis();
}

void loop() {
  // Measure the RPM using the IR sensor
  int sensorValue = digitalRead(irPin);
  if (sensorValue == HIGH) {
    rpm++;
    
    kmph = 800 * wheelDiameter  * 0.1885;   // constant 0.1885 includes the pi and conversion into kmph
  }

  // Calculate the battery voltage and percentage
  voltage = (analogRead(A0) * 5.0) / 1024.0 * 12.0;
  percentage = (int)((voltage - 30.0) * 100.0 / 12.0);

  // Update the LCD display once per second
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

    // Display the RPM, battery voltage, and battery percentage on the LCD
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Speed = ");
    lcd.print(kmph);
    lcd.print(" km/h");
    lcd.setCursor(0, 1);
    lcd.print("V= ");
    lcd.print(voltage);
    lcd.print("  ");
    lcd.print(percentage);
    lcd.print("%");
    
    // Reset the RPM counter
    rpm = 0;
  }
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
lcd1:VSS
lcd1:VDD
lcd1:V0
lcd1:RS
lcd1:RW
lcd1:E
lcd1:D0
lcd1:D1
lcd1:D2
lcd1:D3
lcd1:D4
lcd1:D5
lcd1:D6
lcd1:D7
lcd1:A
lcd1:K
ir1:GND
ir1:VCC
ir1:DAT