#include #include LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display #include const byte rxPin = 2; const byte txPin = 3; #define MY_NAME "Slaveb" SoftwareSerial mySerial(rxPin, txPin); const byte numChars = 32; char receivedChars[numChars]; char tempChars[numChars]; // temporary array for use when parsing // variables to hold the parsed data char messageFromPC[numChars] = { 0 }; int integerFromPC = 0; boolean newData = false; void setup() { Serial.begin(9600); mySerial.begin(9600); lcd.init(); // initialize the lcd lcd.backlight(); } void loop() { recvWithStartEndMarkers(); if (newData == true) { strcpy(tempChars, receivedChars); // this temporary copy is necessary to protect the original data // because strtok() used in parseData() replaces the commas with \0 parseData(); Serial.print("Message "); Serial.println(messageFromPC); Serial.print("Integer "); Serial.println(integerFromPC); if (strcmp(messageFromPC, MY_NAME) == 0 && integerFromPC == 1) { lcd.blink(); lcd.setCursor(1, 0); lcd.print("Hello, world!"); Serial.print("ِAction"); } else if (messageFromPC[numChars] = "Slavea" && integerFromPC == 1) { lcd.clear(); } else if (messageFromPC[numChars] = "Slavea" && integerFromPC == 0) { lcd.clear(); } newData = false; } } //============ void recvWithStartEndMarkers() { static boolean recvInProgress = false; static byte ndx = 0; char startMarker = '<'; char endMarker = '>'; char rc; while (mySerial.available() > 0 && newData == false) { rc = mySerial.read(); if (recvInProgress == true) { if (rc != endMarker) { receivedChars[ndx] = rc; ndx++; if (ndx >= numChars) { ndx = numChars - 1; } } else { receivedChars[ndx] = '\0'; // terminate the string recvInProgress = false; ndx = 0; newData = true; } } else if (rc == startMarker) { recvInProgress = true; } } } //============ void parseData() { // split the data into its parts char* strtokIndx; // this is used by strtok() as an index strtokIndx = strtok(tempChars, ","); // get the first part - the string strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC strtokIndx = strtok(NULL, ","); // this continues where the previous call left off integerFromPC = atoi(strtokIndx); // convert this part to an integer } //============