[solved]Problem with HC05 module code

Hi guys!
I'm going to use HC05 to control my motor (this isn't my problem). because HC05 is new to me, i've tried to do a simple project with it, in beginning. I wrote this code and hoped to get off with press "s" in my phone and on with other key, but just second condition is working and no turn off I see. why???
I used serial.print to check is the board receiving correct char and it was. (pls just examine the code)

#include <SoftwareSerial.h>


SoftwareSerial bluetooth(2, 3);

void setup()
{
  Serial.begin(9600);
  bluetooth.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
}


void loop()
{
  if (bluetooth.available() > 0)
  {
        char a = bluetooth.read();
      
     if (a == 's')
    {
      digitalWrite(LED_BUILTIN,LOW);
      
    } else
    {digitalWrite(LED_BUILTIN,HIGH);}
  }
 
}

Echo what you get to serial so you can see whether you're actually receiving anything.

wildbill:
Echo what you get to serial so you can see whether you're actually receiving anything.

I did and was correct.

Are you sure that when you send an s, that other characters (CR, LF) don't get sent too?

wildbill:
Are you sure that when you send an s, that other characters (CR, LF) don't get sent too?

I see in my laptop serial terminal receiver just "s".

using serial-output is the main technique to analyse what is going on in your code.
CR / LF with "CR" meaning carriage return and "LF" meaning Line feed"
are unprintable "characters" they do what they say

CR jump back to first columm
LF jump into next line

so here is a version of your program that prints out what is received

#include <SoftwareSerial.h>

SoftwareSerial bluetooth(2, 3);

void setup() {
  Serial.begin(9600);
  bluetooth.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
}


void loop() {
  if (bluetooth.available() > 0) {
    char a = bluetooth.read();

    Serial.print("received #");
    Serial.print(a);
    Serial.println("#");

    if (a == 's') {
      digitalWrite(LED_BUILTIN, LOW);
    } 
    else {
      digitalWrite(LED_BUILTIN, HIGH);
    }
  }
}

to analyse with high precision what you received this code prints a "#" right before and right after the received character. This will make visible of you received something like LF.
If a LF is received to serial output looks like this

#
#

the first "#" is in one line and the second is in the line below
because
a contains the unvisible LF
print the first "#"
jump into new line because of the LF
print "#"
best regards Stefan

StefanL38:
using serial-output is the main technique to analyse what is going on in your code.
CR / LF with "CR" meaning carriage return and "LF" meaning Line feed"
are unprintable "characters" they do what they say

CR jump back to first colum upm
LF jump into next line

so here is a version of your program that prints out what is received

#include <SoftwareSerial.h>

SoftwareSerial bluetooth(2, 3);

void setup() {
  Serial.begin(9600);
  bluetooth.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
}

void loop() {
  if (bluetooth.available() > 0) {
    char a = bluetooth.read();

Serial.print("received #");
    Serial.print(a);
    Serial.println("#");

if (a == 's') {
      digitalWrite(LED_BUILTIN, LOW);
    }
    else {
      digitalWrite(LED_BUILTIN, HIGH);
    }
  }
}




to analyse with high precision what you received this code prints a "#" right before and right after the received character. This will make visible of you received something like LF.
If a LF is received to serial output looks like this




the first "#" is in one line and the second is in the line below 
because
a contains the unvisible LF
print the first "#"
jump into new line because of the LF
print "#"
best regards Stefan

Many thanks I will test it tomorrow.
Seems u r right, cause each "s" is in the separate line, maybe I must change my app. which app do u suggest?

So you are using a a "standard" serial bluetooth app? Where you just type "s". I guess in the configuration you can switch on / off sending additional CR / LF or not.

I have done only a little bit tinkering with the App Serial bluetooth. I did not test a lot of apps just two and this one is pretty good configurable

The other solution is to change your arduino-code that it can handle CR / LF correctly

take a look into the serial input basics with example codes how to do that

https://forum.arduino.cc/index.php?topic=396450

best regards Stefan

Choose two characters to control the system. If you get 's' turn the LED off. if you get 't', turn it on. Your current problem is that anything but 's' turns it on, including CR & LF. You just need to be explicit about the second character and if you get anything but 's' or 't' do nothing.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.