Hi,
I'm working on a project where I'm using a 20x4 LCD and using a push button to "page". That part works fine.
The issue is that I'm reading data from a load cell and I'm not able to get the value to "dynamically" update on the LCD. When the code enters the "Page 2 section" it reads the value of the scale at that time but doesn't change if the scale changes, unless you "re-page" with the button.
Basically, when the display is on "Page 2" or any other page that I may add, I'd like the scale to operate as if though it's part of the main loop.
Attached is the code I'm using, some I've written and some I've gleaned from the internet.
Any help will be greatly appreciated.
/*
* This code create a MENU structure on a 16x2 LCD screen.
* Six (6) different screens are implemented, and you can travel
* thru them by pressing a button on Arduino PIN 4. Each press
* of the button advances one (1) screen.
*
* Made by Clovis Fritzen in 05/06/2017
* http://www.FritzenMaker.com
* http://www.FritzenLab.com.br
*/
//Based on YWROBOT's LiquidCrystal_I2C library, Library version:1.1
//Also based on the Debounce.ino sketch that comes with Arduino IDE
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "HX711.h"
LiquidCrystal_I2C lcd(0x3f,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
int WhichScreen =0; // This variable stores the current Screen number
boolean hasChanged = true;
const int buttonPin = 7; // the number of the pushbutton 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
HX711 LRscale;
#define calibration_factor 1190.0 //This value is obtained using the SparkFun_HX711_Calibration sketch
#define CLK 2
#define LROUT 3
int PagePin=7;
float LRWeight;
void setup()
{
lcd.init();
lcd.backlight();
pinMode(buttonPin,INPUT_PULLUP);
Serial.begin(9600);
//BEGIN SCALE SETUP
LRscale.begin(LROUT, CLK);
LRscale.set_scale(calibration_factor);
LRscale.tare();
//END BEGIN SCALE SETUP
}
void loop()
{
LRWeight = LRscale.get_units();
if (hasChanged == true) {
switch(WhichScreen) {
case 1:
{
firstScreen();
}
break;
case 2:
{
secondScreen();
}
break;
}
}
//-------------------------------
// BEGIN of the switch debouncing code
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
// reset the debouncing timer
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) {
hasChanged = true;
WhichScreen++;
}
} else {
hasChanged = false;
}
}
lastButtonState = reading;
// END of the switch Debouncing code
// --------------------------------------
if (WhichScreen > 2){
WhichScreen = 1;
}
}
void firstScreen()
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("First Screen");
}
void secondScreen()
{
Serial.println(LRWeight); //Printing to serial monitor, it 'updates' dynamically like I want the LCD to do
lcd.clear();
lcd.setCursor(4,1);
lcd.print(LRWeight); // This doesn't update dynamically. It only shows the static weight at the time secondScreen() was entered.
}