Code for using 1602 display with setup value using EEPROM stored values

Hi all!

I’m new to Arduino and created small setup to use 1602 LCD display to indicate required values of LNG cargo tank pressure for officers who monitor parameters. I’m going to develop this further and expand code as soon as will gain more knowledge.

So the question is how good is the code and what can be improved, debugged and removed in order to make it more ‘’readable’’ and consume less power, potentially use less EEPROM cycles in order to save more cycles.

Scheme consists of:

  • Arduino UNO R3
  • LCD 1602 display 16x2
  • Two push buttons
  • 10K pot
  • Wiring
  • 1 x 220 Ohm resistor
  • I have powered it by stack of 6 x 1.5 Volts batteries (however want to make project more compact).

As mentioned want to make project more compact and use smallest possible microcontroller, replace battery stack with smaller rechargeable battery with charging option, replace LCD with smaller OLED or other options, will 3d print small enclosure to place everything inside + make power button + install button covers.

#include <LiquidCrystal.h>  //Library LiquidCrystal
#include <EEPROM.h>         //EEPROM library

const int rs = 12, en = 11, d4 = 7, d5 = 6, d6 = 5, d7 = 4;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

const int  Up_buttonPin   = 2;
const int  Down_buttonPin = 3;

int buttonPushCounter = 50;    // counter for the number of button presses
int up_buttonState = 0;       // current state of the up button
int up_lastButtonState = 0;   // previous state of the up button
int eepromAddress = 0; // address to store the pressure in EEPROM

int down_buttonState = 0;         // current state of the up button
int down_lastButtonState = 0;     // previous state of the up button
bool bPress = false;


void setup()
{
  Serial.begin(9600);
  pinMode( Up_buttonPin , INPUT_PULLUP);
  pinMode( Down_buttonPin , INPUT_PULLUP);
  buttonPushCounter = EEPROM.read(eepromAddress); // read the previous recorded pressure from EEPROM
  lcd.begin(16, 2);
  lcd.setCursor(1, 0);
  lcd.print("Tank pressure:  ");
  lcd.setCursor(4, 1);
  lcd.print(buttonPushCounter);
  lcd.setCursor(8, 1);
  lcd.print("mbar");
}
void loop()
{
  checkUp();
  checkDown();

  if ( bPress)
  {
    bPress = false;
    lcd.setCursor(4, 1);
    lcd.print("               ");
    lcd.setCursor(4, 1);
    lcd.print(buttonPushCounter);
    lcd.setCursor(8, 1);
  lcd.print("mbar");
  }
}
void checkUp()
{
  up_buttonState = digitalRead(Up_buttonPin);
  if (up_buttonState != up_lastButtonState)    // compare the buttonState to its previous state
  {
    if (up_buttonState == LOW)   // if the state has changed, increment the counter
    {
      bPress = true;  // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
      EEPROM.update(eepromAddress, buttonPushCounter);
    }
    else {
      Serial.println("off"); // if the current state is LOW then the button went from on to off:
    }
    delay(50);
  }

  up_lastButtonState = up_buttonState;   // save the current state as the last state, for next time through the loop
}

void checkDown()
{
  down_buttonState = digitalRead(Down_buttonPin);
  if (down_buttonState != down_lastButtonState)  // compare the buttonState to its previous state
  {
    if (down_buttonState == LOW)   // if the state has changed, increment the counter
    {
      bPress = true;
      buttonPushCounter--;  // if the current state is HIGH then the button went from off to on:
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
      EEPROM.update(eepromAddress, buttonPushCounter);
    } else {
      Serial.println("off");  // if the current state is LOW then the button went from on to off:
    }
    delay(50);
  }
  down_lastButtonState = down_buttonState;  // save the current state as the last state, for next time through the loop

}

can you take a step back and explain to mere mortals what this actually means ?

it seems the code is basically a plus and minus button adding or subtracting 1 mbar from the displayed value and saving that into EEPROM.

you could benefit from using a ePaper display (no power required to maintain the display but slow content update) and having your processor sleep most of the time unless a button is pressed

General idea is enhancing safety while peforming cargo operations on LNG vessel. I need to indicate operational parameters for officers while they keep cargo watch while we do transfer of LNG from/to terminal and operational parameters needs to be declared. I don’t like idea of using whiteboards and see programmable displays as a benefit to keep their focus on my requirements. Thus I want to make such a project so I can keep their attention on required limitations. I also want this to be an independent system from main cargo system so it can serve as a redundant alarm system. Only thing what I have to do - sharing of data stream from main sensors - I have a source of this data - but I’m looking towards creating a BT failsafe bridge between them so info can be updated and then I’ll hook up alarms basis this monitoring system.

Regarding displays - thank you for advise, I’ve got two small size displays to do checks with them. 1.54 inch e-Paper from Waveshare and 2.8 inch TFT touch shield for UNO R3. Will try to play them so see balance between appearance/ergonomics while using and power consumption.

OK

I would assume that anything electronic getting close to natural gas would need some sort of certification like ATEX / IECEx...

You’re totally right! All our equipment which is close enough by standard to cargo containment systems is ATEX/EX-proof certified.
Whatever is located within accommodation is allowed to be non-Exproof equipment, so it’s ok to use such devices in cargo control rooms within accommodation block.

I would use one of the button library (such as Button in easyRun or OneButton or Toggle or EasyButton or Bounce2, ...) to simplify the code

Depending on which arduino you go for, eeprom might not be the final choice, so I would not optimize this part until you’ve decided on the hardware.

If you want something tiny and well integrated, look at M5Stack devices. TTGO also has some ready made enclosures giving a pro look to your prototype

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