How do i convert a LCD display code into a LCD i2c display code?

I am trying to make a chess clock with 2 buttons using a LCD i2c display. I got the code for this using the internet and i am trying to convert this to a i2c compatable code.

Any reply would be really helpful .

Here is my code

#include <LiquidCrystal.h>
LiquidCrystal lcd (7, 8, 9, 10, 11, 12);
const int buttonPin = 2;    // the number of the pushbutton pin
const int ledPin = 13;      // the number of the LED pin
// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

  double whiteTime = 600000;
  int whiteMinutes = 0;
  int whiteSeconds = 0;
  double whiteLastCheck = millis();
  double blackTime = 600000;
  int blackMinutes = 0;
  int blackSeconds = 0;
  double blackLastCheck = millis();
  bool isWhiteTurn = true;
  bool gameOver = false;
void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  UpdateWhiteTime();
  UpdateBlackTime();

  //int whiteButtonState = 0;
  //int blackButtonState = 0;
}
void loop() {
  int reading = digitalRead(buttonPin);
    Serial.println(ledState);

  if (reading != lastButtonState) {
   
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }// set the LED:
  digitalWrite(ledPin, ledState);
  if(ledState==1){
    StartWhiteTurn (); 
        Serial.println("White");
    UpdateWhiteTime();      
  } else if(ledState==0){
    StartBlackTurn ();
        Serial.println("Black");
    UpdateBlackTime();
  }
lastButtonState = reading;
}
void UpdateWhiteTime() {
  lcd.setCursor(10,0);
  lcd.print("White");
  lcd.setCursor(12, 1);
      Serial.println("White");
  whiteTime -= ((millis() - whiteLastCheck));
  whiteLastCheck = millis();
  if (whiteTime <= 0) {
    gameOver = true;
    lcd.print("LOSE");
    return;
  }
  whiteMinutes = floor(whiteTime / 60000);
  whiteSeconds = floor(whiteTime / 1000) - whiteMinutes * 60;
  lcd.print(whiteMinutes);
  lcd.print(":");
  if (whiteSeconds < 10) {
    lcd.print(0);
  }
  lcd.print(whiteSeconds);
}// end printWait  
void UpdateBlackTime() {
  lcd.setCursor(1,0);
  lcd.print("Black");
  lcd.setCursor(0, 1);
      Serial.println("Black");
  blackTime -= ((millis() - blackLastCheck));
  blackLastCheck = millis();
  if (blackTime <= 0) {
    gameOver = true;
    lcd.print("LOSE");
    return;
  }
  blackMinutes = floor(blackTime / 60000);
  blackSeconds = floor(blackTime / 1000) - blackMinutes * 60;
  lcd.print(blackMinutes);
  lcd.print(":");
  if (blackSeconds < 10) {
    lcd.print(0);
  }
  lcd.print(blackSeconds);
}// end printWait
void StartWhiteTurn () {
  if (isWhiteTurn) {
    return;
  }
  isWhiteTurn = true;
  whiteLastCheck = millis();
} // end StartWhiteTurn
void StartBlackTurn () {
  if (!isWhiteTurn) {
    return;
  }
  isWhiteTurn = false;
  blackLastCheck = millis();
} // end StartBlackTurn

The changes needed depend on which library you are using. Start by looking at the I2C LCD library examples

I would recommend that you use the currently best available library for the i2c lcd modules which is Bill Perry's hd44780.h. It is available through the library manager.

It will auto configure the i2c address and the pin configuration. There is a comprehensive diagnostic sketch as part of the library if there are any issues. Reference material is here.

https://github.com/duinoWitchery/hd44780

Using that library, these changes will make the sketch should work with your i2c display


//#include <LiquidCrystal.h>
//LiquidCrystal lcd (7, 8, 9, 10, 11, 12);
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h> // include i/o class header for i2c expanders

hd44780_I2Cexp lcd; // declare lcd object: auto locate & config display for hd44780 chip

ok i will check

Thanks

I often use this to switch between the two.


//********************************************^************************************************
//                       S e r i a l   O R   P a r a l l e l   L C D   ?
//********************************************^************************************************
 
//uncomment the next line if you have a serial LCD                                <-------<<<<<
#define serialLCDisBeingUsed
 
//****************************************
#ifdef  serialLCDisBeingUsed
 
#include <Wire.h>
 
//Use I2C library:     https://github.com/duinoWitchery/hd44780
//LCD Reference:       https://www.arduino.cc/en/Reference/LiquidCrystal
 
#include <hd44780.h>   //main hd44780 header
 
//NOTE:
//hd44780_I2Cexp control LCD using I2C I/O expander backpack (PCF8574 or MCP23008)
//hd44780_I2Clcd control LCD with native I2C interface (PCF2116, PCF2119x, etc...)
 
#include <hd44780ioClass/hd44780_I2Cexp.h> //I2C expander i/o class header
 
//If you do not know what your I2C address is, first run the "I2C_Scanner" sketch
//OR
//run the "I2CexpDiag" sketch that comes with the hd44780 library
//hd44780_I2Cexp lcd(0x3F);
 
hd44780_I2Cexp lcd(0x27);
 
//****************************************
#else
 
#include <LiquidCrystal.h>
 
// LCD pin         4   6  11  12  13  14
//                RS  EN  D4  D5  D6  D7
LiquidCrystal lcd( 4,  5,  6,  7,  8,  9);
 
#endif
 

1 Like

Thanks

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.