Blurred letters on LCD screen (20*4)

Hello, first of all, thank you for reviewing my question.
I am receiving the signal from an acceleration sensor, and I am viewing this information on an LCD screen (20*4 I2C), I am also writing an "if" condition, in which if it is greater than a certain number, it is also written on the screen LCD, but my problem is that when I upload the code, the text is blurred on the screen. But also when the "if" condition is met, the screen becomes clear, but when the condition is not met, the text becomes blurred again.

then I leave the code that I am using



#include<Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
int ADXLAddress= 0x53;
  
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified();




#include <LiquidCrystal_I2C.h> 
LiquidCrystal_I2C lcd(0x27,20,4);  



unsigned long time1=0;


long interval1= 60;




void setup()
{
  Serial.begin(115200);
  

  Wire.begin();
  if (!accel.begin())
  {          
    Wire.beginTransmission(ADXLAddress);
    Wire.endTransmission();
    Serial.println(" this sensor inactive");
    while (1);
  }
  




lcd.init();
  
  
  lcd.backlight();
  lcd.clear();

}

void loop()
{
  
   
  
 
  unsigned long fmillis1=millis();
if((fmillis1-time1)>=interval1){
time1=fmillis1;
  //sensor de aceleracion 
  sensors_event_t event;
  accel.getEvent (&event);
  Serial.print("acceleration" );
  Serial.println(event.acceleration.x);
  lcd.setCursor(0,1);
 lcd.print("acceleration: " );
 lcd.setCursor(13,1);
  lcd.println(event.acceleration.x);
  if (event.acceleration.x >= 1.00){
    lcd.setCursor(0,0);
    lcd.print("accelerated");}
    else  {
      
      lcd.clear();
       lcd.setCursor(0,0);
      lcd.print("");
     
      
    }
    }



  
}

I also leave a picture of the screen, when the "if" function is not fulfilled

and this photo, when the "if" function is fulfilled.

I think that you are updating the display too often. Slow it down. A human can't keep up with more than about 2 times a second (every 500ms). Or only update the part of the display when the data changes.

Please format your code. Use the IDE autoformat function (Tools, Auto Format). The way it is makes it harder to read and folllow. Do you really need all that excess white space?

There is no condition for the if statement below:

    else if {
      
      lcd.clear();
       lcd.setCursor(0,0);
      lcd.print("");
     
      
    }

The only time you need to write to the LCD is if something to be displayed has changed.

For example, there is no need to clear the screen and write "acceleracion" each pass through loop. Just update the acceleration value, if it has changed.

And, as mentioned above, never faster than 2-3 times per second.

the "else if" condition I was wrong, it's only "else". I have already fixed the code
The point is that when the given condition is not met, I want "accelerated" not to appear on the screen.
[/quote]

I don't understand

Excuse me, how do I update the screen when the data changes?

Store the last value of the acceleration and if the new value of the acceleration is significantly different, print it on the display and update the last stored value.

In loop(), you can use the "static" keyword so that a variable is not automatically initialized.

void loop() {
static int last_accel_x = 0;  //(or static float) set to zero only on the first pass of loop
...
if (event.acceleration.x != last_accel_x) {  //accel changed from last value
 lcd.setCursor(13,1);
  lcd.print(event.acceleration.x);
 lcd.print("  "); //erase previous trailing digits
 last_accel_x = event.acceleration.x;
}

You can also check whether the change is larger than a certain value:

if (abs (event.acceleration.x - last_accel_x) > 10) { //accel changed by more than 10 units

I'm reviewing the code, and it's helping me, thanks.
But I have a question, I want to put a conditional. for instance
If the acceleration is greater than 1,

  • if the condition is met, it can be written on the screen "accelerated",
  • but at the same time I want to display the acceleration that exists
    -when not met, just don't see the "accelerated"

do you understand me?
thank you very much for your help and attention

Look up compound if statements.

if (x > 1 and y < 2) do_something();

Here is your code after I formatted it with the autoformat function (ctrl-t or Tools, Auto Format) and removed the excess white space (blank lines).

#include<Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
int ADXLAddress = 0x53;

Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified();

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);

unsigned long time1 = 0;
long interval1 = 60;

void setup()
{
   Serial.begin(115200);

   Wire.begin();
   
   if (!accel.begin())
   {
      Wire.beginTransmission(ADXLAddress);
      Wire.endTransmission();
      Serial.println(" this sensor inactive");
      while (1);
   }
   lcd.init();
   lcd.backlight();
   lcd.clear();
}

void loop()
{
   unsigned long fmillis1 = millis();
   if ((fmillis1 - time1) >= interval1)
   {
      time1 = fmillis1;
      //sensor de aceleracion
      sensors_event_t event;
      accel.getEvent (&event);
      Serial.print("acceleration" );
      Serial.println(event.acceleration.x);
      lcd.setCursor(0, 1);
      lcd.print("acceleration: " );
      lcd.setCursor(13, 1);
      lcd.println(event.acceleration.x);
      if (event.acceleration.x >= 1.00)
      {
         lcd.setCursor(0, 0);
         lcd.print("accelerated");
      }
      else
      {
         lcd.clear();
         lcd.setCursor(0, 0);
         lcd.print("");
      }
   }
}

Hi, I've been testing how to fix this problem, and as they previously said, the time must be taken into account to display it on the LCD, and not use "lcd.clear" thus avoiding the problem it presented. Thank you

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.