Please be aware of all shipping deadlines before placing your order - we cannot guarantee orders will arrive before Christmas!

20x4 RGB LCD code advice If statement no text
Moderators: adafruit_support_bill, adafruit

20x4 RGB LCD code advice If statement no text

by Blimey on Sat May 25, 2013 3:20 pm

I had this code working on a 16x2 RGB LCD, but having upgraded to the 20x4 RGB LCD, I am not getting the same result.
The 20x4 screen is wired correctly and tests okay with the swirl sketch and all for lines work fine with simple text, so the problem must be the If statement I am using. The first line prints showing voltage and the value, but the 'if statement' should prompt the next line of text to display, but it very briefly partially shows then disappears...
I'd really appreciate some help. Thanks!
Code: Select all | TOGGLE FULL SIZE
//Test code for forum advice

int warningThreshold1 = 600; // sets lower threshold for if statement in void loop()

#include <LiquidCrystal.h>
#include <Wire.h>

#define REDLITE 3 // backlight color
#define GREENLITE 5 // backlight color

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

int brightness = 255; // change brightness by range 0 -> 255

// voltage measurement using internal reference voltage
int analogPin = 0; //The voltage is connected to Analog pin 0
// E #define CLS  0x01  //Character to send to the ATS-1 to clear the LCD and home the cursor 
#define SCALE_FACTOR .00488    //the voltage difference to change the 10 bit Arduino A/D reading by one 
#define TRUNC_VAL  4  //divisor for truncating lower bits. 2 will truncate LSB, 4 lowest 2 bits, 8 lowest 3 bits

void setup()
{
//Serial.begin(4800);   // opens serial port, sets data rate to 4800 baud to LCD display
}
void loop()
{
float voltage;
int rawAD;      //raw A/D reading value
rawAD = analogRead(analogPin);
voltage = (float)rawAD * SCALE_FACTOR*1000;  //convert the A/D reading to a type float and multiply by scaling factor (Then I added *1000 to change reading into milliVolts)
//lcd.clear();      // this clears the LCD screen
lcd.begin(20,4); // need to specify how many columns and rows are in the LCD unit
lcd.setCursor(0,0); // positions starting point on LCD, column 0, row 0 (that is, the top left of our LCD)

// E Serial.print("Voltage:");
lcd.print("Voltage: -"); // minus dash moved for spacing
// E Serial.print(voltage);  //note that Serial.print defaults to two digits after the decimal point.
lcd.print(voltage);
lcd.print("mV"); // adds mV after voltage reading
delay(250);   //put in a short delay between readings

//int val =analogRead(analogPin);
  if(voltage < 600) // if voltage less than 600mV then:
 {
    pinMode(REDLITE, OUTPUT); // RED backlight is ON
     pinMode(GREENLITE, LOW); // GREEN backlight is OFF
  lcd.setCursor(1,1);
  lcd.print("WARNING: FAULT");
}}
Blimey
 
Posts: 22
Joined: Fri Mar 01, 2013 6:03 pm

Re: 20x4 RGB LCD code advice If statement no text

by floresta on Sat May 25, 2013 3:34 pm

You certainly want to move the lcd.begin() statement out of loop().

Don
floresta
 
Posts: 221
Joined: Thu Jul 31, 2008 10:27 am
Location: Western New York, USA

Re: 20x4 RGB LCD code advice If statement no text

by Blimey on Sat May 25, 2013 5:45 pm

Thanks, Don. I thought that would fix the issue but I guess there is at least one more fault in my 'if statement' code as the second line text will not print on the lcd when it should (value less than 600). The backlight color changes as expected, following the if statement, just not the text... Any idea why? :?

Code: Select all | TOGGLE FULL SIZE
//int val =analogRead(analogPin);
  if(voltage < 600) // if voltage less than 600mV then:
{
    pinMode(REDLITE, OUTPUT); // RED backlight is ON
     pinMode(GREENLITE, LOW); // GREEN backlight is OFF
  lcd.setCursor(1,1);
  lcd.print("WARNING: FAULT");
}}
Blimey
 
Posts: 22
Joined: Fri Mar 01, 2013 6:03 pm

Re: 20x4 RGB LCD code advice If statement no text

by floresta on Sat May 25, 2013 6:05 pm

You certainly want to move the lcd.begin() statement out of loop().


I hope you didn't follow this literally and just remove it from loop(). It has to be in setup().

Why don't you comment out the 'lcd.setCursor(1,1);' line and see what happens. This should append your error message to wherever was sent previously. It won't look pretty but it will tell us if the information is getting to the LCD.

Don
floresta
 
Posts: 221
Joined: Thu Jul 31, 2008 10:27 am
Location: Western New York, USA

Re: 20x4 RGB LCD code advice If statement no text

by Blimey on Sun May 26, 2013 11:54 pm

Moving the lcd.begin() to setup() helped, but I still have some problems with the if statement (sketch code reshown below).

1. Why do I need to repeat the if(val<600) statement in order to get more than one action to occur for an if statement?
I thought as long as within the braces of that statement, any number of actions will be carried out?

2. The second problem is that the text does not change with the second if statement. I want each if statement to change the backlight color and text if values are 'less than 600' or 'greater than 600'. But following the second, 'greater than' if statement, the backlight changes to green (proving the statement works) but the text of the previous statement still remains displayed. Why is that?

Code: Select all | TOGGLE FULL SIZE
int val =analogRead(analogPin);
  if(val < 600)
  {
    pinMode(REDLITE, OUTPUT); // RED backlight is ON
     pinMode(GREENLITE, LOW); // GREEN backlight is OFF
     }
  if(voltage < 600) // if less than 600 then:
  {
  lcd.setCursor(1,1);
  lcd.print("WARNING!  FAULT!  ");
  lcd.setCursor(0,2);
  lcd.print("Check this ...  ");
  lcd.setCursor(0,3);
  lcd.print("Or try this ... ");
  }
 if(voltage > 600)
   {
     pinMode(REDLITE, LOW);      // RED backlight is OFF
     pinMode(GREENLITE, OUTPUT); // GREEN baclight is ON
  lcd.setCursor(2,2); // text on 2nd line
  lcd.print("No problem  "); // text on third line
  }
 


Thanks for any advice!
Blimey
 
Posts: 22
Joined: Fri Mar 01, 2013 6:03 pm

Re: 20x4 RGB LCD code advice If statement no text

by Blimey on Tue May 28, 2013 11:14 am

I don't understand what you mean, or what this will do?
You said, "Why don't you comment out the 'lcd.setCursor(1,1);' line and see what happens. This should append your error message to wherever was sent previously".

You want me to add "// start text at this LCD position", after lcd.setCurser(1,1); ?
No problem, but why will this help?

My sketch compiles okay, it is just that the text from the first if statement remains on the lcd despite the second if statement. That is what I need help with, please.
Blimey
 
Posts: 22
Joined: Fri Mar 01, 2013 6:03 pm

Re: 20x4 RGB LCD code advice If statement no text

by floresta on Tue May 28, 2013 2:42 pm

You want me to add "// start text at this LCD position", after lcd.setCurser(1,1); ?

No, this is what i meant:
Change this: lcd.setCursor(1,1);
To this: // lcd.setCursor(1,1);

Looking back I see that I didn't answer this: "...but the text of the previous statement still remains displayed. Why is that?"

Any information that is displayed on the LCD will remain there until overwritten. Information can only be removed by repositioning the cursor and writing 'space' characters over the old characters.

Don
floresta
 
Posts: 221
Joined: Thu Jul 31, 2008 10:27 am
Location: Western New York, USA