Go Down

Topic: Strange LCD behavior (Read 2031 times) previous topic - next topic

alfiesty

My project is an1280 mega with an I2C 2004 LCD.


code is
//com 11
char letter;
char Tmessage[30];
char Hmessage[30];
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); //set the LCD address to 0x27 for a 16 chars and 2 line display


void setup()
{
   Serial.begin(9600);   //Set up serial           
   Serial1.begin(9600);   //Set up APC220 wireless           
   Serial1.flush();
   lcd.init();
   lcd.backlight();
   lcd.setCursor(0, 0);
}

void loop()
{
int i = 0;
int h = 0;
int t = 0;
int count = 1;
  if (Serial1.available() >= 0) // is there data from other unit?
   {
     while( Serial1.available() && i < 40)
     {
        letter = Serial1.read(); // read rest of message into string
//        Serial.print(letter);
        if (count == 1)
          {
            Tmessage[t++] = letter;
            if (letter == 'F')           
            {
              Tmessage[t] = '\0';
              count = 2;
            }
          }
        else
          {
            Hmessage[h++] = letter;
          }
     Hmessage[h] = '\0';
     } 
   }
lcd.setCursor(0, 0);
lcd.print(Tmessage);
lcd.setCursor(0, 1);
lcd.print(Hmessage);
delay (300);
delay(3000);
}
   
everything works until the last 7 lines. If you comment the two lcd.print statments the backlight is lit continuously. If you leave them uncommented, the H/Tmessages display on the LCD,

alfiesty

Sorry, bad fingers!
add text
but, the LCD backlight goes off for the first delay(200), then turns on for the second delay(3000).
Jim

Grumpy_Mike

lcd.print(Tmessage);
Expects a string not a character array.

alfiesty


lcd.print(Tmessage);
Expects a string not a character array.

Grumpy_Mike, by the number of my posts, you can see I'm a relative newbie. I don't know the difference. This was the only way I could find to separate the text into two pieces. I thought in C a string was a char array. It does print what I want on the LCD. I'm not sure how this would have any effect on the backlight.
Jim

Grumpy_Mike

Quote
I'm not sure how this would have any effect on the backlight.

The problem with using libraries is that you don't know anything about the code or its quality. I suspect it is poor quality code.
A string is a character array but it is terminated by a null.
Forget the other stuff and just get your hello world working on the LCD first.

alfiesty


Quote
I'm not sure how this would have any effect on the backlight.

The problem with using libraries is that you don't know anything about the code or its quality. I suspect it is poor quality code.
A string is a character array but it is terminated by a null.
Forget the other stuff and just get your hello world working on the LCD first.

Now I am confused! if you follow my code, both strings are terminated by a '/0', and the LCD is displaying the correct text.
the code says

            Tmessage[t++] = letter;
            if (letter == 'F')           
            {
              Tmessage[t] = '\0';<-- termination
              count = 2;
            }
          }
        else
          {
            Hmessage[h++] = letter;
          }
     Hmessage[h] = '\0';<-- ditto

Jim



PaulS

It would be better to keep the strings NULL terminated. Every time you add a character, add a NULL. As it is, you only append a NULL to Tmessage when an F is received. No F, no NULL.

Remember, serial data arrives ssslllooowwwlllyyy. Loop may be called several million times between the first character arriving and the F arriving. Well, it would if those silly delay()s weren't there.

Your indenting makes it unclear exactly when Hmessage is NULL terminated.

Now, please try again to explain what the problem is, including where the serial data comes from.
The art of getting good answers lies in asking good questions.

alfiesty


Now, please try again to explain what the problem is, including where the serial data comes from.


I have an Arduino in my observatory in a workshop separate from where the  telescope is.  It  has a temperature sensor and 2 relays connected to a heater and a fan. I have another Arduino in the house 80 feet away across a creek. This one has an I2C 20x4 LCD and two push button on/off switches. They are communicating with each other through APC220 transceivers. I want it so when I want to work out there I can check the temperature and turn on the heater or fan as required. I do not want the room heated all the time, much too expensive( outside temperatures here can go as low as -29F). I have the temperature displaying on the LCD just fine with the code I posted however, I don't seem to get the observatory unit to get and decode commands coming back. I know they are being transmitted because I can see them on another APC220 connected to my PC. I'll just put up with the blinking backlight if I can somehow get 2 way communication working. The code I am sending is 11,- both off 21, heat on fan off ,- 12, heat off fan on,- 22, both on. I tried swapping APC220 modules between the PC and the observatory Arduino, same thing. I delay the second device long enough that the message could be totally there before I look for it. Anything else you need? If I get this working, the second unit will respond back to the first with relayS status, which I'll display on lineS 3 and 4 on the LCD.

Jim

liuzengqiang

Check if what Paul said was the source of your problem (very likely). On the other hand, may I suggest you use my Serial LCD backpack? If all you want is to display a few things on a remote display, hook the serial output from APC220 transceiver to my Serial LCD, save an arduino and you use the arduino with temperature sensor to print things like serial.print("Temp:"); serial.println(temp); every once in a while to the other APC220 transceiver. The serial display understands new line and scrolls the display besides doing a whole bunch of things if you want it to do, say sound an alarm if temp is too high/low. Take a look here:

http://www.inmojo.com/store/liudr-arduino-and-physics-gadgets/item/serial-lcd-back-pack---phi-panel/
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

Go Up