0

I have a Arduino Uno R3 and a Bluetooth Mate. When linking the Mate to the Arduino Hardware Serial (pin 0,1) I can send multiple characters at once from my connected device but when I try to make the same thing with Software Serial (using pin 4,2 for example) I only get the first character and the rest of the chars are messed up.

My code:

#include <SoftwareSerial.h>  

int bluetoothTx = 4;  
int bluetoothRx = 2;  

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup() 
{
  Serial.begin(115200);  
  bluetooth.begin(115200);  
}

void loop()
{
  if(bluetooth.available())
  {
    Serial.print((char)bluetooth.read());  
  }
}

For example if I send this chars: abcd from my android device I get this in the serial monitor: a±,ö

This code that uses Hardware Serial (I link my bluetooth to pins 0 and 1) works just fine:

void setup()
{
  Serial.begin(115200);  
}

void loop()
{
  if(Serial.available())
  {
    Serial.print((char)Serial.read());  
  }
}

I even tried changing the baud rate but it didn't helped

If I send the chars one by one it works fine but I would like to be able to send them as a string.

amarkovits
  • 776
  • 1
  • 10
  • 17
  • First try scaling back the baud rate on SoftwareSerial as low as you can get it. If the issue clears up, then you know you're up against the limitations of bit-banging. –  Oct 08 '14 at 12:54
  • Looks like the guide here supports my theory: https://learn.sparkfun.com/tutorials/using-the-bluesmirf/example-code-using-command-mode –  Oct 08 '14 at 13:00
  • Another thing to consider. That board runs at 16MHz, making for a cycle time of 62.5ns. At 115200bps, the width of a single bit is 8.6uS (about 140 cycles). Assume that for RX you need to sample 8 times per bit to frame it properly. At the same time, you have to handle changing the output state for TX. All while running your application code. It's not surprising if the frames get a bit garbled. –  Oct 08 '14 at 13:11
  • double-check that your RX pin supports pin-change interrupts: http://arduino.cc/en/Reference/softwareSerial. This link also recommends Paul Stoffregen's AltSoftSerial library as a more robust alternative. –  Oct 08 '14 at 13:14
  • you are right, it is baudrate related, I created an answer with the correct code. – amarkovits Oct 09 '14 at 07:22

2 Answers2

0

You could try to bufferize the string before printing it.

Look at the following answer: Convert serial.read() into a useable string using Arduino?

Community
  • 1
  • 1
ladislas
  • 3,037
  • 19
  • 27
0

As pointed out by @hyperflexed in the comments this is a baudrate related issue. I had to take the baudrate as low as 9600 to make it work.

This is the code that worked:

#include "SoftwareSerial.h";
int bluetoothTx = 4;
int bluetoothRx = 2;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
  Serial.begin(9600);
  delay(500);
  bluetooth.begin(115200);
  delay(500);
  bluetooth.print("$$$");
  delay(500);
  bluetooth.println("U,9600,N");
  delay(500);
  bluetooth.begin(9600);
}

void loop()
{
  if(bluetooth.available()) {
    char toSend = (char)bluetooth.read();
    Serial.print(toSend);
  }

  if(Serial.available()) {
    char toSend = (char)Serial.read();
    bluetooth.print(toSend);
  }
}

For changing the baudrate I had to put some big delays to make sure the commands are executed otherwise it won't work.

amarkovits
  • 776
  • 1
  • 10
  • 17