I just got a Spark Fun serial enable LCD for use with my power monitor project. The problem is that if I just leave the LCD screen plugged in and I upload new code, or I just hit the reset button the screen just dies out on me. I'm running the LCD over a soft serial connection. If after I've performed a reset I pull the ground then plug it back in I'm in business, which makes me think it may be something in my setup loop but I'm not finding what it could be. Anyone have any ideas? The code I'm using is found below:
#include <SoftwareSerial.h>
//setup my pins
#define rxPin 5
#define txPin 4
#define Relay 7
//initialize my interrupt
volatile int state = LOW;
//initialize my software serial port.
SoftwareSerial LCD = SoftwareSerial(rxPin, txPin);
byte pinState = 0;
void setup()
{
//define my pinmodes
pinMode(Relay, OUTPUT);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
//attach my interupt
attachInterrupt(0, control, CHANGE);
// set the data rate for the SoftwareSerial port
LCD.begin(9600);
delay(100);
//turn backlight on
backlightOn();
//clear the screen
clearLCD();
}
void loop()
{
digitalWrite(Relay, state);
selectLineOne();
LCD.print("Relay status: ");
LCD.print(state);
delay(500);
}
void control()
{
state = !state;
}
void clearLCD(){
LCD.print(0xFE, BYTE); //command flag
LCD.print(0x01, BYTE); //clear command.
}
void backlightOn(){ //turns on the backlight
LCD.print(0x7C, BYTE); //command flag for backlight stuff
LCD.print(157, BYTE); //light level.
}
void backlightOff(){ //turns off the backlight
LCD.print(0x7C, BYTE); //command flag for backlight stuff
LCD.print(128, BYTE); //light level for off.
}
void backlight50(){ //sets the backlight at 50% brightness
LCD.print(0x7C, BYTE); //command flag for backlight stuff
LCD.print(143, BYTE); //light level for off.
}
void serCommand(){ //a general function to call the command flag for issuing all other commands
LCD.print(0xFE, BYTE);
}
//SerialLCD Functions
void selectLineOne(){ //puts the cursor at line 0 char 0.
LCD.print(0xFE, BYTE); //command flag
LCD.print(128, BYTE); //position
}
void selectLineTwo(){ //puts the cursor at line 2 char 0.
LCD.print(0xFE, BYTE); //command flag
LCD.print(192, BYTE); //position
}
The SoftwareSerial library is obsolete. You should be using the NewSoftSerial library. I have a sparkfun serial LCD and I don't have these issues using NewSoftSerial.
thanks for your reply. I am already using NewSoftSerial, as written above. I tried it again after rebooting, rewiring, crossing fingers and doing a little rain dance and now it works like a charm. I am not sure what I did wrong though
And I found that your screen will go dark and freeze with something like:
LCD.print(0x7C, BYTE); //command flag for backlight stuff
LCD.print(backlight_level, BYTE); // Sets the backlight level
LCDmessage("LCD bcklt level is:",backlight_level); // Prints current backlight level
Printing right after adjusting backlight is something the LCD backpack doesn't like.
This minor delay will solve the problem:
LCD.print(0x7C, BYTE); //command flag for backlight stuff
LCD.print(backlight_level, BYTE);
delay(5); // Prevents the LCD from freezing
LCDmessage("LCD bcklt level is:",backlight_level);
Bingo! Thanks a million! Just got one of these displays (V2.5) and this issue adjusting the backlight was driving me crazy. Otherwise the thing seems to work well and I had it jumping through most of the hoops. I was able to get by with a delay as short as 2mS, but 1mS was not enough. But I'm going with 5mS as in your example, figuring that'll give a little added safety margin.
Not sure whether if this is a bug or a feature but it definitely should be documented!
SoftwareSerial and NewSoftSerial seem to work identically for me, at least as far as my little test (Arduino Uno, IDE ver 0022).
SoftwareSerial and NewSoftSerial seem to work identically for me, at least as far as my little test (Arduino Uno, IDE ver 0022).
NewSoftSerial has advantages, though. It supports much higher speeds (up to 57600) and has an available() method to report whether there is data to read.
That's what I gather. I'm new to Arduinos (and loving it!), but saw a lot of references to NewSoftSerial so I downloaded it to try it out. Just taking baby steps at this point, so probably not doing anything that stretches the capabilities of SoftwareSerial. My little test sketch is 2364 bytes with SoftwareSerial and 4068 bytes with NewSoftSerial. SoftwareSerial does have the available() method as well.
What I haven't seen is an "official" status of SoftwareSerial. Is it still being actively developed and maintained? Maybe it's been abandoned given that NewSoftSerial seems to gathered a fair amount of steam. If so, that might be good reason to avoid it as well.