Guys,
I'm making a device that will read from serial and display in LCD. Now, I can read... but the backlight, i want to make it a bit different...
my requirements is:
normally backlight will be 10% PWM.
when something arrives in serial, the backlight will go to 80% PWM
if nothing comes to serial in next 5 mins, it will go backto 10%.
consider it like the PC monitor power saver... I cant make it at all... delay() will not work... here is a piece of code that I'm working with:
// include the library code:
#include <LiquidCrystal.h>
// The necessary constants
const int baudRate = 9600;
const int LCD_col = 16;
const int LCD_row = 2;
const int LCD_BL_pin = 9; //Arduino pin 9 for bakclight control via PWM
// The special commands
const String LCD_clear = "AT+CLS"; //The CLS command
const String LCD_newLine = "AT+NL"; //This is \n (new line) Currently ONLY for 2 line display
const String LCD_setCursor = "AT+CR="; //Command for setting cursor. Trailing will be Col and Row seperated by , [e.g. 02,01]
// The necessary variables
String sub_str = "";
String str_rx = "";
int interval = 5000;
unsigned long current_time_val;
unsigned long prev_time_val = 0;
int row = 0;
int col = 0;
// initialize the library with the numbers of the interface pins
//LiquidCrystal lcd(RS, EN, D4, D5, D6, D7);
LiquidCrystal lcd(14, 15, 16, 17, 18, 19);
void setup(){
pinMode(LCD_BL_pin, OUTPUT);
analogWrite(LCD_BL_pin, 80); //default. change later
// set up the LCD's number of columns and rows:
lcd.begin(LCD_col, LCD_row);
// initialize the serial communications:
Serial.begin(baudRate);
//The splash screen. lcd.clear();
lcd.setCursor(0,0);
lcd.write("An R&D From");
lcd.setCursor(0,1);
lcd.write("banglardamal.org");
smartDelay(1500);
lcd.clear();
Serial.println("Splash Screen Printed");
//Config the display
if (str_rx == "conf t"){
str_rx = "";
configMode();
}
current_time_val = millis();
}
void loop()
{
str_rx = "";
sub_str = "";
// when characters arrive over the serial port...
if (Serial.available() > 0) {
//Read until the newline \n comes from serial
str_rx = String(Serial.readStringUntil('\n'));
analogWrite(LCD_BL_pin, 80);
//Set parameters like line number, and then print data.
if (str_rx == LCD_clear){ //The CLS.
lcd.clear();
str_rx = "";
}
if (str_rx == LCD_newLine){ //currently only for 2 line display
lcd.setCursor(0,1);
str_rx = "";
}
if (str_rx.startsWith(LCD_setCursor)){ //Set cursor to Col, Row
sub_str = str_rx.substring(6,8); //Skip the AT+CR= And Col is two char. (01)
col = sub_str.toInt();
sub_str = str_rx.substring(9,11); //Row is after the , and till \n, two char (01)
row = sub_str.toInt();
lcd.setCursor(col, row);
str_rx = "";
}
else { // display each character to the LCD
lcd.print(str_rx);
}
}
if ((current_time_val - prev_time_val) < interval) {
prev_time_val = current_time_val;
analogWrite(LCD_BL_pin, 10);
}
}
// This custom version of delay() ensures that the gps object is being "fed".
static void smartDelay(unsigned long ms){
unsigned long start = millis();
do {
//while (Serial.available()){
while (Serial.available() > 0){
str_rx = String(Serial.readStringUntil('\n'));
}
}
while (millis() - start < ms);
}
// Config mode function
void configMode(){
int config_mode = 1;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Config Mode");
Serial.println("Config mode");
while (str_rx != "exit") {
if (Serial.available() > 0) {
//Read until the newline \n comes from serial
str_rx = String(Serial.readStringUntil('\n'));
}
if (str_rx.startsWith("bright=")){ //Set cursor to Col, Row
sub_str = str_rx.substring(7); //Skip the AT+CR= And Col is two char. (01)
int blBright = sub_str.toInt();
Serial.println(blBright);
analogWrite(9, blBright);
str_rx = "";
}
}
soft_reset();
}
void soft_reset()
{
Serial.println("Reboot in 3 seconds...");
delay(3000);
asm volatile (" jmp 0 ");
}