guys,
need help...
I'm sending some String Text to Serial from one Arduino...
mostly this is via:
Serial.println("Hellow World");
Serial.println("AT+CLS"); //This is a command for clearing screen of LCD via serial port
and so on.
Now, the above one was serial transmitter and the serial receiver will receive them via the code below:
/*
Serial config: 8N1, default baud is 9600 and user configurable.
* EEPROM ADDRESSES:
* LCD Backlight = 0-1
* LCD type = 2-3
* LCD BAUD = 4-5
*/
// include the library code:
#include <LiquidCrystal.h>
#include <EEPROM.h>
// The necessary constants
const int default_baudRate = 9600; //default
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]
const String LCD_conf = "AT+CONF"; //Command to enter config mode from anywhere.
const String LCD_stat = "AT+STAT"; //Command to see the current running config.
// The necessary variables
String sub_str = "";
String str_rx = "";
unsigned long interval = 10000; //in ms.
unsigned long current_time_val;
unsigned long prev_time_val = 0;
int row = 0;
int col = 0;
int backlight;
int LCD_type; //(1=16x2, 2=16x4, 3=20x2, 4=20x4)
int LCD_baud;
int LCD_col;
int LCD_row;
// 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() {
// Read the parameters from EEPROM
backlight = EEPROMReadInt(0);
LCD_type = EEPROMReadInt(2);
LCD_baud = EEPROMReadInt(4);
// Now config the system as per that:
backlight = ((255*backlight)/100);
analogWrite(LCD_BL_pin, backlight);
switch (LCD_type) {
case 1: // 16x2 LCD
LCD_col = 16;
LCD_row = 2;
break;
case 2: // 16x4 LCD
LCD_col = 16;
LCD_row = 4;
break;
case 3: // 20x2 LCD
LCD_col = 20;
LCD_row = 2;
break;
case 4: // 20x4 LCD
LCD_col = 20;
LCD_row = 4;
break;
default :
LCD_col = 16;
LCD_row = 2;
break;
}
// set up the LCD's number of columns and rows:
lcd.begin(LCD_col, LCD_row);
// initialize the serial communications with default baud of 9600 for config mode:
Serial.begin(default_baudRate);
//The splash screen. Branding indeed...
lcd.clear();
lcd.setCursor(0,0);
lcd.write("Serial LCD");
lcd.setCursor(0,1);
lcd.write("Firmware 1.1");
smartDelay(1500);
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 == LCD_conf) || (str_rx == "conf t")) {
str_rx = "";
configMode();
}
//config complete/no config requires, move to operational config.
Serial.end();
Serial.begin(LCD_baud);
}
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, backlight);
prev_time_val = current_time_val; //Reset the timeout for one more full cycle
//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 = "";
}
if (str_rx == LCD_conf) { // Conf mode force entry
str_rx = "";
configMode();
}
else { // display each character to the LCD
lcd.print(str_rx);
}
}
//The timeout for BackLight dimming
LCD_BL_dim();
}
// 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);
}
// Function for LCD Backlight dimming
void LCD_BL_dim() {
current_time_val = millis();
if ((current_time_val - prev_time_val) > interval) {
prev_time_val = current_time_val;
analogWrite(LCD_BL_pin, 5);
}
}
And for simplicity, i haven't added the extra functions here... but functions works fine.
Now, when I use the serial monitor and type something to my arduino+LCD, it works "exactly" fine. Then when I send a string via the serial of another arduino, i get nothing or garbage... even all scattered and not the way it was supposed to be... (serial monitor works fully ok).
any ideas where i'm missing something??