//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.
This has 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.
(This will be corrected in next step 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 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 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();
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);
*/
// Seperate the Codes
first_Space_position = incom_Msg.indexOf(" ");
second_Space_position = incom_Msg.indexOf(" ",first_Space_position + 1);
third_Space_position = incom_Msg.indexOf(" ",second_Space_position + 1);
Category = incom_Msg.substring(0,first_Space_position);
Number_str = incom_Msg.substring(first_Space_position + 1,second_Space_position);
//Serial.print("third_Space_position : ");
//Serial.println(third_Space_position);
if (third_Space_position > 0)
{
Action = incom_Msg.substring(second_Space_position + 1,third_Space_position); // incom_Msg.substring(second_Space_position + 1);
Board_No = incom_Msg.substring(third_Space_position + 1);
}
else if (third_Space_position <= 0 )
{
Action = incom_Msg.substring(second_Space_position + 1);
Board_No = "";
}
Number = Number_str.toInt();
//Serial.print("Board Address : ");
//Serial.println(Board_No);
/*
Serial.print("Category : ");
Serial.println(Category);
Serial.print("Category : ");
Serial.println(Category);
Serial.print("Number : ");
Serial.println(Number);
Serial.print("Action : ");
Serial.println(Action);
Serial.print("Board_No : ");
Serial.println(Board_No);
Serial.print("Action Size : ");
Serial.println(Action.length());
*/
//Serial.print("Board_No : ");
//Serial.println(Board_No);
// decide the action
if(Action == "ON")
{
State = true;
Action_Error = false;
Action == ""; ///////////////////////
}
else if(Action == "OFF")
{
State = false;
Action_Error = false;
Action == ""; /////////////////
}
else Action_Error = true;
// Implement the Action
if((Board_No == This_board_no) || (Board_No =="") )//this board proceed msg execution
{
Serial.println("This board ! executing ....");
Board_No == ""; //////////////////
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.
}
}
}