Ok, so "refresh" was a poor choice of words! What really happens in our sketch is that there are 4 different pages we display on the LCD. Any time you change pages, the LCD is cleared, the appropriate headings for that page ("WEIGHT", "<-NEW SET POINT", etc.) are written to the LCD, and the current data (weights, settings, etc.) is written to the LCD. Once that is done, the characters which contain current data are cleared and re-written with fresh data at whatever interval we choose (currently 500ms.) The rest of the display is left alone (no "refreshing") until we change pages again.
Our problem is that occasionally the entire screen will go blank at one of those regular intervals when only the data fields are supposed to be getting updated, and then the whole screen will come back at the next interval (headings and data fields, even though no new information for the headings was sent at that point.) I'm trying to figure out what is making the entire screen go blank and then come back, more or less on its own. Most situations I can think of that would clear the entire screen (power loss, crash of some sort, etc.) would not then restore the entire screen (including headings) automatically the next time you sent just a few characters (the data fields) to display.
Thanks in advance for any help or insights into our situation. Our code as it stands made this post too long to submit, so I've summarized the purpose of a few procedures below to save space (leaving all LCD related code untouched so you can see what's going on)
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(26, 40, 35, 33, 31, 28);
// These constants won't change:
const int sensorPin = A0; // pin that the sensor is attached to
const byte ledPin = 2; // pin that the LED is attached to
const byte buttonUp = 27;
const byte buttonDown = 29;
const byte buttonOne = 36;
const byte buttonTwo = 34;
const byte buttonThree = 32;
const byte buttonFour = 30;
const int stableCount = 1000; //How many times the stable loop must run before the value is accepted
// variables:
float sensorValue = 0; // the sensor value
float sensorSet = 0; // sensor set point
int timer = 0; //timer
float calibrateLow = 0; //
float calibrateHigh = 1000; //
float stableLoop = 5000;
boolean upState = true; //Pressing a button pulls the pin low. Since low == false...
boolean downState = true; //...we set these all to true (high)
boolean oneState = true;
boolean twoState = true;
boolean threeState = true;
boolean fourState = true;
byte page = 0;
const byte numValues = 50; // Define the number of samples to keep track of. The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input.
float values[numValues]; // the readings from the analog input
byte valueIndex = 0; // the index of the current reading
float valueTotal = 0; // the running total
float valueAverage = 0; // the average
long lastDisplay=0;
void setup() {
delay(1000);
pinMode(buttonUp, INPUT);
pinMode(buttonDown, INPUT);
pinMode(buttonOne, INPUT);
pinMode(buttonTwo, INPUT);
pinMode(buttonThree, INPUT);
pinMode(buttonFour, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(sensorPin, INPUT);
lcd.begin(20, 4); // set up the LCD's number of columns and rows:
delay(1000);
for (byte thisValue = 0; thisValue < numValues; thisValue++){
values[thisValue] = 0;
}
lcdPage(); //set display to page 0
}
// Main Loop
void loop() {
switch(checkButtons()){ //first, check for a button press
case 1: //first button is pressed
switch(page){ //what page are we on
case 0: //do nothing on page 0
break;
case 1: //pages 1-3 use stableValue to set variables
sensorSet=stableValue();
break;
case 2:
calibrateHigh=stableValue();
break;
case 3:
calibrateLow=stableValue();
break;
}
case 4:
while(fourState == LOW ){
checkButtons();
}
break;
case 5: //button 5 (up) is pressed
if (page==0){ //cycle backwards through pages
page=3;
}
else{
page--;
}
lcdPage(); //display new page
break;
case 6:
if (page==3){ //cycle forward through pages
page=0;
}
else{
page++;
}
lcdPage(); //display new page
break;
case 0: //0= no button press
if ((millis()-500)>lastDisplay){
switch(page){ //idle behavior - no button presses
case 0: //screen 0
lcd.setCursor(7,0);
lcd.print(" ");
delay(0);
lcd.setCursor(7,0);
lcd.print(mapValue(smoothValue())); //use smoothValue to display current reading
break;
default:
lcd.setCursor(8,3);
lcd.print(" ");
delay(0);
lcd.setCursor(8,3);
lcd.print(smoothValue());
break;
}
lastDisplay=millis();
}
}
}
float mapValue(float value){
// code to map values based on calibration points goes here
return (value*0.01);
}
float smoothValue(){
// code to average values goes here
return valueAverage;
}
byte checkButtons(){ //when called, checks each button against the xState boolean
// button checking code goes here
return 0; //if no buttons are turned on, send 0 to main
}
float stableValue(){ //used by the set and calibrate functions to find a stable scale value
// stable-value-finding code goes here
}
if ((millis()-500)>lastDisplay){ //every 500 ms...
lcd.setCursor(8,3); //clear each field
lcd.print(" ");
lcd.setCursor(13,1);
lcd.print(" ");
lcd.setCursor(16,3);
lcd.print(" ");
lcd.setCursor(8,3);
lcd.print(sensorValue); //output current sensor value
lcd.setCursor(13,1);
lcd.print(stableLoop); //output current loop value
lcd.setCursor(16,3);
lcd.print(timer); //output current count
lastDisplay=millis();
}
timer++; //increment timer
}
lcd.setCursor(19,0);
lcd.print(" ");
return stableLoop; //loop value has not changed in [stableCount] counts, output value.
}
void lcdPage(){
lcd.clear();
switch(page){
case 0:
lcd.setCursor(0,0);
lcd.print("WEIGHT");
lcd.setCursor(0,1);
lcd.print("SET");
lcd.setCursor(7,1);
lcd.print(mapValue(sensorSet));
break;
case 1:
lcd.setCursor(0,0);
lcd.print("<-NEW SET POINT");
lcd.setCursor(0,1);
lcd.print("RUNNING SET");
lcd.setCursor(0,3);
lcd.print("READING");
break;
case 2:
lcd.setCursor(0,0);
lcd.print("<-CALIBRATE HIGH");
lcd.setCursor(0,1);
lcd.print("RUNNING HIGH");
lcd.setCursor(0,3);
lcd.print("READING");
break;
case 3:
lcd.setCursor(0,0);
lcd.print("<-CALIBRATE LOW");
lcd.setCursor(0,1);
lcd.print("RUNNING LOW");
lcd.setCursor(0,3);
lcd.print("READING");
break;
}
return;
}