Author Topic: arduino lcd code problem lcd flickering  (Read 13598 times)

0 Members and 1 Guest are viewing this topic.

Offline little_carlosTopic starter

  • Regular Contributor
  • *
  • Posts: 133
arduino lcd code problem lcd flickering
« on: April 01, 2015, 05:28:23 pm »
hi guys, ive donde a code to show the status of a water pump with 2 motors, if any of the motors is on or off, and it does, it shows if its on or off, but in the screen it flickers a lot, especially the end of the message, the part of "apagada" or "encendida", whats wrong with the code?



 #include <LiquidCrystal.h>


LiquidCrystal lcd(7, 8, 9, 10, 11 , 12);
int bomba1  = 1;
int bomba2  = 2;
int emergencia = 3;
int val = 0;

void setup() {
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  pinMode(bomba1, INPUT);
  pinMode(bomba2, INPUT);
  pinMode(emergencia, INPUT);
 
}

void loop() {

  val = digitalRead(bomba1);
if(val == HIGH){
 lcd.clear();
  lcd.setCursor(0, 0);
  lcd.write("bomba1 encendida");
}
else{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.write("bomba1 apagada");
}
val = digitalRead(bomba2);
if(val == HIGH){
  lcd.clear();
  lcd.setCursor(0,1);
  lcd.write("bomba2 encendida");
}
else{
  lcd.clear();
  lcd.setCursor(0, 1);
  lcd.write("bomba2 apagada");
}

}
 

Offline DavidDLC

  • Frequent Contributor
  • **
  • Posts: 755
  • Country: us
Re: arduino lcd code problem lcd flickering
« Reply #1 on: April 01, 2015, 05:32:21 pm »
On the loop() routine try to change the LCD message ONLY when a change to the current status ocurrs.

Your code is setting the display message on every loop even if nothing has changed.

David.
 

Offline little_carlosTopic starter

  • Regular Contributor
  • *
  • Posts: 133
Re: arduino lcd code problem lcd flickering
« Reply #2 on: April 01, 2015, 05:40:13 pm »
On the loop() routine try to change the LCD message ONLY when a change to the current status ocurrs.

Your code is setting the display message on every loop even if nothing has changed.

David.

could you give me an example of how to do it?
 

Offline DavidDLC

  • Frequent Contributor
  • **
  • Posts: 755
  • Country: us
Re: arduino lcd code problem lcd flickering
« Reply #3 on: April 01, 2015, 05:45:47 pm »
I want you to try, one way of doing it is using flags. Google that, if you still cannot do it, me or somebody else can give you an example.

David.
 

Offline DavidDLC

  • Frequent Contributor
  • **
  • Posts: 755
  • Country: us
Re: arduino lcd code problem lcd flickering
« Reply #4 on: April 01, 2015, 05:53:38 pm »
You can set a flag for example with the next values:

bomba1status = 0     Initial value and before entering the loop
bomba1status = 1     bomba1 encendida
bomba1status = 2     bomba1 apagada

and combine this with your if sentence.

same for bomba2

David.
 

Offline free_electron

  • Super Contributor
  • ***
  • Posts: 8517
  • Country: us
    • SiliconValleyGarage
Re: arduino lcd code problem lcd flickering
« Reply #5 on: April 01, 2015, 06:19:05 pm »
remove the lcd.clear instructions.
Professional Electron Wrangler.
Any comments, or points of view expressed, are my own and not endorsed , induced or compensated by my employer(s).
 

Offline little_carlosTopic starter

  • Regular Contributor
  • *
  • Posts: 133
Re: arduino lcd code problem lcd flickering
« Reply #6 on: April 01, 2015, 06:22:41 pm »
remove the lcd.clear instructions.

when i remove the clear instructions it combines both messages, for example, when the input of bomba 1 is off, it says bomba apagada, when its on it says bomba encendida, but when it gets off again, it displays "bomba apagadada" the last "da" from "encendida" remains in apagada, thats the problem that im having
 

Offline chamod

  • Contributor
  • Posts: 20
  • Country: nz
Re: arduino lcd code problem lcd flickering
« Reply #7 on: April 01, 2015, 06:36:29 pm »
Small tip regarding lcd.clear(). The clear routine in most libraries takes a considerable time as it writes to the whole DDRAM(Even to the memory beyond the visible area. If you are only writing a word or two, it would be more efficient to go to the beginning and write spaces to the characters that need to be cleared.
 

Offline DavidDLC

  • Frequent Contributor
  • **
  • Posts: 755
  • Country: us
Re: arduino lcd code problem lcd flickering
« Reply #8 on: April 01, 2015, 06:37:44 pm »
Sorry, try this:

if( (val == HIGH)  &&  (bomba1status !=1) )  //bomba1status != 1, means the bomba is not on
{
bomba1status = 1                                    //bomba encendida
 lcd.clear();
  lcd.setCursor(0, 0);
  lcd.write("bomba1 encendida");
}
elseif ( (val == LOW)  && (bomba1satus != 2) )
{
bomba1status = 2;                                 //bomba apagada
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.write("bomba1 apagada");
}
 

Offline alexanderbrevig

  • Frequent Contributor
  • **
  • Posts: 700
  • Country: no
  • Musician, developer and EE hobbyist
    • alexanderbrevig.com
Re: arduino lcd code problem lcd flickering
« Reply #9 on: April 01, 2015, 06:41:51 pm »
Try this (no clear):

//bomba1status != 1, means the bomba is not on
if( (val == HIGH)  &&  (bomba1status !=1) ) 
{
  bomba1status = 1
  lcd.setCursor(0, 0);
  lcd.write("bomba1 encendida");
}
elseif ( (val == LOW)  && (bomba1satus != 2) )
{
  bomba1status = 2;
  lcd.setCursor(0, 0);
  //notice spaces pads to 16 char length, so will clear
  lcd.write(" bomba1 apagada ");
}
« Last Edit: April 01, 2015, 06:43:59 pm by alexanderbrevig »
 

Offline DavidDLC

  • Frequent Contributor
  • **
  • Posts: 755
  • Country: us
Re: arduino lcd code problem lcd flickering
« Reply #10 on: April 01, 2015, 06:43:12 pm »
Small tip regarding lcd.clear(). The clear routine in most libraries takes a considerable time as it writes to the whole DDRAM(Even to the memory beyond the visible area. If you are only writing a word or two, it would be more efficient to go to the beginning and write spaces to the characters that need to be cleared.

Several ways of doing this.

That is a good idea if you are only displaying the status of the pumps, but if later you want to do something else on the code for control for example is better to use flags and execute the code only when the status changes.

David.
 

Offline DavidDLC

  • Frequent Contributor
  • **
  • Posts: 755
  • Country: us
Re: arduino lcd code problem lcd flickering
« Reply #11 on: April 01, 2015, 06:45:55 pm »
Try this (no clear):

//bomba1status != 1, means the bomba is not on
if( (val == HIGH)  &&  (bomba1status !=1) ) 
{
  bomba1status = 1
  lcd.setCursor(0, 0);
  lcd.write("bomba1 encendida");
}
elseif ( (val == LOW)  && (bomba1satus != 2) )
{
  bomba1status = 2;
  lcd.setCursor(0, 0);
  //notice spaces pads to 16 char length, so will clear
  lcd.write(" bomba1 apagada ");
}


 :-+ O0
 

Offline alexanderbrevig

  • Frequent Contributor
  • **
  • Posts: 700
  • Country: no
  • Musician, developer and EE hobbyist
    • alexanderbrevig.com
Re: arduino lcd code problem lcd flickering
« Reply #12 on: April 01, 2015, 06:52:51 pm »
For the fun of it, here's a version that is slightly commented and should be easy to extend with more states and more behavior:

Code: [Select]
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11 , 12);

int bomba1  = 1;
int bomba2  = 2;
int emergencia = 3;
int bombaState = 0;
int previousBombaState = 0;

void setup() {
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  pinMode(bomba1, INPUT);
  pinMode(bomba2, INPUT);
  pinMode(emergencia, INPUT);

  bombaState = getBombaState();
  previousBombaState = bombaState;
}

void loop() {
  //check current bomba state
  bombaState = getBombaState();

  if (bombaState != previousBombaState) {
    //we detected a new state, let's update the display
    updateDisplay();
    //and maybe do something else like "sound the alarm" if appropriate for instance?
  }
 
  previousBombaState = bombaState;
}

int getBombaState() {
  // you can change this to add more states or
  // tweak the current rule for bomba1 and bomba2
  return digitalRead(bomba1) || digitalRead(bomba2);
}

void updateDisplay() {
  lcd.setCursor(0, 0);
  switch (bombaState) {
    case HIGH:
      lcd.write("bomba1 encendida");
      break;
    case LOW:
      lcd.write(" bomba2 apagada ");
      break;
  }
  //feel free to add more cases for more states, or do something else
  //it's up to you!
}
 

Offline little_carlosTopic starter

  • Regular Contributor
  • *
  • Posts: 133
Re: arduino lcd code problem lcd flickering
« Reply #13 on: April 01, 2015, 06:53:04 pm »
Try this (no clear):

//bomba1status != 1, means the bomba is not on
if( (val == HIGH)  &&  (bomba1status !=1) ) 
{
  bomba1status = 1
  lcd.setCursor(0, 0);
  lcd.write("bomba1 encendida");
}
elseif ( (val == LOW)  && (bomba1satus != 2) )
{
  bomba1status = 2;
  lcd.setCursor(0, 0);
  //notice spaces pads to 16 char length, so will clear
  lcd.write(" bomba1 apagada ");
}


 :-+ O0
thanks guys, made it work, but i really need some serious arduino lessons to take, thanks for your time
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf