To blink a number (alternate between the number and empty space(s)), you could try the BlinkLCD
class in this sketch. It only works with numbers from 0 to 4294967295 or text (char
array, 16 characters max). It does not work with floating point numbers.
// 4540 bytes.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
class BlinkLCD{
private:
byte m_blinkState, m_startPosition, m_dataLength;
unsigned long m_dataToBlink, m_previousMillis, m_blinkRate;
char m_text[16 + 1];
public:
BlinkLCD(): m_blinkState(0), m_startPosition(0), m_dataLength(0),
m_dataToBlink(0), m_previousMillis(0), m_blinkRate(500),
m_text(){}
void Update(){
unsigned long m_currentMillis = millis();
if(m_currentMillis - m_previousMillis >= m_blinkRate){
byte m_row = 0;
m_blinkState = !m_blinkState;
m_previousMillis = m_currentMillis;
if(m_startPosition > 15){
m_startPosition -= 16;
m_row = 1;
}
lcd.setCursor(m_startPosition, m_row);
if(m_blinkState){
if((unsigned)strlen(m_text) > 0){
lcd.print(m_text);
}
else{
lcd.print(m_dataToBlink);
}
}
else{
for(byte i = 0; i < m_dataLength; i++){
lcd.print(" ");
}
}
}
}
void SetBlinkRate(unsigned long blinkRate){
m_blinkRate = blinkRate;
}
void SetNumber(unsigned long dataToBlink){
m_dataToBlink = dataToBlink;
}
void SetLength(byte dataLength){
m_dataLength = dataLength;
}
// LCD top row, columns 0 - 15
// LCD Bottom row, 16 - 31
void SetStartPosition(byte startPosition){
m_startPosition = startPosition;
}
void SetText(char text[]){
strcpy(m_text, text);
}
};
BlinkLCD TopRowLCD;
BlinkLCD BottomRowLCD;
BlinkLCD BottomRowLCDtext;
void setup(){
lcd.init();
lcd.backlight();
lcd.clear();
lcd.print("Temperature 35 C");
lcd.setCursor(0, 1);
lcd.print("Num = 4294967295");
}
void loop(){
// Simulate sensor data.
static byte counter = 35;
static unsigned long previousMillis = 0;
unsigned long currentMillis = millis();
// Update the temperature reading every 2 seconds.
if(currentMillis - previousMillis >= 2000){
previousMillis = currentMillis;
counter++;
if(counter > 45){counter = 35;}
lcd.setCursor(12, 0);
lcd.print(counter);
}
// Blink the temperature reading if it's over 40 C.
if(counter > 40){
TopRowLCD.SetNumber(counter);
TopRowLCD.SetStartPosition(12);
TopRowLCD.SetLength(2);
TopRowLCD.SetBlinkRate(250);
TopRowLCD.Update();
}
// Blink a number on the second row of the LCD
// with a default blink rate of 500ms ON, 500ms OFF.
BottomRowLCD.SetNumber(4294967295);
BottomRowLCD.SetStartPosition(22);
BottomRowLCD.SetLength(10);
BottomRowLCD.Update();
// Blink some text with at the default blink rate.
BottomRowLCDtext.SetText("Num");
BottomRowLCDtext.SetStartPosition(16);
BottomRowLCDtext.SetLength(3);
BottomRowLCDtext.Update();
}
lcd.setCursor(7, 0);
is in bothif
blocks ... use it before the firstif
insteadshow
, byteposition
and intvalue
....value
is the data to display ...position
is location of displayed data ...show
if true, display data, if false, display spaces .....show
is set by a timer function at the beginning of loop()