I'm super new to this, so please forgive me if this isn't extremely clear.
I have a basic HD44780 16x2 parallel LCD hooked up and functioning. Using the LiquidCrystal library and the serial input functions, I can pass strings of text to the display from my computer using a PHP script. The text displays on the LCD as expected.
My next logical step is to control cursor position, clear screen, etc. with my PHP script. From my understanding I should be able to send hex commands to the LCD to accomplish this. Unfortunately, this is where I'm having a difficult time. The hex displays as a string or as odd characters.
I feel like I'm missing something obvious. Do I have to use a serial LCD if I want to send commands through an external PHP script? Or am I thinking about this all wrong? A nudge in the right direction would be greatly appreciated.
The easiest way would be to have the Arduino interpret the data it is being sent, and react accordingly.
For example, you could send "T(Show this on the LCD)", and the Arduino would see that the first character is a T (for text), and show everything between the ( and ) on the LCD.
You could send "P(1, 3)", and the Arduino would see that the first character is a P (for position), and parse the row and column values, and set the cursor correctly.
You could come up with other "commands" for the Arduino (C, for clear, etc.).
Thanks for pointing me in the right direction. I'll give this a try. I was hoping to avoid as much Arduino/C language as possible, but this will be a fun learning opportunity.
Here's the code I ended up using to solve my issue. Hopefully others will find it useful. Using a PHP script I can prefix my text string with certain characters that essentially control the cursor position. In this case, a string that starts with "!" sets the cursor on the first line. "@" sets the cursor on the second line, and "^" clears the screen.
#include <LiquidCrystal.h>
#include <WString.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(2, 3, 4, 9, 10, 11, 12);
void setup(){
analogWrite(5, 100); // Set LCD contrast level (0-255)
lcd.begin(16, 2); // Set the LCD's number of rows and columns
Serial.begin(9600); // Initialize communication with serial(USB) port.
lcd.print("Welcome."); // Print welcome message to LCD.
}
int bufferArray[250];
int output = 0;
int i = 0;
void loop() {
int count = Serial.available();
if (Serial.available() > -1) {
delay(1000);
for (i=0; i<count; i++) {
bufferArray[i] = Serial.read();
output = 1;
}
}
if (output != 0) { // if new bytes have been recieved
int position = 0;
if (bufferArray[0] == '!') { // Print on first line if message begins with !
lcd.clear();
lcd.setCursor(0,0);
position = 1;
} else if (bufferArray[0] == '@') { // Print on second line if message begins with @
lcd.setCursor(0,1);
position = 1;
} else if (bufferArray[0] == '^') { // Clears the display
lcd.clear();
lcd.setCursor(0,0);
position = 1;
} else {
lcd.clear();
lcd.setCursor(0,0);
}
int j;
for (j = position; j < count; j++) {
lcd.write(bufferArray[j]);
}
output = 0;
memset(bufferArray, 0, count);
count = 0;
}
}