I am trying to figure out how to use the LiquidCrystal_I2C_Spark library with my SparkFun 16x2 serial LCD (https://www.sparkfun.com/products/10097) and the serial backpack (https://www.sparkfun.com/products/258). I am able to print and adjust the brightness of the screen and print “Hello world!” without any library using the following code:
However, I would much rather use the library to do more complex actions. I’ve tried the sample program included in the library but nothing happens when I flash it to my Photon.
Thanks! This works great. Occasionally the output will get corrupted and I have to unplug/reset the Photon and LCD screen to get it back to normal. I saw another post where this was a problem and they resolved it, I’ll have to look into that. I expanded on your helper functions and added a web based function to print anything to the screen, its pretty cool! Thanks for your help.
int timeDelay = 10;
void setup() {
Particle.function("print",printToScreen);
Serial1.begin(9600);
displayClear();
displayBacklight(100);
printToScreen(" Welcome!");
underlineCursor(false);
blinkingBox(false);
}
void loop() {
}
//================================================
// HELPER FUNCTIONS
//================================================
void underlineCursor(bool val) {
delay(timeDelay);
if (val) {
Serial1.write(0xFE); // Command
Serial1.write(0x0E);
} else {
Serial1.write(0xFE); // Command
Serial1.write(0x0C);
}
}
void blinkingBox(bool val) {
delay(timeDelay);
if (val) {
Serial1.write(0xFE); // Command
Serial1.write(0x0D);
} else {
Serial1.write(0xFE); // Command
Serial1.write(0x0C);
}
}
void displayHome() {
delay(timeDelay);
Serial1.write(0xFE); // Command
Serial1.write(128); // Move cursor to beginning of first line
}
void displayMoveTo(int pos) {
delay(timeDelay);
char posChar;
if (pos>=1 && pos<=16) {
posChar = char(128+pos-1);
} else if (pos>=17 && pos<=32) {
posChar = char(192+pos-17);
} else {
posChar = 128; // on error, go home
}
Serial1.write(0xFE); // Command
Serial1.write(posChar);
}
void displayClear() {
delay(timeDelay);
Serial1.write(0xFE); // Command
Serial1.write(0x01); // Clear screen command
}
void displayBacklight(int brightpercent) {
delay(timeDelay);
int mappedBright = map(brightpercent, 0,100,128,157);
// Make sure range is OK
if (mappedBright >= 128 && mappedBright <= 157) {
Serial1.write(0x7C); // Command
Serial1.write(char(mappedBright));
}
}
int printToScreen(String phrase) {
displayClear();
Serial1.print(phrase);
}
Serial is the USB serial port, while Serial1 is the the TX/RX pins which is where you would normally hook-up the display. There is also a Serial2 available on two other pins if you include a header for it.