Bluetooth problems turning on/off LED.

My code uses a button to input Morse code which is displayed on an LCD and flashes an LED on pin 5, but also incorporates a bluetooth HC05 which controls an LED on pin 13.

Seperately, they work fine but I wanted to merge the two sketches. I have succeeded but for one small problem.

When pressing the LED 'ON' button on my android app, the LED only comes on after the Morse code button has been pressed. And it works the same way for the LED 'OFF' function on the app. Press it, then press the Morse code button and the LED switches off again. This repeats. I cannot figure out why this button is controlling the Bluetooth LED.

(I'm very new to all this so please be gentle!)

Here is the code...
(I uploaded it to pastebin as it exceeds the 9000 character limit here. Hope that is ok!)

Bluetooth code

Can anyone shed some light on how to solve this?

Hope that is ok!

It is, if you don't expect anyone to look at it.

If you do, use Reply (not the Quick Reply field), and use the Additional Options link to attach the code HERE.

Ah, ok. I have attached it as an ino file.
Hope that is ok this time?

Morse_BT.ino (7.72 KB)

I have attached it as a txt file.

Where?

It's an ino in the post above. Sorry, was being hampered by post restriction times as I'm a newbie.

I'm wondering if changing the baud rate of the bluetooth module would fix this issue?
Am I on the right tracks?

edit No, I was not. That made no difference!

// While there is data in the buffer
    delay( 3 );

While there is data, sit around for a while hoping it will go away? What is the point of the delay()?

When pressing the LED 'ON' button on my android app, the LED only comes on after the Morse code button has been pressed.

I don't know what the "LED 'ON' button" does, but I can guess that it sends serial data to the Arduino. Serial data arrives slowly, so there will be time between the 'o' arriving and the 'n' arriving. During that time, loop() could iterate 1000 times.

Except that you do other things that block, like the call to getNextChar(). If you don't want blocking behavior, don't write blocking code.

Like I say, I am new to this.

The delay was already in the code I used from a tutorial, I am unsure what it was for. Just didn't want to remove it unless I fully understood what it did. (Still learning!)

The LED ON sends a message from the android app to the bluetooth to turn on the LED at pin13.

The LED ON sends a message from the android app to the bluetooth to turn on the LED at pin13.

What message? Why do you assume that it

arrives all

at once?

I didn't say it did. I said what it should do.

Look, forget it. This is the problem with asking some programmers for help.
They act like massive douchebags because someone else is trying to learn what they have mastered.

If you wanted to help, great. I would appreciate it.

But you clearly want to act like some superior jerk.
So don't bother offering advice, not that you have.
I'll go ask somewhere else.

Enjoy your smug sense of self satisfaction.

Apparently this is the answer ...

You have spin-loops waiting for the button press. Once in that part of the code you are stuck until the button is pressed and the code exits. Instead to look to see if a button has been pressed. If so, handle it. Otherwise go check for stuff to do on the Bluetooth.

Seems other forums have people who aren't up their own backsides.

I'll leave the answer here should anyone else ever run into this issue.