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
}