#include <SoftwareSerial.h> //Using the Arduino library Software Serial
in order to avoid using the Hardware Serial on the Arduino Uno / microcontroller
char junk; //Defining an empty character variable named junk
String inputString=""; //Defining an empty string variable named inputString
SoftwareSerial mySerial(6, 7); // RX, TX Initializing the Software Serial
void setup() {
Serial.begin(9600); // Initializing the Hardware Serial
mySerial.begin(38400); // Initializing the Software Serial
pinMode(4, OUTPUT); // Defining the pin for an I/O device (vibrating motors, in our case)
pinMode(5, OUTPUT); // Defining the pin for an I/O device (vibrating motors, in our case)
}
void loop()
{
if(mySerial.available()){ //Checks the Serial connection
while(mySerial.available())
{
char inChar = (char)Serial.read(); //read the input
inputString += inChar; //make a string of the characters coming on serial
}
Serial.println(inputString);
while (mySerial.available() > 0)
{ junk = mySerial.read() ; } // clear the serial buffer
if(inputString == "a"){ //in case of 'a' turn the left motor on and the right motor off
digitalWrite(l, HIGH);
digitalWrite(r, LOW);
}else if(inputString == "b"){ //incase of 'b' turn the left motor off and the right motor on
digitalWrite(l, LOW);
digitalWrite(r, HIGH);
}
inputString = "";
}
}