2

I need to recieve data from a android phone and also estimate distance of phone from the HC 05 module , So i want to write two threads or functions. In one part i want recieve data from android phone store it in a variable and in other enter AT command mode to see RSSI of the phone.

2 Answers 2

2

You can switch between command and "online" modes, but you cannot use both at once. While in command mode you can't be receiving data, and while in online mode you can't be sending commands.

You would have to periodically switch between the two in your program at a time when you know you won't be receiving data (or you don't mind losing that bit of data).

And no, the Arduino doesn't have the concept of threads. There are a number of libraries that do emulate threads to a certain extent, but they are (usually) cooperative multi-tasking (one "thread" has to manually "yield" to another) and as such are pretty pointless, since you can get the same effect with a decent finite state machine.

2

Receiving serial characters is partially done in hardware and partially in the Arduino firmware. The only thing you need to do here is: - check if you've received a full message. - handle the message (if a full message has been received)

For the ultrasonic sensor, you'll have to: - Start a measurement. - wait for a response. - convert the response.

You can't do these exactly at the same time. But none of these will/should take up 100% of your "processor time". So we'll have to "share the time" of your microcontroller.

Though, timing may be critical in some of these parts. So for this, we will have to make sure that there is no delay in, for example, handling the ultrasonic sensor. (I'm not sure, and can't check since I'm on my phone, if Arduino uses interrupts for receiving characters from serial, which may mess up my logic below if these interrupts aren't prioritized well or the time an arduino RX interrupt takes so long it will influence your ultrasonic sensor readings significantly.)

But, you'll want to handle the ultrasonic responses as fast as possible, since this timing indicates the distance between an object. And you don't want to use processor speed while waiting for the result.We will probably want to use an interrupt here (if polling doesn't give a good enough result). See the Arduino page in interrupts, or use a HC-sr04 library that is non-blocking or interrupt/event-based

Now you can, if the ultrasonic sensor doesn't require any attention. Check and handle your serial buffer. (I believe there even is an option in the Arduino library to set a callback for a line end).

By doing the above. You can be sure that your ultrasonic readings are stable. And as long as the ultrasonic routines don't consume all processing time (which they shouldn't), you'll have time to handle your serial messages. Also have a look at state machines and non-blocking code, to avoid making your "subroutines" block eachothers execution while waiting for a result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.