//by Nissanka MD SLCOTM
/*instructions:
Assign different values for This_board_no for diffarant boards and uploa
the codes for each board.
connect 3 or more boards in ring config:
pin 10(Rx) of (previous)board 1 to pin 13(Tx) of (next) board 2
pin 11(Tx) of (previous)board 1 to pin 12(Rx) of (next)board 2
Pin 10(Rx) of (previous)board 2 to pin 13(Tx) of (next)bord 3
pin 11(Tx) of (previous)board 2 to pin 12(Rx) of (next)board 3 and so on.
Finally Pin 10(Rx) of last board to pin 13(Tx) of fisrst bord (1)
pin 11(Tx) of last board to pin 12(Rx) of fisrst bord (1) to complete the ring.
previous had bug/fault of looping continuously if wrong board included in the messege
as every board receives it, will send it to next board as it is not for that board.
And t will forward endlessly since no board to stop and accept it.
it is corrected in here by introducing senders address)
*/
#include <SoftwareSerial.h>
SoftwareSerial LHSerial (10, 11); // incomming serial messeges
SoftwareSerial RHSerial (12, 13);// outgoing serial messages
String incom_Msg;
String foward_Msg;
String This_board_no = "BORDA1";//Address of this board
String Creator; // address of message creator
String Category; // first code or segment of msg
String Number_str; // second code or segment of msg
String Action; // Third code or segment of msg
String Board_No;// Fourth code or segment of msg
int No_Of_LEDs = 5;
int first_Space_position;
int second_Space_position;
int third_Space_position;
int foruth_Space_position;
int Number; // Device Number
bool State; // LED ON OFF State
bool Action_Error = true; // Valid Action
bool valid_Number = false; // Valid LED Number
bool Serial_available = false;
bool LH_Serial_Available = false;
void setup() {
Serial.begin(9600);
LHSerial.begin(9600);
RHSerial.begin(9600);
Serial.println("Enter the command");
for(int i= 2; i< 1 + No_Of_LEDs; i++ )
{
pinMode(i, OUTPUT);
digitalWrite(i, HIGH);
delay(100);
digitalWrite(i, LOW);
}
}
void loop() {
Serial_available = (Serial.available() > 0);
LH_Serial_Available = (LHSerial.available() > 0);
if(Serial_available || LH_Serial_Available)
{
if(Serial_available)
{
incom_Msg = Serial.readString();
foward_Msg = This_board_no + " " + incom_Msg; // insert the initiators address
incom_Msg = foward_Msg;
}
else if (LH_Serial_Available)
{
incom_Msg = LHSerial.readString();
foward_Msg = incom_Msg;
}
/*
delay(50);
Serial.print("Serial Message Size : ");
Serial.println(Serial.available());
*/
//
/*
Serial.print("Incomming Message : ");
Serial.println(incom_Msg);
Serial.print("Incomming Message Size : ");
Serial.println(incom_Msg.length());
*/
/*
incom_Msg.trim();
Serial.print("Trimmed Message : ");
Serial.println(incom_Msg);
Serial.print("Trimmed Message Size : ");
Serial.println(incom_Msg.length());
*/
if(incom_Msg.indexOf("\n") >= 0)
{
//Serial.print("Newline Character found at position : ");
//Serial.println(incom_Msg.indexOf('\n'));
incom_Msg.replace("\n", " ");
//Serial.println("Newline Character removed");
}
if(incom_Msg.indexOf("\r") >= 0)
{
//Serial.print("Carriage return Character found at position : ");
//Serial.println(incom_Msg.indexOf("\r"));
incom_Msg.replace("\r", " ");
//Serial.println("Carriage return Character removed");
}
/*
Serial.print("modified incomming Message (CR and NL removed) : ");
Serial.println(incom_Msg);
Serial.print("lenght of modified Message : ");
Serial.println(incom_Msg.length());
*/
while (incom_Msg.indexOf(" ") >= 0) // if double spaces present
{
incom_Msg.replace(" ", " "); // double space replaced with single space
}
incom_Msg.trim(); // to remove leading space
/*
Serial.print("Middle Duplicate Spaces Trimmed Message : ");
Serial.println(incom_Msg);
Serial.print("Middle Duplicate Spaces Trimmed Message Size : ");
Serial.println(incom_Msg.length());
*/
incom_Msg.toUpperCase();
/*
Serial.print("Message Converted to All Caps : ");
Serial.println(incom_Msg);
*/
second_Space_position = -1;
third_Space_position = -1;
foruth_Space_position = -1;
// Seperate the Codes
first_Space_position = incom_Msg.indexOf(" ");
if(first_Space_position > 0) second_Space_position = incom_Msg.indexOf(" ",first_Space_position + 1);
if(second_Space_position > 0) third_Space_position = incom_Msg.indexOf(" ",second_Space_position + 1);
if(third_Space_position > 0) foruth_Space_position = incom_Msg.indexOf(" ",third_Space_position + 1);
if(foruth_Space_position > 0)
{
Creator = incom_Msg.substring(0,first_Space_position);
Category = incom_Msg.substring(first_Space_position + 1,second_Space_position);
Number_str = incom_Msg.substring(second_Space_position + 1,third_Space_position);
Action = incom_Msg.substring(third_Space_position + 1,foruth_Space_position);
Board_No = incom_Msg.substring(foruth_Space_position + 1);
}
else if(third_Space_position > 0)
{
Creator = incom_Msg.substring(0,first_Space_position);
Category = incom_Msg.substring(first_Space_position + 1,second_Space_position);
Number_str = incom_Msg.substring(second_Space_position + 1,third_Space_position);
Action = incom_Msg.substring(third_Space_position + 1);
Board_No ="";
}
else if(second_Space_position > 0)
{
Creator = incom_Msg.substring(0,first_Space_position);
Category = incom_Msg.substring(first_Space_position + 1,second_Space_position);
Number_str = incom_Msg.substring(second_Space_position + 1);
Action = "";
}
else if(first_Space_position > 0)
{
Creator = incom_Msg.substring(0,first_Space_position);
Category = incom_Msg.substring(first_Space_position + 1);
}
else
{
Creator = incom_Msg;
Category ="";
}
/*
if(first_Space_position >0) Creator = incom_Msg.substring(0,first_Space_position);
else
{
Creator = incom_Msg;
Number_str ="";
Action = "";
Board_No = "";
}
if(second_Space_position > 0)
{
Category = incom_Msg.substring(first_Space_position + 1,second_Space_position);
}
else
{
Category = incom_Msg.substring(first_Space_position + 1);
Number_str ="";
Action = "";
Board_No = "";
}
*/
Number = Number_str.toInt();
/*
Serial.print("fst spc : ");
Serial.println(first_Space_position);
Serial.print("scnd spc : ");
Serial.println(second_Space_position);
Serial.print("third spc : ");
Serial.println(third_Space_position);
Serial.print("forth spc : ");
Serial.println(foruth_Space_position);
*/
/*
Serial.print("Creator : ");
Serial.println(Creator);
Serial.print("Category : ");
Serial.println(Category);
Serial.print("Number : ");
Serial.println(Number);
Serial.print("Action : ");
Serial.println(Action);
Serial.print("Action Size : ");
Serial.println(Action.length());
Serial.print("Board_No : ");
Serial.println(Board_No);
*/
if((LH_Serial_Available) && (Creator == This_board_no)) return; // stop continuous looping of wrong destination board // Stops executing the rest of loop() for this pass
// decide the action
if(Action == "ON")
{
State = true;
Action_Error = false;
}
else if(Action == "OFF")
{
State = false;
Action_Error = false;
}
else Action_Error = true;
// Implement the Action
if((Board_No == This_board_no) || Board_No =="") //if no board number or this board, proceed msg execution
{
Serial.println("This board ! executing ....");
if(Category == "LED") // Whether the category is valid
{
if(Number < 1 || Number > No_Of_LEDs ) valid_Number = false;
else valid_Number = true;
if(!Action_Error) // of no error in action
{
if(valid_Number)
{
digitalWrite(Number + 1,State);
Serial.println("Done !");
}
else Serial.println("Invalid LED Number !");
}
else Serial.println("Invalid Action !");
}
else
{
Serial.println("Invalid Category !");
}
}
else // Other board forward msg.
{
Serial.println("Not this board ! forwarding ....");
RHSerial.print(foward_Msg); // forward the original incomming msg.
Serial.println("Msg forwarded to next board!");
}
Creator = "";
Category = "";
Number = 0;
Action == "";
Board_No = "";
}
}