Use [code ] tags like I am.
#define rxBtPin 14
#define txBtPin 15
It's the C++ way to use const int or const byte to define constants
SoftwareSerial BTport = SoftwareSerial(rxBtPin, txBtPin);
That technically works but the correct way is to do this:
SoftwareSerial BTport(rxBtPin, txBtPin);
delay(1000);
Why?
pinMode(rxBtPin, INPUT);
pinMode(txBtPin, OUTPUT);
These are unnecessary I think
else
{
String pcPortIsReady;
if (BTport.available() != 0)
{
pcPortIsReady = constructText();
}
// if Arduio gets "coffee" from ComPort of the PC, it means PC port is ready. Then Arduino can send the menu to PC
if (pcPortIsReady == "Coffee")
{
showMenu(); // Now the PC port is open. And arduino will send me it's task list (the menu)
pcPortReady = true;
}
Using the String class can cause you some hurt. It makes things a tiny bit easier, but It can also case weird bugs when it fragments the heap. You can easily go without it here if you learn the most basic of the c string functions (which are no harder to use than any other...)
// In case the ComPort buffer is not empty, make it empty.
while (BTport.available() > 0)
{
BTport.read();
}
What if the computer had sent good data by this point and you throw it away?
void showMenu()
{
BTport.println("W : Close Curtains");
BTport.println("w : Open Curtains");
BTport.println("L : Turn on Light");
BTport.println("l : Turn off Light");
BTport.println("A : Set alarm");
BTport.println("a : Reset Alarm");
}
You might want to put those in PROGMEM with the F macro. Easy and saves you quite a bit of RAM
String constructText() // make a string from the chars in the serial buffer
{
String msg;
byte incomingByte;
delay(1000);
while (BTport.available() != 0)
{
byte len = BTport.available(); // Determine number of chars in serial buffer
for (byte i=0; i < len; i++)
{
incomingByte = BTport.read(); // Read a byte from serial buffer
if (incomingByte != 10) // Carriage return
msg = msg + (char)incomingByte; // construct a string from chars in the serial buffer
}
return(msg);
}
}
Get rid of the String class