I'm using a IR sensor with a LCD screen on an Arduino Uno when I test the code the serial was working without any issues but once I changed <LiquidCrystal.h> to <LiquidCrystal_I2C.h> I'm not getting any response from the serial monitor.
#include <LiquidCrystal_I2C.h>
#include <TimerOne.h>
#include <Wire.h>
//#include <LiquidCrystal.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display
//const int rs = 8, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
//LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int IRSensorPin = 2;
const int ledPin = 13;
int inputState;
int lastInputState = LOW;
long lastDebounceTime = 0;
long debounceDelay = 5;
long time;
long endTime;
long startTime;
int RPM = 0;
double trip = 0;
double kkbanspd = 0.00223;
float lnTime = 0;
void setup(void) {
pinMode(IRSensorPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.begin(16, 2);
lcd.clear();
lcd.print("Eduardo");
lcd.setCursor(0, 1);
lcd.print("Alvarez");
delay(3000);
lcd.clear();
lcd.print("Test");
lcd.setCursor(0, 1);
lcd.print("Bike Speedometer");
delay(2000);
lcd.clear();
endTime = 0;
Timer1.initialize(1000000); // Set the timer to 60 rpm, 1,000,000 microseconds (1 second)
Timer1.attachInterrupt(timerIsr); // Attach the service routine here
}
// ---------------------------------------------------------------
void loop(void) {
time = millis();
int currentSwitchState = digitalRead(IRSensorPin);
if (currentSwitchState != lastInputState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (currentSwitchState != inputState) {
inputState = currentSwitchState;
if (inputState == LOW) {
digitalWrite(ledPin, LOW);
calculateRPM(); // Real RPM from sensor
}
else {
digitalWrite(ledPin, HIGH);
}
}
}
lastInputState = currentSwitchState;
}
// ---------------------------------------------------------------
void calculateRPM() {
startTime = lastDebounceTime;
lnTime = startTime - endTime;
RPM = 60000 / (startTime - endTime);
endTime = startTime;
trip++;
}
void timerIsr()
{
// Print RPM every second
// RPM based on timer
Serial.println("---------------");
time = millis() / 1000;
Serial.print(time);
Serial.print(" RPM: ");
Serial.println(RPM);
Serial.print(trip);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Spd:");
lcd.setCursor(5, 0);
lcd.print((RPM * kkbanspd) * 60);
lcd.setCursor(12, 0);
lcd.print("Km/h");
lcd.setCursor(0, 1);
lcd.print("Trp:");
lcd.setCursor(5, 1);
lcd.print(trip * kkbanspd);
lcd.setCursor(12, 1);
lcd.print("Km");
delay(500);
RPM = 0;
}