I'm having a small issue when printing to my LCD.
I may be overlooking something very simple.
Basically, i can type commands into the IDE serial monitor and the text pops up on my LCD fine as it's writes whatever is on the 'Serial.read'. However, when i press a physical button that prints to serial, the LCD does not print anything. I want to see my button press basically on both the serial monitor and LCD.
Heres my code -
//RS232 TA TOILET PROGRAM
#include <LiquidCrystal.h> //lcd library
const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
int button_h_fcm = 11; //sets button as digital pin #2
int button_d1 = 12; // sets button as digital pin #3
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() { //starts Comms, lcd and button pinouts.
pinMode(button_h_fcm, INPUT); //sets pintype to input with an internal pull-up resistor
pinMode(button_d1, INPUT); //sets pintype to input with an internal pull-up resistor
lcd.begin(16, 2); //starts lcd and screen sizing
Serial.begin(9600); //starts comms and sets baudrate on serial monitor/usb
Serial1.begin(9600, SERIAL_7O2); //starts comms and sets baudrate on rs485 bus
}
void loop() {
if (Serial.available()) {
delay(200);
lcd.clear();
lcd.print("TOILET: ");
}
while (Serial.available() > 0) {
lcd.write(Serial.read());
}
//prints incoming data to serial monitor
if (Serial1.available() > 0) { //Check for available incoming data on Serial
String rx = Serial1.readString(); //stores incoming data in buffer
Serial.print("TOILET: "); //prints device name before incoming data to serial monitor
Serial.print(rx); //prints incoming data string to serial monitor
}
//prints rs485 commands to both serial monitor and rs485 bus for a visual reference and data transfer
if (digitalRead(button_h_fcm) == HIGH) { //reads button state
Serial.println("H FCM"); //prints outgoing data to serial monitor
Serial1.println("H FCM"); //prints outgoing data to rs485 bus, should return *MICBAC*
delay(1000); //1 second delay to stop mechanical button bounce
}
if (digitalRead(button_d1) == HIGH) { //reads button state
Serial.println("D1 startaddress stopaddress"); //print outgoing data to serial monitor
Serial1.println("D1 startaddress stopaddress"); //prints outgoing data to rs485 bus, should Return *"addreses"*
delay(1000); //1 second delay to stop mechanical button bounce
}
}
When you detect a button press you print to the Serial monitor but that is an output to the Serial monitor, not an input to it. If you want to print a message to the LCD when your press a button then you must do an explicit lcd.print()
Change INPUT to INPUT_PULLUP as the comment says then pressed is LOW.
You haven't denounced so results will be erratic but at least the logic will be correct.
Got it working now with this new code below. Although one small issue i've noticed is that if i send a message too fast in the serial monitor, i may only get part of my message.
For example, I may type "HELLO", "H" gets transmitted and then the rest of the "ELLO" comes after my set delay.
//RS232 TA TOILET PROGRAM
#include <LiquidCrystal.h> //lcd library
const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
int button_h_fcm = 11; //sets button as digital pin #2
int button_d1 = 12; // sets button as digital pin #3
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() { //starts Comms, lcd and button pinouts.
pinMode(button_h_fcm, INPUT); //sets pintype to input with an internal pull-up resistor
pinMode(button_d1, INPUT); //sets pintype to input with an internal pull-up resistor
lcd.begin(16, 2); //starts lcd and screen sizing
Serial.begin(9600); //starts comms and sets baudrate on serial monitor/usb
Serial1.begin(9600, SERIAL_7O2); //starts comms and sets baudrate on rs485 bus
}
void loop() {
if (Serial.available()) {
delay(5000);
lcd.clear();
lcd.print("TOILET: ");
}
while (Serial.available() > 0) {
lcd.write(Serial.read());
}
//prints incoming data to serial monitor
if (Serial1.available() > 0) { //Check for available incoming data on Serial
String rx = Serial1.readString(); //stores incoming data in buffer
Serial.print("TOILET: "); //prints device name before incoming data to serial monitor
Serial.print(rx); //prints incoming data string to serial monitor
}
//prints rs485 commands to both serial monitor and rs485 bus for a visual reference and data transfer
if (digitalRead(button_h_fcm) == HIGH) { //reads button state
Serial.println("H FCM"); //prints outgoing data to serial monitor
Serial1.println("H FCM"); //prints outgoing data to rs485 bus, should return *MICBAC*
lcd.clear();
lcd.write("COMMAND: H FCM");
delay(1000); //1 second delay to stop mechanical button bounce
}
if (digitalRead(button_d1) == HIGH) { //reads button state
Serial.println("D1 startaddress stopaddress"); //print outgoing data to serial monitor
Serial1.println("D1 startaddress stopaddress"); //prints outgoing data to rs485 bus, should Return *"addreses"*
lcd.clear();
lcd.print("COMMAND: D1 startaddress stopaddress");
delay(1000); //1 second delay to stop mechanical button bounce
}
}
If I understand the code correctly, it is this part of the code that does not behave as expected.
If so, I suggest that you read (and learn from ) Robin's updated Serial Input Basics; example 2 or 3 for starters.
The problem is that your code executes faster than the data comes in. So the E might still be in transmission and not completely received. If you collect the data first (as demonstrated in above link), you will not have the problem.