Go Down

Topic: lcd.print and Serial.print in one header.h file (Read 794 times) previous topic - next topic

Ed Armstrong

I'm trying to learn header files. I need to duplicate data that I send to an LCD and also send this over serial bluetooth to be displayed on my Android phone. The simple way to do this is go through my code and just duplicate all the lines like this:
lcd.print ("any string of 16 characters");
Serial.println("any string of 16 characters");
This is what I am doing now just as a test, it works, but this is hack and is unforgiving of things I might miss. It seems to me that some simple header file could do the job. However, it's not quite that simple as I wrote above. Some of the information displayed on the LCD is placed in a specific location like:
lcd.setCursor(8,1); // set to 8th character place, bottom line
lcd.print(PinVoltage,3); //displays value with 3 decimals resolution
You couldn't just send that over serial.println. Also sometimes once the data is used it is gone.
I'm thinking write to a 2x16 array then read it both with the LCD and bluetooth serial. I've been searching in hopes that someone created a header file which does all that so I could just use a different header in my code and have it all instantly solved, but no luck.

PaulS

You don't mention which class lcd is an instance of. If it's an instance of the LiquidCrystal class (or any other class, for that matter), create a new class that derives from LiquidCrystal (or whichever class lcd is an instance of).

Change the lcd instance to be an instance of your new class. Override the print methods that you use. In your print methods, invoke the base class' print() method and the Serial.print() method.

This way, you only need to make one call to lcd.print() to print to the lcd and the serial port.

It isn't clear what the issue with setting the position on the lcd is. Why does that matter when printing to the serial port?

Ed Armstrong

The reason I set the print to position on the LCD is to display something like a voltage value while a user adjusts a potentiometer. those 4 last digits will be a active and constantly updating floating point value while the other 28 are just holding the same text. Idealy the display on my Android phone would look the same as the display on the LCD. This however may not be possible. I could do something like make 3 fields on the Android then just send that voltage data to the 3rd field.

The main thing I want to accomplish is to use android tablets as touch screens to interface to the world. I've done that and for anyone else who wants to do it. Unless your a good programmer don't go for the eclipse java method. go for googles app inventor. Its not powerful but you dont need much power for this kind of thing and it actually teaches you java with it's graphic user interface so when you do need more power you can try eclipse. 7" Android tablets are under $200. To make a touch screen that size from scratch is more expensive and very slow.

But what I would like to do is port all the programs I have written for 2lines X 16 character LCDs to display on Android. I've ported them from standard 2X16 4 bit to 2X16 IC2 just by changing 3 lines, the header, declarations, and initialization. It would be nice if Android porting could be as easy.

Ed Armstrong

A guy from another forum gave me a very simple solution. I like this stuff the best because it really helps me understand. I still don't understand it exactly, but I will study up. It's all in this definition doesn't even need a header. I wrote this code, inserted this definition and it worked!!! it transmits to the LCD and the bluetooth!! The exact placement of characters is still uncontrollable, but I can work around that I think.

the magical definition:
#define println(mystr) { lcd.print(mystr); Serial.println(mystr); }

here is how to use it
Code: [Select]


#include <LiquidCrystal.h> //front panel LCD display driver

#define println(mystr) { lcd.print(mystr); Serial.println(mystr); }
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); //defines the digital pins used on the LCD panel

void setup() // sets all default values at power up or after reset prior to running any test
{
Serial.begin(9600); // for bluetooth
lcd.begin(16, 2); // start the LCD library
lcd.setCursor(0,0); // set to top line first character
println("Hello world");
   delay(1000); 
}


void loop() {
lcd.setCursor(0,0);
println("This is the loop");   // set the LED on
delay(1000);   

lcd.setCursor(0,0);
println("Hi Again all");    // set the LED off
delay(1000);              // wait for a second
}


Go Up