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);}
}
}
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
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
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
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.