Yesterday I was experiencing hot moments with my new LCD display, which working fine and then suddenly stopped responding. Later I found why: I sent the command for setting the intensity of brightness, then clear display, set cursor position and print some text. These sequence of consecutive commands (between which there was no delay) unintentionally changed baud rate to unknown value. After resetting (CTRL/R) LCD UART to the default 9600 Bd the LCD works normally until I repeat the "wrong" sequence. Finally I must insert small delay after brightness command, so LCD keeps the baud rate.
#include <SoftwareSerial.h>
#define LCD_BRIGHTNESS_H_LIMIT 157
#define LCD_BRIGHTNESS_L_LIMIT 128
byte LCD_brightness = LCD_BRIGHTNESS_H_LIMIT; // LCD brightness: max
#define CLR_DISP 0b00000001 //Clear display
#define CUR_HOME 0b00000010 //Move cursor home and clear screen memory
#define CUR_RIGHT 0b00010100 //Move cursor one to right
#define CUR_LEFT 0b00010000 //Move cursor one to left
#define SCROLL_RIGHT 0b00011100 //Scroll entire screen right one space
#define SCROLL_LEFT 0b00011000 //Scroll entire screen left one space
#define DISP_ON 0b00001100 //Turn visible LCD on
#define DISP_OFF 0b00001000 //Turn visible LCD off
#define UNDERLINE_ON 0b00001110 //Turn on underlined cursor
#define UNDERLINE_OFF 0b00001100 //Turn off underlined cursor
#define BLINKCUR_ON 0b00001101 //Turn on blinking box cursor
#define BLINKCUR_OFF 0b00001100 //Turn off blinking box cursor
#define DUALCUR_ON 0b00001111 //Turn on blinking box and underline cursor
#define DUALCUR_OFF 0b00001100 //Turn off blinking box and underine cursor
#define LCD_COMMAND 0b11111110 //next byte will be command
#define SET_CURSOR 0b10000000 //SET_CURSOR or XPOS : Sets cursor position to XPOS
#define SET_BRIGHTNESS 0b01111100 //next byte will be brightness 128 ~ 157
SoftwareSerial SerLCD(19,18); // RX2 D19, TX2 D18 - connect LCD RX with pin D18 on Arduino Mega
void printLCD(String lcdmsgtxt, byte cursorpos){
if (cursorpos == 0){
SerLCD.write(LCD_COMMAND);
SerLCD.write(CLR_DISP);
};
if (cursorpos <= 103){
SerLCD.write(LCD_COMMAND);
SerLCD.write(SET_CURSOR+cursorpos);
};
SerLCD.print(lcdmsgtxt);
};
void setbrightnessLCD(){
if (LCD_brightness < LCD_BRIGHTNESS_L_LIMIT){
LCD_brightness = LCD_BRIGHTNESS_L_LIMIT;
};
if (LCD_brightness > LCD_BRIGHTNESS_H_LIMIT){
LCD_brightness = LCD_BRIGHTNESS_H_LIMIT;
};
SerLCD.write(SET_BRIGHTNESS); //NOTE THE DIFFERENT COMMAND FLAG = 124 dec
SerLCD.write(LCD_brightness); //any value between 128 and 157 or 0x80 and 0x9D
delay(100); //delay is necessary!!!
};
void initLCD(){
SerLCD.begin(9600); // set up serial port for 9600 baud
//how to reset to 9600 bd: disconnect LCD VCC, reset Arduino and connect LCD VCC back to power
for(int iii = 0; iii < 10; iii++){
SerLCD.write(18); // CTRL+R
delay(250);
};
delay(1500);
SerLCD.write(LCD_COMMAND);
SerLCD.write(DISP_ON);
setbrightnessLCD();
printLCD("EtherMega",0);
printLCD("Arduino Mega 2560 R3",64);
};
void setup()
{
initLCD();
};
void loop()
{
....
};
Is this bug or feature? One special command byte following more than one byte as many parameters?