I'm pretty lost and I need some help. I already searched in Google for some solutions, but I really didn't come forward to be honest.
I'm currently trying something with the HC-05 module. The goal is, to write an AT Command over the Arduino Software. Normally you have to type this physical on your own, but I want to prevent that.
if (Serial.available())
{
HC05Serial.write(Serial.read());
}
if (HC05Serial.available())
{
Serial.write(HC05Serial.read());
}
delay(100);
}
What I expect is, that the Serial Monitor writes "AT" on its own. Then it should get a feedback froom the HC05Serial, that it wrote down "OK", but unfortunately, nothing comes back. First I thought it might be the connection, which is wrong on my Hardware, but that's wrong, because I already tried it physically over the Serial Monitor. This was a total succeed.
Still tho, I want to make the first thing happen.
Now, I have two questions to this.
Is it even possible to send an AT Command over the Software with the Serial.write line or not?
Can you detect the fault? If yes, please explain it well enough, that I can understand it.
I'm totally sorry, if my grammer has been wrong all the time, because I'm actually from switzerland. If you have any questions to my post, feel free to message me.
and get rid of the delay(100);. Waiting a tenth of a second is a bad idea.
at 38400 bauds, you get 3840 characters per second if the line is busy, that is potentially 384 characters coming to you while you are stuck in the delay. The incoming buffer being only 64 bytes deep you'll be loosing information.
When dealing with asynchronous code, never delay anything, read stuff as fast as possible when they come in.
as a side note, Please correct your post above and add code tags around your code: [code]`` [color=blue]// your code is here[/color] ``[/code].
It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)
I found meanwhile a solution, and I don't really understand why I should get rid of the delay, because if I take it away, the AT Command gets send multiple times, which I don't want to do.
#include <SoftwareSerial.h>
SoftwareSerial HC05Serial(10, 11); // From the Module TX to 10, RX to 11
void loop()
{
// Read the State of the Button on Pin 5
buttonState = digitalRead(5);
// If the Button on Pin 5 is pressed...
if (buttonState)
{
if (buttonOneShot == 0)
{
// ... write in here the Address from the Slave you want to connect with, but only once thanks to the buttonOneShot variable
HC05Serial.write("AT+BIND=98d3,71,fdaf58\r\n");
}
buttonOneShot++;
}
else
{
buttonOneShot = 0;
}
if (Serial.available())
HC05Serial.write(Serial.read());
if (HC05Serial.available())
Serial.write(HC05Serial.read());
// Short delay, that the AT Command will be send only once
delay(100);
}
#include <SoftwareSerial.h>
SoftwareSerial HC05Serial(10, 11); // From the Module TX to 10, RX to 11
char buttonOneShot = 0;
char buttonState = 0;
void setup()
{
Serial.begin(9600);
HC05Serial.begin(38400);
pinMode(5, INPUT);
}
void loop()
{
// Read the State of the Button on Pin 5
buttonState = digitalRead(5);
// If the Button on Pin 5 is pressed...
if (buttonState)
{
if (buttonOneShot == 0)
{
// ... write in here the Address from the Slave you want to connect with, but only once thanks to the buttonOneShot variable
HC05Serial.write("AT+BIND=98d3,71,fdaf58\r\n");
}
buttonOneShot++;
}
else
{
buttonOneShot = 0;
}
if (Serial.available())
HC05Serial.write(Serial.read());
if (HC05Serial.available())
Serial.write(HC05Serial.read());
// Short delay, that the AT Command will be send only once
delay(100);
}
If you are going to echo Serial data it is better to have the BAUD rates matching so not one of the buffers overflows the other.
// Short delay, that the AT Command will be send only once
delay(100);
You'd be better of waiting for the button to be 'un-pressed' at the very start of things.
You are trying to do that with the 'buttonOneShot', but obviously it isn't working for you. (why would you need the delay otherwise)
If you are going to echo Serial data it is better to have the BAUD rates matching so not one of the buffers overflows the other.
// Short delay, that the AT Command will be send only once
delay(100);
You'd be better of waiting for the button to be 'un-pressed' at the very start of things.
You are trying to do that with the 'buttonOneShot', but obviously it isn't working for you. (why would you need the delay otherwise)
The goal of the whole code is this: I want to receive the "OK" from the HC05Serial over the Serial over PC, as soon as I press the button.
By the last code it does work and I wonder too, why the delay needs to be included. I already tried it to remove, but then the Serial Monitor sends more "OK"'s at once.
but then the Serial Monitor sends more "OK"'s at once.
So your debounce method is not working !
how do you have this button wired up ? pinMode(5, INPUT);The most normal method is to use an 'INPUT_PULLUP' either the arduino's internal one or an addition one on the outside, and let the button 'Ground the pin out' In that case you should test for opposite logic hereif (buttonState)
It's a matter of your perception, but on this forum it is not an uncommon 'tone'
About you debouncechar buttonOneShot = 0;chances are the variable rolls over before you let go, you should do a debounce method using millis() iow the buttonState needs to be the same for the debounce time to be 'confirmed altered' That said, this is just details if you've got it working as is, though it may haunt you later in the development.