arduino lcd code problem lcd flickering - Page 1
Forum section: electronics / beginners

author: little_carlos
author_rank: Regular Contributor
timestamp: April 01, 2015, 05:28:23 pm
content: 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");}}

author: DavidDLC
author_rank: Frequent Contributor
timestamp: April 01, 2015, 05:32:21 pm
content: 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.

author: little_carlos
author_rank: Regular Contributor
timestamp: April 01, 2015, 05:40:13 pm
content: Quote from: DavidDLC on April 01, 2015, 05:32:21 pmcould you give me an example of how to do it?

author: DavidDLC
author_rank: Frequent Contributor
timestamp: April 01, 2015, 05:45:47 pm
content: 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.

author: DavidDLC
author_rank: Frequent Contributor
timestamp: April 01, 2015, 05:53:38 pm
content: You can set a flag for example with the next values:bomba1status = 0 Initial value and before entering the loopbomba1status = 1 bomba1 encendidabomba1status = 2 bomba1 apagadaand combine this with your if sentence.same for bomba2David.

author: free_electron
author_rank: Super Contributor
timestamp: April 01, 2015, 06:19:05 pm
content: remove the lcd.clear instructions.

author: little_carlos
author_rank: Regular Contributor
timestamp: April 01, 2015, 06:22:41 pm
content: Quote from: free_electron on April 01, 2015, 06:19:05 pmwhen 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

author: chamod
author_rank: Contributor
timestamp: April 01, 2015, 06:36:29 pm
content: 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.

author: DavidDLC
author_rank: Frequent Contributor
timestamp: April 01, 2015, 06:37:44 pm
content: Sorry, try this:if( (val == HIGH) && (bomba1status !=1) ) //bomba1status != 1, means the bomba is not on{bomba1status = 1 //bomba encendidalcd.clear();lcd.setCursor(0, 0);lcd.write("bomba1 encendida");}elseif ( (val == LOW) && (bomba1satus != 2) ){bomba1status = 2; //bomba apagadalcd.clear();lcd.setCursor(0, 0);lcd.write("bomba1 apagada");}

author: alexanderbrevig
author_rank: Frequent Contributor
timestamp: April 01, 2015, 06:41:51 pm
content: Try this (no clear)://bomba1status != 1, means the bomba is not onif( (val == HIGH) && (bomba1status !=1) ){bomba1status = 1lcd.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 clearlcd.write(" bomba1 apagada ");}

author: DavidDLC
author_rank: Frequent Contributor
timestamp: April 01, 2015, 06:43:12 pm
content: Quote from: chamod on April 01, 2015, 06:36:29 pmSeveral 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.

author: DavidDLC
author_rank: Frequent Contributor
timestamp: April 01, 2015, 06:45:55 pm
content: Quote from: alexanderbrevig on April 01, 2015, 06:41:51 pmO0

author: alexanderbrevig
author_rank: Frequent Contributor
timestamp: April 01, 2015, 06:52:51 pm
content: 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:#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 statebombaState = getBombaState();if (bombaState != previousBombaState) {//we detected a new state, let's update the displayupdateDisplay();//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 bomba2return 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!}

author: little_carlos
author_rank: Regular Contributor
timestamp: April 01, 2015, 06:53:04 pm
content: Quote from: DavidDLC on April 01, 2015, 06:45:55 pmthanks guys, made it work, but i really need some serious arduino lessons to take, thanks for your time