//by Nissanka MD SLCOTM
String incom_Msg;
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
void setup() {
Serial.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() {
if(Serial.available()>0)
{
/*
delay(50);
Serial.print("Serial Message Size : ");
Serial.println(Serial.available());
*/
incom_Msg = Serial.readString();
/*
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);
Action = incom_Msg.substring(second_Space_position + 1); // incom_Msg.substring(second_Space_position + 1,third_Space_position);
//Board_No = incom_Msg.substring(third_Space_position);
Number = Number_str.toInt();
/*
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());
*/
// 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(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 !");
}
}
}