#include <SoftwareSerial.h>
SoftwareSerial BTserial(8, 9); // RX | TX
const long baudRate = 9600;
//char c=' ';
boolean NL = true;
int LED = LED_BUILTIN;
int index = 0;
char data[10];
char c;
boolean flag = false;
void setup()
{
Serial.begin(9600);
Serial.print("Sketch: "); Serial.println(__FILE__);
Serial.print("Uploaded: "); Serial.println(__DATE__);
Serial.println(" ");
BTserial.begin(baudRate);
Serial.print("BTserial started at "); Serial.println(baudRate);
Serial.println(" ");
}
void processCommand(){
char command = data[0];
char inst = data[1];
switch(command){
case 'L':
if(inst == 'Y'){
digitalWrite(LED_BUILTIN,HIGH);
Serial.println("Light: ON");
}
else if(inst == 'N'){
digitalWrite(LED_BUILTIN,LOW);
Serial.println("Light: OFF");
}
break;
}
}
void loop()
{
// Read from the Bluetooth module and send to the Arduino Serial Monitor
if (BTserial.available())
{
while(BTserial.available() > 0){
c = BTserial.read();
delay(10); //Delay required
data[index] = c;
index++;
}
data[index] = '\0';
flag = true;
if(flag){
processCommand();
flag = false;
index = 0;
data[0] = '\0';
}
}
// Read from the Serial Monitor and send to the Bluetooth module
if (Serial.available())
{
c = Serial.read();
BTserial.write(c);
// Echo the user input to the main window. The ">" character indicates the user entered text.
if (NL) { Serial.print(">"); NL = false; }
Serial.write(c);
if (c==10) { NL = true; }
}
}