I am trying to control 2 devices with my arduino using RS485 communication. Basically switch on device 1 for 5 seconds when I send 'g' to the master. And switch on device 2 for e3 seconds when I send 'h' to the master.
I have a similar setup working with delays in the code I cannot use delays in the final version.
Here is the code I am using -
Master -
#include<SoftwareSerial.h>
#define NL 10
#define CR 13
SoftwareSerial Serial1(10,11); //RX, TX
String inputString = ""; // a String to hold incoming data
bool stringComplete = false; // whether the string is complete
char prevChar = (char) 0;
char inChar = (char) 0;
void setup() {
// put your setup code here, to run once:
Serial1.begin(9600);
Serial.begin(9600);
pinMode(8, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
char getdata = 'm';
digitalWrite(8, LOW);
if (stringComplete) {
Serial.println(inputString);
inputString.trim();
//Serial.println("abc");
if (inputString.length() == 1 && inputString.charAt(0) == 'g') {
Serial.println("Starting laser 1.");
digitalWrite(8, HIGH);
Serial1.print('1');
//Serial1.print('h');
Serial1.print(CR);
Serial1.print(NL);
Serial1.flush();
digitalWrite(8, LOW);
}
else if (inputString.length() == 1 && inputString.charAt(0) == 'h') {
Serial.println("Starting laser 2.");
digitalWrite(8, HIGH);
Serial1.print('2');
//Serial1.print('h');
Serial1.print(CR);
Serial1.print(NL);
Serial1.flush();
digitalWrite(8, LOW);
}
inputString = "";
stringComplete = false;
}
if (Serial1.available()) {
while(Serial1.available() && getdata != '\n') {
getdata = Serial1.read();
Serial.print(getdata);
}
Serial.println("");
}
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
prevChar = inChar;
inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
//Serial.println(inputString);
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n' && prevChar == '\r') {
stringComplete = true;
}
}
}
Slave -
#include <TimerOne.h>
bool isLightOn = false;
int counter = 0;
int address = 2;
String inputString = ""; // a String to hold incoming data
bool stringComplete = false; // whether the string is complete
char prevChar = (char) 0;
char inChar = (char) 0;
void timerISR() {
if (isLightOn) {
counter = counter+1;
if (counter >= 20) {
counter = 0;
isLightOn = false;
digitalWrite(5, LOW);
digitalWrite(13, LOW);
}
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Timer1.initialize(100000);
Timer1.attachInterrupt(timerISR);
pinMode(5, OUTPUT);
pinMode(13, OUTPUT);
pinMode(8, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
//digitalWrite(13, LOW);
digitalWrite(8, LOW);
if (stringComplete) {
inputString.trim();
//if (inputString.length() == 1 && inputString.toInt() == address) {
isLightOn = true;
digitalWrite(5, HIGH);
digitalWrite(13, HIGH);
//}
}
while (Serial.available()) {
// get the new byte:
prevChar = inChar;
inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n' && prevChar == '\r') {
stringComplete = true;
}
}
}
/*
void serialEvent() {
while (Serial.available()) {
// get the new byte:
prevChar = inChar;
inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n' && prevChar == '\r') {
stringComplete = true;
}
}
}
*/
My connections are something like this -
- Master - Uno,
- Slave 1 - Uno,
- Slave 2 - Nano

Serial1.println("1"); sends "1\r\n". no need to print the characters separately.inputString = Serial.readStringUnil('\n');reads all the input from Serial Monitor. but if you send only one character, why do you read into String?