Liquid Crystal Adaptation 16x2

Hello, I have a big request, can someone with experience help me with the code below? I would like to use a 16x2 liquid crystal display using ic2 communication, having fewer wires than the 9 used by the display in the current code.

#include <Wire.h>                   // for I2C communication
#include <LiquidCrystal.h>      // for LCD
#include <RTClib.h>
#include <avr/wdt.h>// for RTC
// create LCD with I2C address 0x27, 16 characters per line, 2 lines
RTC_DS3231 rtc;

#define SETpin 4
#define Up 3
#define Down 2
#define FW A0
#define BW A1
#define Feed A2
#define FCFW 13
#define FCBW 12
int timz = 0, k = 0, key = 1, err = 0, retour = 0;
int hr1 [4][6] = {
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0}
};
int mn1 [4][6] = {
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0}
};
int se1 [4][6] = {
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0}
};

int Dhr1 [4][6] = {
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0}
};
int Dmn1 [4][6] = {
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0}
};
int Dse1 [4][6] = {
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0}
};

int SET = 0 , i = 0, j = 0, SET1 = 0, SET2 = 0, SET3 = 0, hr = 0, mn = 0, se = 0, item1 = 0, item2 = 0, reg = 0, ref1 , ref2 ;
const int rs = 5, en = 10, d4 = 6, d5 = 7, d6 = 8, d7 = 9;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// create rtc for the DS3231 RTC module, address is fixed at 0x68
/*
   function to update RTC time using user input
*/
void updateRTC()
{

  lcd.clear();  // clear LCD display
  lcd.setCursor(0, 0);
  lcd.print("Edit Mode...");

  // ask user to enter new date and time
  const char txt[6][15] = { "year [4-digit]", "month [1~12]", "day [1~31]",
                            "hours [0~23]", "minutes [0~59]", "seconds [0~59]"
                          };
  String str = "";
  long newDate[6];

  while (Serial.available()) {
    Serial.read();  // clear serial buffer
  }

  for (int i = 0; i < 6; i++) {

    Serial.print("Enter ");
    Serial.print(txt[i]);
    Serial.print(": ");

    while (!Serial.available()) {
      ; // wait for user input
    }

    str = Serial.readString();  // read user input
    newDate[i] = str.toInt();   // convert user input to number and save to array

    Serial.println(newDate[i]); // show user input
  }

  // update RTC
  rtc.adjust(DateTime(newDate[0], newDate[1], newDate[2], newDate[3], newDate[4], newDate[5]));
  Serial.println("RTC Updated!");
}


/*
   function to update LCD text
*/
void updateLCD()
{

  /*
     create array to convert digit days to words:

     0 = Sunday    |   4 = Thursday
     1 = Monday    |   5 = Friday
     2 = Tuesday   |   6 = Saturday
     3 = Wednesday |
  */
  const char dayInWords[7][4] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};

  /*
     create array to convert digit months to words:

     0 = [no use]  |
     1 = January   |   6 = June
     2 = February  |   7 = July
     3 = March     |   8 = August
     4 = April     |   9 = September
     5 = May       |   10 = October
     6 = June      |   11 = November
     7 = July      |   12 = December
  */
  const char monthInWords[13][4] = {" ", "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
                                    "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"
                                   };

  // get time and date from RTC and save in variables
  DateTime rtcTime = rtc.now();

  int ss = rtcTime.second();
  int mm = rtcTime.minute();
  int hh = rtcTime.hour();
  int DD = rtcTime.dayOfTheWeek();
  int dd = rtcTime.day();
  int MM = rtcTime.month();
  int yyyy = rtcTime.year();

  // move LCD cursor to upper-left position
  lcd.setCursor(0, 0);

  // print date in dd-MMM-yyyy format and day of week
  if (dd < 10) lcd.print("0");  // add preceeding '0' if number is less than 10
  lcd.print(dd);
  lcd.print("-");
  lcd.print(monthInWords[MM]);
  lcd.print("-");
  lcd.print(yyyy);

  lcd.print("  ");
  lcd.print(dayInWords[DD]);

  // move LCD cursor to lower-left position
  lcd.setCursor(0, 1);

  // print time in 12H format
  if (hh < 10) lcd.print("0");
  lcd.print(hh);
  lcd.print(':');

  if (mm < 10) lcd.print("0");
  lcd.print(mm);
  lcd.print(':');

  if (ss < 10) lcd.print("0");
  lcd.print(ss);

  if (rtcTime.isPM()) lcd.print(" PM"); // print AM/PM indication
  else lcd.print(" AM");
}



////////////////////////////////////////////////////////////////////
void setup()
{
  Serial.begin(9600); // initialize serial

  lcd.begin(16, 2); // switch-on lcd backlight

  rtc.begin();   // initialize rtc
  pinMode(SETpin, INPUT_PULLUP);
  pinMode(Up, INPUT_PULLUP);
  pinMode(Down, INPUT_PULLUP);
  pinMode(FW, OUTPUT);
  pinMode(BW, OUTPUT);

  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  pinMode(Feed, OUTPUT);
  //wdt_enable(WDTO_120MS);

  // attachInterrupt(digitalPinToInterrupt(2), UP, FALLING);
  // attachInterrupt(digitalPinToInterrupt(3), DOWN, FALLING);
}





void loop()
{
  updateLCD();  // update LCD text

  if (Serial.available()) {
    char input = Serial.read();
    if (input == 'u') updateRTC();  // update RTC time
  }

  for (int i = 0 ; i < 6; i++) {
    DateTime rtcTime = rtc.now();
    if (hr1[0][i] == rtcTime.hour() && mn1[0][i] == rtcTime.minute() && se1[0][i] == rtcTime.second()) {
      digitalWrite(A0, HIGH);
    }
    if (hr1[1][i] == rtcTime.hour() && mn1[1][i] == rtcTime.minute() && se1[1][i] == rtcTime.second()) {
      digitalWrite(A1, HIGH);
    }

    if (hr1[2][i] == rtcTime.hour() && mn1[2][i] == rtcTime.minute() && se1[2][i] == rtcTime.second()) {
      digitalWrite(A2, HIGH);
    }

    if (hr1[3][i] == rtcTime.hour() && mn1[3][i] == rtcTime.minute() && se1[3][i] == rtcTime.second()) {
      digitalWrite(A3, HIGH);
    }
    //////
    if (Dhr1[0][i] == rtcTime.hour() && Dmn1[0][i] == rtcTime.minute() && Dse1[0][i] == rtcTime.second()) {
      digitalWrite(A0, LOW);
    }
    if (Dhr1[1][i] == rtcTime.hour() && Dmn1[1][i] == rtcTime.minute() && Dse1[1][i] == rtcTime.second()) {
      digitalWrite(A1, LOW);
    }

    if (Dhr1[2][i] == rtcTime.hour() && Dmn1[2][i] == rtcTime.minute() && Dse1[2][i] == rtcTime.second()) {
      digitalWrite(A2, LOW);
    }

    if (hr1[3][i] == rtcTime.hour() && Dmn1[3][i] == rtcTime.minute() && Dse1[3][i] == rtcTime.second()) {
      digitalWrite(A3, LOW);
    }

  }

  if (digitalRead(SETpin) == 0) {
    delay(100);
    if (digitalRead(SETpin) == 0) {
      lcd.clear() ; SET = 1; item1 = 0; item2 = 0;
    }
  }

  while (SET == 1 || SET == 2) {
    while (SET == 1) {
      for (int j = 0; j < 2 ; j++) {
        if (item1 == j && SET == 1 ) {
          lcd.setCursor(4, 0);
          lcd.print("TIMER");
          lcd.print(" :");
          lcd.setCursor(4, 1);
          lcd.print("PUMP ");
          lcd.print(item1 + 1);

        }
      }

      if (digitalRead(Down) == 0) {
        delay(100);
        lcd.clear() ;
        item1 --;
      }
      if (digitalRead(Up) == 0) {
        delay(100);
        lcd.clear() ;
        item1 ++;
      }
      if (item1 == -1 && SET == 1 ) {
        lcd.clear() ;
        item1 = 3;
      }
      if (item1 == 4 && SET == 1 ) {
        lcd.clear() ;
        item1 = 0;
      }
      if (digitalRead(SETpin) == 0) {
        delay(100);
        if (digitalRead(SETpin) == 0) {
          lcd.clear() ; SET++;
          ref1 = item1;
          item1 = 0;
          delay(100);

          if (SET == 3) {
            SET = 0;
          }
        }
      }
    }

    while (SET == 2) {

      for (int i = 0; i < 2; i++) {
        if (item2 == i && i < 2 && SET == 2 ) {
          lcd.setCursor(0, 0);
          lcd.print("T");
          lcd.print(item2 + 1);
          lcd.print(" :");
          lcd.setCursor(7, 0);
          lcd.print("h");
          lcd.setCursor(11, 0);
          lcd.print("m");
          lcd.setCursor(15, 0);
          lcd.print("s");
          lcd.setCursor(0, 1);
          lcd.print("D");
          lcd.print(item2 + 1);
          lcd.print(" :");
          lcd.setCursor(7, 1);
          lcd.print("h");
          lcd.setCursor(11, 1);
          lcd.print("m");
          lcd.setCursor(15, 1);
          lcd.print("s");

        }
        if (item2 < 2 && digitalRead(SETpin) == 0)
        {
          ref2 =  item2;
          item2 = 0;
          reg++;

          delay(200);
        }
        if (reg == 1) {
          lcd.setCursor(4, 0);
          lcd.print(">");
        }
        while (reg == 1) {

          if (digitalRead(Down) == 0) {
            delay(100);
            hr --;
          }
          if (digitalRead(Up) == 0) {
            delay(100);
            hr ++;
          }
          if (hr > 24) {
            hr = 0;
          }
          if (hr == -1) {
            hr = 24;
          }
          if (hr < 10 ) {
            lcd.setCursor(5, 0);
            lcd.print("0");
            lcd.print(hr);
          }
          if (hr >= 10 ) {
            lcd.setCursor(5, 0);
            lcd.print(hr);
          }

          if (digitalRead(SETpin) == 0) {
            delay(200);
            if (digitalRead(SETpin) == 0) {
              reg++;
              hr1[ref1][ref2] = hr;
              delay(200);
            }
          }
        }
        if (reg == 2) {
          lcd.setCursor(4, 0);
          lcd.print(" ");
          lcd.setCursor(8, 0);
          lcd.print(">");
        }
        while (reg == 2) {

          if (digitalRead(Down) == 0) {
            delay(100);
            mn --;
          }
          if (digitalRead(Up) == 0) {
            delay(100);
            mn ++;
          }
          if (mn > 59) {
            mn = 0;
          }
          if (mn == -1) {
            mn = 59;
          }
          if (mn < 10 ) {
            lcd.setCursor(9, 0);
            lcd.print("0");
            lcd.print(mn);
          }
          if (mn >= 10 ) {
            lcd.setCursor(9, 0);
            lcd.print(mn);
          }

          if (digitalRead(SETpin) == 0) {
            delay(200);
            if (digitalRead(SETpin) == 0) {
              reg++;
              mn1[ref1][ref2] = mn;
              delay(200);
            }
          }
        }
        if (reg == 3) {
          lcd.setCursor(8, 0);
          lcd.print(" ");
          lcd.setCursor(12, 0);
          lcd.print(">");
        }
        while (reg == 3) {

          if (digitalRead(Down) == 0) {
            delay(100);
            se --;
          }
          if (digitalRead(Up) == 0) {
            delay(100);
            se ++;
          }
          if (se > 59) {
            se = 0;
          }
          if (se == -1) {
            se = 59;
          }
          if (se < 10 ) {
            lcd.setCursor(13, 0);
            lcd.print("0");
            lcd.print(se);
          }
          if (se >= 10 ) {
            lcd.setCursor(13, 0);
            lcd.print(se);
          }

          if (digitalRead(SETpin) == 0) {
            delay(200);
            if (digitalRead(SETpin) == 0) {
              reg++;
              se1[ref1][ref2] = se;
              delay(200);
            }
          }
        }
        if (reg == 4) {
          lcd.setCursor(12, 0);
          lcd.print(" ");
          lcd.setCursor(4, 1);
          lcd.print(">");
        }
        while (reg == 4) {

          if (digitalRead(Down) == 0) {
            delay(100);
            hr --;
          }
          if (digitalRead(Up) == 0) {
            delay(100);
            hr ++;
          }
          if (hr > 24) {
            hr = 0;
          }
          if (hr == -1) {
            hr = 24;
          }
          if (hr < 10 ) {
            lcd.setCursor(5, 1);
            lcd.print("0");
            lcd.print(hr);
          }
          if (hr >= 10 ) {
            lcd.setCursor(5, 1); ;
            lcd.print(hr);
          }

          if (digitalRead(SETpin) == 0) {
            delay(200);
            if (digitalRead(SETpin) == 0) {
              reg++;
              Dhr1[ref1][ref2] = hr;
              delay(200);
            }
          }
        }

        if (reg == 5) {
          lcd.setCursor(4, 1);
          lcd.print(" ");
          lcd.setCursor(8, 1);
          lcd.print(">");
        }
        while (reg == 5) {

          if (digitalRead(Down) == 0) {
            delay(100);
            mn --;
          }
          if (digitalRead(Up) == 0) {
            delay(100);
            mn ++;
          }
          if (mn > 59) {
            mn = 0;
          }
          if (mn == -1) {
            mn = 59;
          }
          if (mn < 10 ) {
            lcd.setCursor(9, 1);
            lcd.print("0");
            lcd.print(mn);
          }
          if (mn >= 10 ) {
            lcd.setCursor(9, 1);
            lcd.print(mn);
          }

          if (digitalRead(SETpin) == 0) {
            delay(200);
            if (digitalRead(SETpin) == 0) {
              reg++;
              Dmn1[ref1][ref2] = mn;
              delay(200);
            }
          }
        }
        if (reg == 6) {
          lcd.setCursor(8, 1);
          lcd.print(" ");
          lcd.setCursor(12, 1);
          lcd.print(">");
        }
        while (reg == 6) {

          if (digitalRead(Down) == 0) {
            delay(100);
            se --;
          }
          if (digitalRead(Up) == 0) {
            delay(100);
            se ++;
          }
          if (se > 59) {
            se = 0;
          }
          if (se == -1) {
            se = 59;
          }
          if (se < 10 ) {
            lcd.setCursor(13, 1);
            lcd.print("0");
            lcd.print(se);
          }
          if (se >= 10 ) {
            lcd.setCursor(13, 1);
            lcd.print(se);
          }

          if (digitalRead(SETpin) == 0) {
            delay(200);
            if (digitalRead(SETpin) == 0) {
              reg++;
              reg = 0;
              Dse1[ref1][ref2] = se;
              delay(200);
            }
          }
        }

        if (item2 == 6) {
          lcd.setCursor(4, 0);
          lcd.print("<Exit>      ");
          lcd.setCursor(3, 1);
          lcd.print(">>menu<<     ");
        }
      }
      if (digitalRead(SETpin) == 0 && item2 == 6) {
        SET = 0;
        item1 = 0; item2 = 0;
        lcd.clear();
        delay(100);
      }

      if (digitalRead(Down) == 0 || digitalRead(Up) == 0) {
        if (digitalRead(Down) == 0) {
          delay(100);
          lcd.clear() ;
          item2 --;
        }
        if (digitalRead(Up) == 0) {
          delay(100);
          lcd.clear() ;
          item2 ++;
        }
        if (item2 == -1 && SET == 2 ) {
          lcd.clear() ;
          item2 = 6;
        }
        if (item2 == 7 && SET == 2 ) {
          lcd.clear() ;
          item2 = 0;
        }
        lcd.setCursor(5, 0); lcd.print(hr1[ref1][item2]);
        lcd.setCursor(9, 0); lcd.print(mn1[ref1][item2]);
        lcd.setCursor(13, 0); lcd.print(se1[ref1][item2]);
        lcd.setCursor(5, 1); lcd.print(Dhr1[ref1][item2]);
        lcd.setCursor(9, 1); lcd.print(Dmn1[ref1][item2]);
        lcd.setCursor(13, 1); lcd.print(Dse1[ref1][item2]);

      }
    }
  }
}






Thousands of thanks.

Please edit your post to add code tags ("</>" post editor button).

2 Likes

Easy, go get the library that supports the I2C backpack. By the way, you didn't follow the forum guidelines for posting code. If you go back and look, you'll see that it is a mess.

2 Likes

To change from a LCD controlled with an 8 bit LiquidCrystal library to an I2C display you only need to change to the right include files, the right constructor and maybe the begin in setup().

I recommend the hd44780 library filor I2C didplays. The library will automatically find the I2C address and the LCD to I2C expander (backpack) pin mappinng. The hd44780 library is available through the IDE library mansger.

2 Likes

Here is a basic sketch for LCD using the hd44780 library to show the include files, the constructor and the begin function.

// hd44780 simple "Hello World" by c gounding AKA groundFungus
// hd44780 library see https://github.com/duinoWitchery/hd44780
// thehd44780 library is available through the IDE library manager
// if there are other devices on the I2C bus and one of those devices 
// has a lower I2C address than the LCD, that device could be detected 
// as the LCD. In that case simply put the LCD address in the constructor.

#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip

// LCD geometry
const int LCD_COLS = 16;
const int LCD_ROWS = 2;

void setup()
{
   lcd.begin(LCD_COLS, LCD_ROWS);
   lcd.clear();
   lcd.print("Hello World");
   lcd.setCursor(0, 1);
   lcd.print("Millis ");
}

void loop()
{
   updateLCD();
}

void updateLCD()
{
   static unsigned long lcdTimer = 0;
   unsigned long lcdInterval = 500;  // update 2 times per second
   if (millis() - lcdTimer >= lcdInterval)
   {
      lcdTimer = millis();
      lcd.setCursor(8, 1);
      lcd.print("       "); // overwrite old data
      lcd.setCursor(8, 1);  // reset the cursor
      lcd.print(millis());
   }
}

If your have trouble run the diagnostic sketch from the library example code and post the results for help.

Also, in the red box, is the extensive documentation for the library.

1 Like

Here is a basic sketch I have been running with a GPS module. This is for a 20 X 4 LCD but you should get the idea.

#include <Adafruit_GPS.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);  // set the LCD address to 0x27 for a 20 chars and 4 line display


// Connect to the GPS on the hardware I2C port
Adafruit_GPS GPS(&Wire);

// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
// Set to 'true' if you want to debug and listen to the raw GPS sentences
#define GPSECHO false

uint32_t timer = millis();


void setup() {
  //while (!Serial);  // uncomment to have the sketch wait until Serial is ready

  // connect at 115200 so we can read the GPS fast enough and echo without dropping chars
  // also spit it out
  Serial.begin(115200);
  Serial.println("Adafruit I2C GPS library basic test!");

  // Initialize LCD screen
  lcd.init();  // initialize the lcd
  lcd.init();
  lcd.backlight();


  // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
  GPS.begin(0x10);  // The I2C address to use is 0x10
  // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  // uncomment this line to turn on only the "minimum recommended" data
  //GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
  // For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
  // the parser doesn't care about other sentences at this time
  // Set the update rate
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);  // 1 Hz update rate
  // For the parsing code to work nicely and have time to sort thru the data, and
  // print it out we don't suggest using anything higher than 1 Hz

  // Request updates on antenna status, comment out to keep quiet
  GPS.sendCommand(PGCMD_ANTENNA);

  delay(1000);

  // Ask for firmware version
  GPS.println(PMTK_Q_RELEASE);
}

void loop()  // run over and over again
{
  // read data from the GPS in the 'main loop'
  char c = GPS.read();
  // if you want to debug, this is a good time to do it!
  if (GPSECHO)
    if (c) Serial.print(c);
  // if a sentence is received, we can check the checksum, parse it...
  if (GPS.newNMEAreceived()) {
    // a tricky thing here is if we print the NMEA sentence, or data
    // we end up not listening and catching other sentences!
    // so be very wary if using OUTPUT_ALLDATA and trying to print out data
    Serial.println(GPS.lastNMEA());  // this also sets the newNMEAreceived() flag to false
    if (!GPS.parse(GPS.lastNMEA()))  // this also sets the newNMEAreceived() flag to false
      return;                        // we can fail to parse a sentence in which case we should just wait for another
  }

  // approximately every 2 seconds or so, print out the current stats
  if (millis() - timer > 2000) {
    timer = millis();  // reset the timer
    Serial.print("\nTime: ");
    if (GPS.hour < 10) { Serial.print('0'); }
    Serial.print(GPS.hour, DEC);
    Serial.print(':');
    if (GPS.minute < 10) { Serial.print('0'); }
    Serial.print(GPS.minute, DEC);
    Serial.print(':');
    if (GPS.seconds < 10) { Serial.print('0'); }
    Serial.print(GPS.seconds, DEC);
    Serial.print('.');
    if (GPS.milliseconds < 10) {
      Serial.print("00");
    } else if (GPS.milliseconds > 9 && GPS.milliseconds < 100) {
      Serial.print("0");
    }
    Serial.println(GPS.milliseconds);
    Serial.print("Date: ");
    Serial.print(GPS.day, DEC);
    Serial.print('/');
    Serial.print(GPS.month, DEC);
    Serial.print("/20");
    Serial.println(GPS.year, DEC);
    Serial.print("Fix: ");
    Serial.print((int)GPS.fix);
    Serial.print(" quality: ");
    Serial.println((int)GPS.fixquality);
    if (GPS.fix) {
      Serial.print("Location: ");
      Serial.print(GPS.latitude, 4);
      Serial.print(GPS.lat);
      Serial.print(", ");
      Serial.print(GPS.longitude, 4);
      Serial.println(GPS.lon);
      Serial.print("Speed (knots): ");
      Serial.println(GPS.speed);
      Serial.print("Angle: ");
      Serial.println(GPS.angle);
      Serial.print("Altitude: ");
      Serial.println(GPS.altitude);
      Serial.print("Satellites: ");
      Serial.println((int)GPS.satellites);

      // LCD Data
      lcd.clear();
      lcd.backlight();
      lcd.setCursor(3, 0);
      lcd.print("GMT Time & Date");
      lcd.setCursor(0, 1);
      lcd.print("Time: ");
      if (GPS.hour < 10) { lcd.print('0'); }
      lcd.print(GPS.hour, DEC);
      lcd.print(':');
      if (GPS.minute < 10) { lcd.print('0'); }
      lcd.print(GPS.minute, DEC);
      lcd.print(':');
      if (GPS.seconds < 10) { lcd.print('0'); }
      lcd.print(GPS.seconds, DEC);
      lcd.print('.');
      if (GPS.milliseconds < 10) {
        lcd.print("00");
      } else if (GPS.milliseconds > 9 && GPS.milliseconds < 100)
        lcd.print("0");
      {
        lcd.setCursor(0, 2);
        lcd.print("Date: ");
        lcd.print(GPS.month, DEC);
        lcd.print('/');
        lcd.print(GPS.day, DEC);
        lcd.print("/20");
        lcd.print(GPS.year, DEC);
      }
    }
  }
}

I should give the library suggested by groundFungus a try. Note in my sample code I need to spell out the I2C address.

Ron

1 Like

Hi guys, sorry for the messy code posting. My problem is that I don't know how to make it beautiful. Where can I read about how to edit it properly? and I'd also like to mention that I'm new to Arduino and to my shame I don't think I'd be able to tweak the code myself, so I'd really appreciate it if you could modify it for me.

Thank you and once again many apologies for the messy post.

Please read the forum guidelines to see how to properly post code and some information on making a good post.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please go back and fix your original post.

If you fix the posting of your code so that it is easy to copy I will insert the necessary parts of the hd44780 library and re-post it.

1 Like

Well let's figure every enthusiast starts somewhere. So here is how a little of somewhere begins here. First your Arduino IDE under tools the first option is Auto Format. That feature will nicely format your code making it easier to read and give it a nice flow. Next when copying and pasting code into your post if you look at the strip above the posting block of your text you see a little </> symbol. Mouse over it and you see Preformatted text. When you click that you have a place to put your code. I suggest you read How to get the best out of this forum. Reading the link will explain plenty. When you post correctly and clearly state your objectives it is much easier for those wishing to help you give good help and advice.

Finally this is a self help place. Nobody will write your code for you. You are expected to for example post your code and state your objectives. Others will offer suggestions. On a commercial level project then yes, people will do your project and there is a fee.

Earlier I gave you a code sample based on what I think you want to achieve. It's not a full over the counter wrapped in a bow solution. It's a start. :slight_smile:

Ron

soil fungus
and Ron_Blain. Thank you very much for the guidance. I hope it was posted correctly this time.

Well done on posting the code properly.

Here is your code changed to use the hd44780 library. If you have not, yet, you will have to install the hd44780 library. Like I said the hd44780 library is available via the Arduino IDE library manager. If you need to know how to install an Arduino library.

#include <Wire.h>                   // for I2C communication

//#include <LiquidCrystal.h>      // for LCD *****************************
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

#include <RTClib.h>
#include <avr/wdt.h>// for RTC
// create LCD with I2C address 0x27, 16 characters per line, 2 lines
RTC_DS3231 rtc;

#define SETpin 4
#define Up 3
#define Down 2
#define FW A0
#define BW A1
#define Feed A2
#define FCFW 13
#define FCBW 12
int timz = 0, k = 0, key = 1, err = 0, retour = 0;
int hr1 [4][6] = {
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0}
};
int mn1 [4][6] = {
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0}
};
int se1 [4][6] = {
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0}
};

int Dhr1 [4][6] = {
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0}
};
int Dmn1 [4][6] = {
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0}
};
int Dse1 [4][6] = {
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0}
};

int SET = 0 , i = 0, j = 0, SET1 = 0, SET2 = 0, SET3 = 0, hr = 0, mn = 0, se = 0, item1 = 0, item2 = 0, reg = 0, ref1 , ref2 ;
const int rs = 5, en = 10, d4 = 6, d5 = 7, d6 = 8, d7 = 9;

//LiquidCrystal lcd(rs, en, d4, d5, d6, d7); *****************************
hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip


// create rtc for the DS3231 RTC module, address is fixed at 0x68
/*
   function to update RTC time using user input
*/
void updateRTC()
{

  lcd.clear();  // clear LCD display
  lcd.setCursor(0, 0);
  lcd.print("Edit Mode...");

  // ask user to enter new date and time
  const char txt[6][15] = { "year [4-digit]", "month [1~12]", "day [1~31]",
                            "hours [0~23]", "minutes [0~59]", "seconds [0~59]"
                          };
  String str = "";
  long newDate[6];

  while (Serial.available()) {
    Serial.read();  // clear serial buffer
  }

  for (int i = 0; i < 6; i++) {

    Serial.print("Enter ");
    Serial.print(txt[i]);
    Serial.print(": ");

    while (!Serial.available()) {
      ; // wait for user input
    }

    str = Serial.readString();  // read user input
    newDate[i] = str.toInt();   // convert user input to number and save to array

    Serial.println(newDate[i]); // show user input
  }

  // update RTC
  rtc.adjust(DateTime(newDate[0], newDate[1], newDate[2], newDate[3], newDate[4], newDate[5]));
  Serial.println("RTC Updated!");
}


/*
   function to update LCD text
*/
void updateLCD()
{

  /*
     create array to convert digit days to words:

     0 = Sunday    |   4 = Thursday
     1 = Monday    |   5 = Friday
     2 = Tuesday   |   6 = Saturday
     3 = Wednesday |
  */
  const char dayInWords[7][4] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};

  /*
     create array to convert digit months to words:

     0 = [no use]  |
     1 = January   |   6 = June
     2 = February  |   7 = July
     3 = March     |   8 = August
     4 = April     |   9 = September
     5 = May       |   10 = October
     6 = June      |   11 = November
     7 = July      |   12 = December
  */
  const char monthInWords[13][4] = {" ", "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
                                    "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"
                                   };

  // get time and date from RTC and save in variables
  DateTime rtcTime = rtc.now();

  int ss = rtcTime.second();
  int mm = rtcTime.minute();
  int hh = rtcTime.hour();
  int DD = rtcTime.dayOfTheWeek();
  int dd = rtcTime.day();
  int MM = rtcTime.month();
  int yyyy = rtcTime.year();

  // move LCD cursor to upper-left position
  lcd.setCursor(0, 0);

  // print date in dd-MMM-yyyy format and day of week
  if (dd < 10) lcd.print("0");  // add preceeding '0' if number is less than 10
  lcd.print(dd);
  lcd.print("-");
  lcd.print(monthInWords[MM]);
  lcd.print("-");
  lcd.print(yyyy);

  lcd.print("  ");
  lcd.print(dayInWords[DD]);

  // move LCD cursor to lower-left position
  lcd.setCursor(0, 1);

  // print time in 12H format
  if (hh < 10) lcd.print("0");
  lcd.print(hh);
  lcd.print(':');

  if (mm < 10) lcd.print("0");
  lcd.print(mm);
  lcd.print(':');

  if (ss < 10) lcd.print("0");
  lcd.print(ss);

  if (rtcTime.isPM()) lcd.print(" PM"); // print AM/PM indication
  else lcd.print(" AM");
}



////////////////////////////////////////////////////////////////////
void setup()
{
  Serial.begin(9600); // initialize serial

  
  lcd.begin(16, 2); 
  lcd.backlight();  // added switch-on lcd backlight ****************
  
  rtc.begin();   // initialize rtc
  pinMode(SETpin, INPUT_PULLUP);
  pinMode(Up, INPUT_PULLUP);
  pinMode(Down, INPUT_PULLUP);
  pinMode(FW, OUTPUT);
  pinMode(BW, OUTPUT);

  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  pinMode(Feed, OUTPUT);
  //wdt_enable(WDTO_120MS);

  // attachInterrupt(digitalPinToInterrupt(2), UP, FALLING);
  // attachInterrupt(digitalPinToInterrupt(3), DOWN, FALLING);
}





void loop()
{
  updateLCD();  // update LCD text

  if (Serial.available()) {
    char input = Serial.read();
    if (input == 'u') updateRTC();  // update RTC time
  }

  for (int i = 0 ; i < 6; i++) {
    DateTime rtcTime = rtc.now();
    if (hr1[0][i] == rtcTime.hour() && mn1[0][i] == rtcTime.minute() && se1[0][i] == rtcTime.second()) {
      digitalWrite(A0, HIGH);
    }
    if (hr1[1][i] == rtcTime.hour() && mn1[1][i] == rtcTime.minute() && se1[1][i] == rtcTime.second()) {
      digitalWrite(A1, HIGH);
    }

    if (hr1[2][i] == rtcTime.hour() && mn1[2][i] == rtcTime.minute() && se1[2][i] == rtcTime.second()) {
      digitalWrite(A2, HIGH);
    }

    if (hr1[3][i] == rtcTime.hour() && mn1[3][i] == rtcTime.minute() && se1[3][i] == rtcTime.second()) {
      digitalWrite(A3, HIGH);
    }
    //////
    if (Dhr1[0][i] == rtcTime.hour() && Dmn1[0][i] == rtcTime.minute() && Dse1[0][i] == rtcTime.second()) {
      digitalWrite(A0, LOW);
    }
    if (Dhr1[1][i] == rtcTime.hour() && Dmn1[1][i] == rtcTime.minute() && Dse1[1][i] == rtcTime.second()) {
      digitalWrite(A1, LOW);
    }

    if (Dhr1[2][i] == rtcTime.hour() && Dmn1[2][i] == rtcTime.minute() && Dse1[2][i] == rtcTime.second()) {
      digitalWrite(A2, LOW);
    }

    if (hr1[3][i] == rtcTime.hour() && Dmn1[3][i] == rtcTime.minute() && Dse1[3][i] == rtcTime.second()) {
      digitalWrite(A3, LOW);
    }

  }

  if (digitalRead(SETpin) == 0) {
    delay(100);
    if (digitalRead(SETpin) == 0) {
      lcd.clear() ; SET = 1; item1 = 0; item2 = 0;
    }
  }

  while (SET == 1 || SET == 2) {
    while (SET == 1) {
      for (int j = 0; j < 2 ; j++) {
        if (item1 == j && SET == 1 ) {
          lcd.setCursor(4, 0);
          lcd.print("TIMER");
          lcd.print(" :");
          lcd.setCursor(4, 1);
          lcd.print("PUMP ");
          lcd.print(item1 + 1);

        }
      }

      if (digitalRead(Down) == 0) {
        delay(100);
        lcd.clear() ;
        item1 --;
      }
      if (digitalRead(Up) == 0) {
        delay(100);
        lcd.clear() ;
        item1 ++;
      }
      if (item1 == -1 && SET == 1 ) {
        lcd.clear() ;
        item1 = 3;
      }
      if (item1 == 4 && SET == 1 ) {
        lcd.clear() ;
        item1 = 0;
      }
      if (digitalRead(SETpin) == 0) {
        delay(100);
        if (digitalRead(SETpin) == 0) {
          lcd.clear() ; SET++;
          ref1 = item1;
          item1 = 0;
          delay(100);

          if (SET == 3) {
            SET = 0;
          }
        }
      }
    }

    while (SET == 2) {

      for (int i = 0; i < 2; i++) {
        if (item2 == i && i < 2 && SET == 2 ) {
          lcd.setCursor(0, 0);
          lcd.print("T");
          lcd.print(item2 + 1);
          lcd.print(" :");
          lcd.setCursor(7, 0);
          lcd.print("h");
          lcd.setCursor(11, 0);
          lcd.print("m");
          lcd.setCursor(15, 0);
          lcd.print("s");
          lcd.setCursor(0, 1);
          lcd.print("D");
          lcd.print(item2 + 1);
          lcd.print(" :");
          lcd.setCursor(7, 1);
          lcd.print("h");
          lcd.setCursor(11, 1);
          lcd.print("m");
          lcd.setCursor(15, 1);
          lcd.print("s");

        }
        if (item2 < 2 && digitalRead(SETpin) == 0)
        {
          ref2 =  item2;
          item2 = 0;
          reg++;

          delay(200);
        }
        if (reg == 1) {
          lcd.setCursor(4, 0);
          lcd.print(">");
        }
        while (reg == 1) {

          if (digitalRead(Down) == 0) {
            delay(100);
            hr --;
          }
          if (digitalRead(Up) == 0) {
            delay(100);
            hr ++;
          }
          if (hr > 24) {
            hr = 0;
          }
          if (hr == -1) {
            hr = 24;
          }
          if (hr < 10 ) {
            lcd.setCursor(5, 0);
            lcd.print("0");
            lcd.print(hr);
          }
          if (hr >= 10 ) {
            lcd.setCursor(5, 0);
            lcd.print(hr);
          }

          if (digitalRead(SETpin) == 0) {
            delay(200);
            if (digitalRead(SETpin) == 0) {
              reg++;
              hr1[ref1][ref2] = hr;
              delay(200);
            }
          }
        }
        if (reg == 2) {
          lcd.setCursor(4, 0);
          lcd.print(" ");
          lcd.setCursor(8, 0);
          lcd.print(">");
        }
        while (reg == 2) {

          if (digitalRead(Down) == 0) {
            delay(100);
            mn --;
          }
          if (digitalRead(Up) == 0) {
            delay(100);
            mn ++;
          }
          if (mn > 59) {
            mn = 0;
          }
          if (mn == -1) {
            mn = 59;
          }
          if (mn < 10 ) {
            lcd.setCursor(9, 0);
            lcd.print("0");
            lcd.print(mn);
          }
          if (mn >= 10 ) {
            lcd.setCursor(9, 0);
            lcd.print(mn);
          }

          if (digitalRead(SETpin) == 0) {
            delay(200);
            if (digitalRead(SETpin) == 0) {
              reg++;
              mn1[ref1][ref2] = mn;
              delay(200);
            }
          }
        }
        if (reg == 3) {
          lcd.setCursor(8, 0);
          lcd.print(" ");
          lcd.setCursor(12, 0);
          lcd.print(">");
        }
        while (reg == 3) {

          if (digitalRead(Down) == 0) {
            delay(100);
            se --;
          }
          if (digitalRead(Up) == 0) {
            delay(100);
            se ++;
          }
          if (se > 59) {
            se = 0;
          }
          if (se == -1) {
            se = 59;
          }
          if (se < 10 ) {
            lcd.setCursor(13, 0);
            lcd.print("0");
            lcd.print(se);
          }
          if (se >= 10 ) {
            lcd.setCursor(13, 0);
            lcd.print(se);
          }

          if (digitalRead(SETpin) == 0) {
            delay(200);
            if (digitalRead(SETpin) == 0) {
              reg++;
              se1[ref1][ref2] = se;
              delay(200);
            }
          }
        }
        if (reg == 4) {
          lcd.setCursor(12, 0);
          lcd.print(" ");
          lcd.setCursor(4, 1);
          lcd.print(">");
        }
        while (reg == 4) {

          if (digitalRead(Down) == 0) {
            delay(100);
            hr --;
          }
          if (digitalRead(Up) == 0) {
            delay(100);
            hr ++;
          }
          if (hr > 24) {
            hr = 0;
          }
          if (hr == -1) {
            hr = 24;
          }
          if (hr < 10 ) {
            lcd.setCursor(5, 1);
            lcd.print("0");
            lcd.print(hr);
          }
          if (hr >= 10 ) {
            lcd.setCursor(5, 1); ;
            lcd.print(hr);
          }

          if (digitalRead(SETpin) == 0) {
            delay(200);
            if (digitalRead(SETpin) == 0) {
              reg++;
              Dhr1[ref1][ref2] = hr;
              delay(200);
            }
          }
        }

        if (reg == 5) {
          lcd.setCursor(4, 1);
          lcd.print(" ");
          lcd.setCursor(8, 1);
          lcd.print(">");
        }
        while (reg == 5) {

          if (digitalRead(Down) == 0) {
            delay(100);
            mn --;
          }
          if (digitalRead(Up) == 0) {
            delay(100);
            mn ++;
          }
          if (mn > 59) {
            mn = 0;
          }
          if (mn == -1) {
            mn = 59;
          }
          if (mn < 10 ) {
            lcd.setCursor(9, 1);
            lcd.print("0");
            lcd.print(mn);
          }
          if (mn >= 10 ) {
            lcd.setCursor(9, 1);
            lcd.print(mn);
          }

          if (digitalRead(SETpin) == 0) {
            delay(200);
            if (digitalRead(SETpin) == 0) {
              reg++;
              Dmn1[ref1][ref2] = mn;
              delay(200);
            }
          }
        }
        if (reg == 6) {
          lcd.setCursor(8, 1);
          lcd.print(" ");
          lcd.setCursor(12, 1);
          lcd.print(">");
        }
        while (reg == 6) {

          if (digitalRead(Down) == 0) {
            delay(100);
            se --;
          }
          if (digitalRead(Up) == 0) {
            delay(100);
            se ++;
          }
          if (se > 59) {
            se = 0;
          }
          if (se == -1) {
            se = 59;
          }
          if (se < 10 ) {
            lcd.setCursor(13, 1);
            lcd.print("0");
            lcd.print(se);
          }
          if (se >= 10 ) {
            lcd.setCursor(13, 1);
            lcd.print(se);
          }

          if (digitalRead(SETpin) == 0) {
            delay(200);
            if (digitalRead(SETpin) == 0) {
              reg++;
              reg = 0;
              Dse1[ref1][ref2] = se;
              delay(200);
            }
          }
        }

        if (item2 == 6) {
          lcd.setCursor(4, 0);
          lcd.print("<Exit>      ");
          lcd.setCursor(3, 1);
          lcd.print(">>menu<<     ");
        }
      }
      if (digitalRead(SETpin) == 0 && item2 == 6) {
        SET = 0;
        item1 = 0; item2 = 0;
        lcd.clear();
        delay(100);
      }

      if (digitalRead(Down) == 0 || digitalRead(Up) == 0) {
        if (digitalRead(Down) == 0) {
          delay(100);
          lcd.clear() ;
          item2 --;
        }
        if (digitalRead(Up) == 0) {
          delay(100);
          lcd.clear() ;
          item2 ++;
        }
        if (item2 == -1 && SET == 2 ) {
          lcd.clear() ;
          item2 = 6;
        }
        if (item2 == 7 && SET == 2 ) {
          lcd.clear() ;
          item2 = 0;
        }
        lcd.setCursor(5, 0); lcd.print(hr1[ref1][item2]);
        lcd.setCursor(9, 0); lcd.print(mn1[ref1][item2]);
        lcd.setCursor(13, 0); lcd.print(se1[ref1][item2]);
        lcd.setCursor(5, 1); lcd.print(Dhr1[ref1][item2]);
        lcd.setCursor(9, 1); lcd.print(Dmn1[ref1][item2]);
        lcd.setCursor(13, 1); lcd.print(Dse1[ref1][item2]);

      }
    }
  }
}
1 Like

Thank you very much. I will test in the afternoon and come back with the result. All the best!

Hello. groundFungus and thank you very much, the display is correct. Do you think you could help me solve another problem in this code? This tree watering project should control 4 pumps at 6 different moments a day. In its current state, it shows that it controls only two pumps in two moments of the day.

Thank you.

Please post an attempt at programming it.

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