#include <NewSoftSerial.h>
// Graphic Serial LCD 128x64
// Arduino Pin 8 (RX) to LCD Pin TX (NOT USED, NOT NECESSARY)
// Arduino Pin 9 (TX) to LCD Pin RX
// Arduino Pin Vin to LCD Vin (Assuming you're powering Arduino externally with 9 VDC)
// Arduino Pin Gnd to LCD Pin Gnd
NewSoftSerial lcdSerialPort(8, 9);
void setup() {
setBaudRate(9600);
clearScreen();
// Set a dim display
setBackgroundBrightness(1);
// Note how by default it prints at upper left
print("hello");
// Note that 0,0 is at lower left side of display
setX(5);
setY(10);
print("world");
// Draw a big X
drawLine(0, 0, 127, 63, 1);
drawLine(127, 0, 0, 63, 1);
// Draw a centered circle
drawCircle(63, 31, 31, 1);
// Draw a box in the center
drawBox(31, 15, 95, 47, 1);
}
void loop()
{
}
void print(char *data){
lcdSerialPort.print(data);
}
void clearScreen(){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x00,BYTE);
}
void demo(){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x04,BYTE);
}
void toggleSplashScreen(){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x13,BYTE);
}
void setBackgroundBrightness(byte value){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x02,BYTE);
lcdSerialPort.print(value,BYTE);
}
void setBaudRate(long value){
// Get the internal reference for this baud rate
char *ref = " ";
if(value == 4800)
ref = "1";
else if(value == 9600)
ref = "2";
else if(value == 19200)
ref = "3";
else if(value == 38400)
ref = "4";
else if(value == 57600)
ref = "5";
else if(value == 115200)
ref = "6";
else
return;
// Since it often rolls back to 115200, try setting it via 115200 first
lcdSerialPort.begin(115200);
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x07,BYTE);
lcdSerialPort.print(ref);
// Now change the serial port to the desired rate, and set it again.
lcdSerialPort.begin(value);
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x07,BYTE);
lcdSerialPort.print(ref);
}
void setX(byte value){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x18,BYTE);
lcdSerialPort.print(value,BYTE);
}
void setY(byte value){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x19,BYTE);
lcdSerialPort.print(value,BYTE);
}
void setPixel(byte state){
lcdSerialPort.print(0x50,BYTE);
lcdSerialPort.print(0x40,BYTE);
lcdSerialPort.print(state,BYTE);
}
void drawLine(byte startX, byte startY, byte endX, byte endY, byte state){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x0C,BYTE);
lcdSerialPort.print(startX,BYTE);
lcdSerialPort.print(startY,BYTE);
lcdSerialPort.print(endX,BYTE);
lcdSerialPort.print(endY,BYTE);
lcdSerialPort.print(state,BYTE);
}
void drawCircle(byte startX, byte startY, byte radius, byte state){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x03,BYTE);
lcdSerialPort.print(startX,BYTE);
lcdSerialPort.print(startY,BYTE);
lcdSerialPort.print(radius,BYTE);
lcdSerialPort.print(state,BYTE);
}
void drawBox(byte topLeftX, byte topLeftY, byte bottomRightX, byte bottomRightY, byte state){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x0F,BYTE);
lcdSerialPort.print(topLeftX,BYTE);
lcdSerialPort.print(topLeftY,BYTE);
lcdSerialPort.print(bottomRightX,BYTE);
lcdSerialPort.print(bottomRightY,BYTE);
lcdSerialPort.print(state,BYTE);
}
void eraseBox(byte topLeftX, byte topLeftY, byte bottomRightX, byte bottomRightY, byte state){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x05,BYTE);
lcdSerialPort.print(topLeftX,BYTE);
lcdSerialPort.print(topLeftY,BYTE);
lcdSerialPort.print(bottomRightX,BYTE);
lcdSerialPort.print(bottomRightY,BYTE);
lcdSerialPort.print(state,BYTE);
}
I see that this is an older post, but is there a chance that the author is still able to respond with the hardware setup that was used?
Exactly which LCD display was this software written for?
Dredging up an old post, but I can’t for the life of me find a solid example of someone using the 128x64 Serial-enabled GLCD with an Arduino. Apparently people are replacing the Sparkfun firmware but until I get an AVR programming solution I’d like to mess around a bit. This code is the closest I can come to plug-and-play code that I can modify and play around with to teach myself how this all works.
I’m using a Duemilanove (serial ports are 0 (RX) and 1 (TX)).
Can someone add some more comments to the code so I know what’s going on? I have the screen connected to the Arduino as described and substituted “NewSoftSerial lcdSerialPort(0, 1);” into the code to account for the location of my serial port. I have the NewSoftSerial library installed into my Arduino/libraries folder.
I’ve tried the NewSoftSerialTest sketch as well and I see nothing after the Sparkfun logo splash.
I’m sure there’s just a stupid mistake that I’m making that can be easily rectified.
FWIW, [this Hello World script does work.](http://forum.sparkfun.com/viewtopic.php?f=14&t=22546)
Making progress. This code:
// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
#include <Servo.h>
#include <NewSoftSerial.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup()
{
Serial.begin(115200);
setBackgroundBrightness(1);
setX(5);
setY(10);
print(“world”);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
NewSoftSerial lcdSerialPort(0, 1);
void clearScreen(){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x00,BYTE);
}
void print(char *data){
lcdSerialPort.print(data);
}
void setBackgroundBrightness(byte value){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x02,BYTE);
lcdSerialPort.print(value,BYTE);
}
void setX(byte value){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x18,BYTE);
lcdSerialPort.print(value,BYTE);
}
void setY(byte value){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x19,BYTE);
lcdSerialPort.print(value,BYTE);
}
void setPixel(byte state){
lcdSerialPort.print(0x50,BYTE);
lcdSerialPort.print(0x40,BYTE);
lcdSerialPort.print(state,BYTE);
}
void drawLine(byte startX, byte startY, byte endX, byte endY, byte state){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x0C,BYTE);
lcdSerialPort.print(startX,BYTE);
lcdSerialPort.print(startY,BYTE);
lcdSerialPort.print(endX,BYTE);
lcdSerialPort.print(endY,BYTE);
lcdSerialPort.print(state,BYTE);
}
void drawCircle(byte startX, byte startY, byte radius, byte state){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x03,BYTE);
lcdSerialPort.print(startX,BYTE);
lcdSerialPort.print(startY,BYTE);
lcdSerialPort.print(radius,BYTE);
lcdSerialPort.print(state,BYTE);
}
void drawBox(byte topLeftX, byte topLeftY, byte bottomRightX, byte bottomRightY, byte state){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x0F,BYTE);
lcdSerialPort.print(topLeftX,BYTE);
lcdSerialPort.print(topLeftY,BYTE);
lcdSerialPort.print(bottomRightX,BYTE);
lcdSerialPort.print(bottomRightY,BYTE);
lcdSerialPort.print(state,BYTE);
}
void eraseBox(byte topLeftX, byte topLeftY, byte bottomRightX, byte bottomRightY, byte state){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x05,BYTE);
lcdSerialPort.print(topLeftX,BYTE);
lcdSerialPort.print(topLeftY,BYTE);
lcdSerialPort.print(bottomRightX,BYTE);
lcdSerialPort.print(bottomRightY,BYTE);
lcdSerialPort.print(state,BYTE);
}
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
Serial.print(val);
Serial.print(“-”);
clearScreen();
delay(15);
}
Semi-works. It prints the following:
http://dl.dropbox.com/u/19953/Arduino/I … 5-2012.jpg
I don’t see any of the NewSoftSerial funcitons working. Thoughts?
Ports 0 and 1 are the ports that Arduino’s built-in Serial library uses. Your code that calls Serial.print(val) works only because you are connected to pins 0 and 1. The problem with that, is that everytime you want to update your code, you have to disconnect the LCD from 0 and 1 or it interferes with the upload from the Arduino IDE.
To get around this, we use NewSoftSerial. It is very similar to the built-in Serial, except that you can use pins other than 0 and 1.
So…
-
Move your NewSoftSerial lcdSerialPort(0, 1) to some other pins like NewSoftSerial lcdSerialPort(8, 9). Move your LCD wires to 8 and 9 also.
-
Replace your Serial.print(val) statement with lcdSerialPort.print(val);
Now you can update your code without disconnecting the LCD. And you can take advantage of the convenience functions that I created (drawBox(), drawCircle(), etc.)
Additionally, you can send debugging output to Serial.print() and you can see them in the Arduino IDE’s serial port monitor.
Note that there’s been a lot of activity in the comments under the product. You might read through them here: http://www.sparkfun.com/products/9351
I got it up and running pretty quick. If memory serves, there was something quirky about the baud rate. Check into that if it doesn’t work.
hopper1068:
Ports 0 and 1 are the ports that Arduino’s built-in Serial library uses. Your code that calls Serial.print(val) works only because you are connected to pins 0 and 1. The problem with that, is that everytime you want to update your code, you have to disconnect the LCD from 0 and 1 or it interferes with the upload from the Arduino IDE.To get around this, we use NewSoftSerial. It is very similar to the built-in Serial, except that you can use pins other than 0 and 1.
Thanks for the explanation here, this makes sense. I had been disconnecting the TX line per a recommendation read on the Sparkfun product comments, now I know how to get around that.
So…
Move your NewSoftSerial lcdSerialPort(0, 1) to some other pins like NewSoftSerial lcdSerialPort(8, 9). Move your LCD wires to 8 and 9 also.
Replace your Serial.print(val) statement with lcdSerialPort.print(val);
Now you can update your code without disconnecting the LCD. And you can take advantage of the convenience functions that I created (drawBox(), drawCircle(), etc.)
Additionally, you can send debugging output to Serial.print() and you can see them in the Arduino IDE’s serial port monitor.
Is there a reason that the clearScreen() in my code doesn’t work?
Note that there’s been a lot of activity in the comments under the product. You might read through them here: http://www.sparkfun.com/products/9351
I got it up and running pretty quick. If memory serves, there was something quirky about the baud rate. Check into that if it doesn’t work.
I’ve been through those comments and the comments on the standalone backpack a few times, most people recommend SummoningDark’s firmware and there’s not a ton of discussion using the screen as-is.
Are you seeing any output? I just now took my original code from this thread, hooked up the LCD as described in the comments of that code, and it worked first try. You should see a big X, circle, box and hello world on the display.Is there a reason that the clearScreen() in my code doesn’t work?
I forgot that you need to power the LCD with 6-7vdc. Powering everything off of the Arduino pins (and therefore the USB line) won’t work (only 5vdc). That’s why I pump 7vdc into the Arduino’s external power, and then power the LCD off of the Raw or Vin pin next to Gnd (which spits out whatever voltage you’re pumping in the external power jack).
The datasheet (http://www.sparkfun.com/datasheets/LCD/ … LCD-v2.pdf) has a lot of good info on this board.
The whole example code is working for me (screen powered by the Arduino). It leaves a cursor at the end of “world.”
http://dl.dropbox.com/u/19953/Arduino/I … 6-1922.jpg
If I wanted to make some part of the screen constantly update with the value read from a pot, how would I do that? Do I need to redraw everything on the screen or can I update a small portion of the screen at once?
Huge thanks for all of the help so far.
You’re doing awesome, Lam. I’m at dinner so can’t test but suspect you could do a gotoXY and print each time without clearing.
Back home. Forgot about the wierd x y coordinate system in this LCD. 0,0 starts at the lower left (vice upper left).
Here’s an example program that is closer to your requirements. Replace my variable “x” with your pot reading code. You should see a number about mid-screen incrementing and resetting to 0.
#include <NewSoftSerial.h>
// Graphic Serial LCD 128x64
// Arduino Pin 8 (RX) to LCD Pin TX (NOT USED, NOT NECESSARY)
// Arduino Pin 9 (TX) to LCD Pin RX
// Arduino Pin Vin to LCD Vin (Assuming you’re powering Arduino externally with 9 VDC)
// Arduino Pin Gnd to LCD Pin Gnd
NewSoftSerial lcdSerialPort(8, 9);
int x = 0;
char buf[6];
void setup() {
setBaudRate(9600);
clearScreen();
// Set a dim display
setBackgroundBrightness(1);
}
void loop()
{
clearScreen();
setX(55);
setY(50);
// Convert integer x into a char array so we can pass it into print(char *);
itoa(x, buf, 10);
print(buf);
x = x+100;
if(x > 10000){
x = 0;
}
delay(200);
}
void print(char *data){
lcdSerialPort.print(data);
}
void clearScreen(){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x00,BYTE);
}
void demo(){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x04,BYTE);
}
void toggleSplashScreen(){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x13,BYTE);
}
void setBackgroundBrightness(byte value){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x02,BYTE);
lcdSerialPort.print(value,BYTE);
}
void setBaudRate(long value){
// Get the internal reference for this baud rate
char *ref = " ";
if(value == 4800)
ref = “1”;
else if(value == 9600)
ref = “2”;
else if(value == 19200)
ref = “3”;
else if(value == 38400)
ref = “4”;
else if(value == 57600)
ref = “5”;
else if(value == 115200)
ref = “6”;
else
return;
// Since it often rolls back to 115200, try setting it via 115200 first
lcdSerialPort.begin(115200);
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x07,BYTE);
lcdSerialPort.print(ref);
// Now change the serial port to the desired rate, and set it again.
lcdSerialPort.begin(value);
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x07,BYTE);
lcdSerialPort.print(ref);
}
void setX(byte value){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x18,BYTE);
lcdSerialPort.print(value,BYTE);
}
void setY(byte value){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x19,BYTE);
lcdSerialPort.print(value,BYTE);
}
void setPixel(byte state){
lcdSerialPort.print(0x50,BYTE);
lcdSerialPort.print(0x40,BYTE);
lcdSerialPort.print(state,BYTE);
}
void drawLine(byte startX, byte startY, byte endX, byte endY, byte state){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x0C,BYTE);
lcdSerialPort.print(startX,BYTE);
lcdSerialPort.print(startY,BYTE);
lcdSerialPort.print(endX,BYTE);
lcdSerialPort.print(endY,BYTE);
lcdSerialPort.print(state,BYTE);
}
void drawCircle(byte startX, byte startY, byte radius, byte state){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x03,BYTE);
lcdSerialPort.print(startX,BYTE);
lcdSerialPort.print(startY,BYTE);
lcdSerialPort.print(radius,BYTE);
lcdSerialPort.print(state,BYTE);
}
void drawBox(byte topLeftX, byte topLeftY, byte bottomRightX, byte bottomRightY, byte state){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x0F,BYTE);
lcdSerialPort.print(topLeftX,BYTE);
lcdSerialPort.print(topLeftY,BYTE);
lcdSerialPort.print(bottomRightX,BYTE);
lcdSerialPort.print(bottomRightY,BYTE);
lcdSerialPort.print(state,BYTE);
}
void eraseBox(byte topLeftX, byte topLeftY, byte bottomRightX, byte bottomRightY, byte state){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x05,BYTE);
lcdSerialPort.print(topLeftX,BYTE);
lcdSerialPort.print(topLeftY,BYTE);
lcdSerialPort.print(bottomRightX,BYTE);
lcdSerialPort.print(bottomRightY,BYTE);
lcdSerialPort.print(state,BYTE);
}
Any luck, Laminar?
Have a look here: viewtopic.php?f=8&t=28317. Its a new library meant to go with this firmware: http://sourceforge.net/projects/serialglcd/
It will make the display work as it should, and with added functions…
Very nice, Sebasto!
Makes it very easy to work with.
Well done!
hopper1068:
Very nice, Sebasto!Makes it very easy to work with.
Well done!
Thanks, i moved the library to sourceforge. I have also added a few more functions.
You should add your library to the playground page:
hopper1068:
You should add your library to the playground page:
Yes, that was my plan and here it is:
Awesome.
Now we just need more libraries for components like these.
I can’t help but think that Sparkfun would give you free components in exchange for well written, tested libs like yours.
Can’t hurt to ask them… especially if you’re looking for a specific component. Worst case, you might get a discount.
Here’s your justification: people like me are more apt to buy a Sparkfun component if it’s got a library to go with it.
hopper1068:
Here’s your justification: people like me are more apt to buy a Sparkfun component if it’s got a library to go with it.
Yes, I totally agree with you there. The main reason I bought this is because I wanted to start with something easy, a library like this would have been a great help. Instead i had to figure out a lot myself and write that library to get started. Other beginners might not be as patient, and might simply give up on the whole deal if they end up in the same position as I was.
But because of this i feel that i have learned a lot, which was the whole point of this purchase, i don’t really need the display
hopper1068:
Any luck, Laminar?
I just wanted to let you know that I’m still keeping up on this thread, but I haven’t gotten a chance to try anything new yet. I was on vacation last week and I’m overseas for work now. I hope to get back on this once I’m home.