Hi - I have a project to interface a Bluetooth device via Arduino to a PC game. (Game engine won't talk directly to BT.) Learning as I go along - I've got a fair amount done, but not sure if it's actually possible to have both USB and Bluetooth sending to the same sketch?
I'm using a Leonardo board (but have Uno if that would be any better). Using Serial port for the USB/Serial from the PC. Using HC-05 on a SoftwareSerial port to connect to the BT device.
Requirements are:
- PC game sends instructions in the form of numeric values to Arduino via USB/Serial
- For development/debugging, need to write to Serial Monitor, in which case dummy instructions will be sent from the Monitor
- Arduino receives instructions from PC, packages them for the BT device's protocol, and sends them to the device, which will send acknowledgement
- Device also sends 'keep alive' signal, to which Arduino must respond, else the BT connection is broken
This means I need to be able to send/receive to/from both BT and USB. I can get a conversation (keep alive) going between Arduino and BT device; in a separate sketch, I can receive and process signals from PC/USB; but I can't get both in a single sketch. I've read various posts and tutorials, but can't quite pin down just what I need. For example:
- I can't get Serial and SoftwareSerial to 'listen' alternately, as Serial doesn't have a 'listen' member
- I can't use 'Stream *mySerial' to switch between them as this doesn't work with SoftwareSerial. Not sure if an alternative to SS might work with this?
- I've tried using Serial1 (on pins 0 and 1) for the HC-05 but - with no other changes to my sketch - Serial1.available() constantly returns 0, so it's not getting any signals from BT
I'd be most grateful for any advice on whether this is feasible; if so, what should I try to get it working; and do I need a different board and/or alternative to SoftwareSerial?
Many thanks
1 Like
I understand you are trying to use Arduinio as a go-between, and I think it's entirely feasible. I'm not familiar with Leonardo, but I believe it is actually more suitable than the Uno. Leonardo should be OK with Bluetooth on hardware Serial1 using pins 0,1 and USB cable on serial.
I don't understand why you need a "keep alive" signal, and I bet you don't.
The only time Serial doesn't listen is when you don't tell it to listen
http://forum.arduino.cc/index.php?topic=396450.0
You might find the following background notes useful.
http://homepages.ihug.com.au/~npyner/Arduino/GUIDE_2BT.pdf
http://homepages.ihug.com.au/~npyner/Arduino/BT_2_WAY.ino
Many thanks Nick_Pyner for your speedy response. To take your points in order:
- As mentioned, I tried Serial1 on pins 0 and 1 but it doesn't receive any signal under conditions when SoftwareSerial does.
- Sadly, I do need the Keep Alive - this is initiated by the BT device, and if it doesn't receive a reply after 5 signals it breaks the BT connection. I have no control over this. Also, as the device is a bit of a black box, if I don't receive anything from it, I can't tell what (if anything) my signals are doing (ie I can't debug).
Nick_Pyner:
The only time Serial doesn't listen is when you don't tell it to listen
- When I first tried to include both USB and BT receiving in my sketch, it threw an exception on compile that I could only get rid of by removing one or other of the receive functions; that suggested that there was a problem with running both in the same sketch. However, I've just gone back to that original version and it no longer throws the exception, so it looks as though I can carry on with that.
- Thanks for the links - I've already relied heavily on Serial Input Basics to get as far as I have, many thanks to Robin2 for that :). I'll have a read of your other two links, I'm sure there'll be something in there that I haven't found elsewhere, so thanks again.
I'll keep working on it, and come back here if I have further problems - or to close this if all goes well.
Thanks again for your help - I'm definitely past that problem now, still plenty of new puzzles to sort out but hopefully I'll get there eventually!
How do I close this? Or does it get closed automatically somehow?
Just leave it there - as pearls of wisdom for somebody else....
OK - and thanks yet again 
As mentioned, I tried Serial1 on pins 0 and 1 but it doesn't receive any signal under conditions when SoftwareSerial does.
This is peculiar. Hardware Serial1 is far superior to software serial and it would be better to get it working.
Did you cross connect the module TX to Leonardo RX(0) and module RX to Leonardo TX(1)?
What is the SoftwareSerial constructor (RX,TX pins) in the working sketch, and what module pins are connected to each?