bluetooth serial communication

hello,i trying to code using an HC-05 bluetooth module and arduino mega 2560 for bluetooth serial communication.The data is send to bluetooth module using bluetooth serial terminal and recieved to serial monitor of arduino software.But the problem is that while i wan’t it to serial.print"INVALID" only when the any other wrong command has been sent.But it conitinuesly displaing “INVALID” on serial monitor.Pls help me out.

code

#include <SoftwareSerial.h>
SoftwareSerial BTserial(10,11); // RX | TX
// Connect the HC-05 TX to Arduino pin 10 RX.
// Connect the HC-05 RX to Arduino pin 11 TX

String s ;
char c ;
void setup()
{
Serial.begin(9600);
Serial.println(“Arduino is ready”);
BTserial.begin(9600);
BTserial.println(“Bluetooth is ready”);
}

void loop()
{
// using a constant String:
BTserial.available();
String s = BTserial.readString();
char c =BTserial.read();

switch(1){
case 1:
if( s == “hello”){
Serial.println(“hi”);
break;
}

case 2:
if( s == “hey”){
Serial.println(“yeah”);
break;
}

case 3:
for (s!=“hello”; s!=“hey”; c == char (c))
{
Serial.println(“INVALID”);
break;
}
}
}

You have 4 hardware serial ports on the Mega. You only use one of them. Why are you wasting the other three, and using SoftwareSerial?

 for (s!="hello"; s!="hey"; c == char (c))
 {
  Serial.println("INVALID");
  break;
  }

WTF? Why do you have a for statement? Why do you, in the initialization section, assign s the value of true or false? What, for a String, does that mean? The whole for statement is just too stupid to analyze.

Switching on a constant doesn't make any sense, either.

PaulS,
the hardware serial didn't worked for me

do you have suggestion for this part ?

for (s!="hello"; s!="hey"; c == char (c))
{
Serial.println("INVALID");
break;
}

do you have suggestion for this part ?

I do. Select all of it. Hit the delete key. Start over.

It is NOT at all clear what you are trying to do. Whatever it is, I seriously doubt that it involves switch/case or for statements.

okay, i will explain it suppose
If i sent "this" to bluetooth and i want to recieve "that" on serial montor.If something else is sent suppose i send "they" it should serial print "INVALID".

okay, i will explain it suppose
If i sent "this" to bluetooth and i want to recieve "that" on serial montor.If something else is sent suppose i send "they" it should serial print "INVALID".

   String pissAwayResourcesUselessly = Serial.readString();
   if(pissAwayResourcesUselessly == "this")
      Serial.print("that");
   else
      Serial.print("INVALID");

This is a problem, the serial monitor is displaying invalid again and again even if i doesn’t type any word. i only want it to print “invalid” when any wrong word is sent except “this”
can you solve it ???

Ransher:
i only want it to print "invalid" when any wrong word is sent except "this"

Reply #3 is probably the best advice you will ever get, but you might also check the serial input basics sticky. Your logic is fundamentally flawed, and I submit that what is really going on is that you are (successfully) printing "invalid" when the right word is not properly received.

You could send "hello"s for ever but it will print "invalid" every time because

  1. Bluetooth is improperly wired. OR

  2. Your code is junk OR

  3. both

While using software serial on a Mega is stupid, it will work if it is properly written, which is probably not the case here. I believe the serial read section isn't kosher, thereby permanently condemning you to "invalids"s, no matter what you send.

You might find the following background notes useful

http://homepages.ihug.com.au/~npyner/Arduino/GUIDE_2BT.pdf
http://homepages.ihug.com.au/~npyner/Arduino/BT_2_WAY.ino

can you solve it

Of course. PM me and I'll send you an address. You send me your PC with the code on it, the Arduino with all required hardware, and a blank check. I'll fix the code, fill in a suitable amount on the check, and deposit it. When it clears, I'll send everything back to you.

If you want free help here, YOU need to post the code, AND proof that you have made SOME effort to solve the problem, such as echoing what the Arduino received that is being compared to the expected input.

hello,
the coding i am doing from last 1 week to run a simple program has been attached below.
i want it to print serially on serial monitor “INVALID” only once and only when any char, word,int,var,symbol is sent to bluetooth HC-05 through bluetooth terminal except “hello” and “hey”

the problem is with case 3 its printing " INVALID" simultaneously

though case 1 and case 2 are working perfectly

serial and Bluetooth terminal screenshot is attached

code

#include <SoftwareSerial.h>
SoftwareSerial BTserial(10, 11); // RX | TX
// Connect the HC-05 TX to Arduino pin 10 RX.
// Connect the HC-05 RX to Arduino pin 11 TX

String s ;

void setup()
{
Serial.begin(9600);
Serial.println(“Arduino is ready”);
BTserial.begin(9600);
BTserial.println(“Bluetooth is ready”);
}

void loop()
{
// using a constant String:
BTserial.available();
String s = BTserial.readString();
char c = BTserial.read();

switch (1) {
case 1:
if ( s == “hello”) {
Serial.println(“hi”);
break;
}

case 2:
if ( s == “hey”) {
Serial.println(“yeah”);
break;
}

case 3:
if (s != “hello” || s != “hey” )
{

Serial.println(“INVALID”);
break;
}
}
}

      if (s != "hello" || s != "hey" )

Lets suppose that s contains "banana". "banana" is not "hello", so Invalid is printed.

Lets suppose that s contains "hey". "hey" is not "hello", so Invalid is printed.

Lets suppose that s contains "hello". "hello" is equal to "hello", so the first clause is false. "hello" is not "hey", so the second clause is true, so Invalid is printed.

Perhaps you want && there, instead. "banana" is not "hello" AND "banana" is not "hey".

the picture of bluetooth terminal and serial monitor is attached down
after changing to
if (s != “hello” && s != “hey” )

still case 3 is having some issue
case 1 and case 2 are working perfectly

PaulS:

      if (s != "hello" || s != "hey" )

Lets suppose that s contains “banana”. “banana” is not “hello”, so Invalid is printed.

Lets suppose that s contains “hey”. “hey” is not “hello”, so Invalid is printed.

Lets suppose that s contains “hello”. “hello” is equal to “hello”, so the first clause is false. “hello” is not “hey”, so the second clause is true, so Invalid is printed.

Perhaps you want && there, instead. “banana” is not “hello” AND “banana” is not “hey”.

BTserial.available();

Determine how many bytes of data are available to read. Completely ignore the value returned.

Why?

String s = BTserial.readString();

What the hell is in s? Why is that SO difficult for you to determine?

   Serial.print("The doofus entered: [");
   Serial.print(s);
   Serial.println("]");

@Ransher - no PM please.

post stuff here

J-M-L:
@Ransher - no PM please.

post stuff here

…and in code tags this time, please.

Withswitch (1) {how do you plan to execute anything else than case 1?
With the break within the if(), you are going through cases unless the if was validated - seems a weird way to use a switch case statement

Why are you using software serial while your mega has plenty of free hardware Serial?

You seem very confused about if, switch, for etc...

Please read a tutorial on simple flow control (check on the left column the various constructs)

still the same problem is there
it runs invaid continously :confused: :frowning:

void loop()

{
BTserial.available();
s = BTserial.readString();

if ( s == "hello")
{
Serial.println("hi");
}
else if ( s == "hey")
{
Serial.println("yeah");
}
else if (s != "hello" && s != "hey" )
{
Serial.println("INVALID");
}
else
{
Serial.println();
}
}

And what is in s when the code runs "invaid"?

Please remember to use code tags when posting code.

What is this BTserial.available(); for?

and the code tags would help too...

AWOL:
And what is in s when the code runs “invaid”?

Please remember to use code tags when posting code.

What is this

BTserial.available();

for?

BTserial.available //bluetooth available

check the last code posted