1

The code is this:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
unsigned long blinkTime = 0;
int ValueA = 22;
String placeHolder = " ";
void setup() {
 lcd.begin();
}
void loop() {
 lcd.setCursor(7, 0);
 if (millis() - blinkTime > 2000) {
 lcd.print(ValueA);
 blinkTime = millis();
 }
 if (millis() - blinkTime > 1000) {
 lcd.print(placeHolder);
 }
}

The I2C LCD library have a lcd.blink() function but it seems that only blinks a "cursor".

The problem with above code is the placeHolder, what if ValueA gets bigger than 2 digits?

Is there a better way to blink a value on LCD?

asked Jun 27, 2019 at 17:45
10
  • 1
    tip: lcd.setCursor(7, 0); is in both if blocks ... use it before the first if instead Commented Jun 27, 2019 at 18:12
  • write a function that accepts three parameters ... boolean show, byte position and int value .... 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() Commented Jun 27, 2019 at 18:19
  • @jsotola I've never wrote a function before, can you please post an example of what you just described? Commented Jun 27, 2019 at 18:40
  • google arduino functions ... arduino.cc/en/Reference/FunctionDeclaration Commented Jun 27, 2019 at 18:45
  • The LCD module has this feature built in. There is no need to implement yourself. Call lcd.blink(); and the current cursor position will blink. Commented Jun 27, 2019 at 19:01

1 Answer 1

2

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();
}
answered Jun 28, 2019 at 19:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.