Possible SoftwareSerial bug

I'm using;

Mac OS X 10.6.8
Arduino UNO with the Arduino 1.0.1 release
Sparkfun Bluetooth Mate Gold / Sparkfun Bluesmirf

Since I want to use pin 0 and 1 for Serial debugging purposes, I want to be able to use the bluetooth module on other pins; enter SoftwareSerial.

When I take the Arduino sketch and SoftwareSerial out of the equation by communicating directly via bluetooth to the device using a terminal, everything works fine (including enabling echo functionality). However, when I'm making my own Arduino echo sketch, the data coming back to the terminal is garbled (bit shifted?).

I've included code and further description here: arduino - Making an echo sketch using SoftwareSerial and Bluetooth - Electrical Engineering Stack Exchange.

I really can use some help figuring this out.

Thanks.

I really can use some help figuring this out.

Then post your code and other information somewhere that doesn't require authentication to access.

I'm not sure what you are referring to about the authentication. The link provided is viewable without logging in, you can see my code there. However, it's not problem for me to post it here;

#include <SoftwareSerial.h> 

SoftwareSerial softwareSerial(8, 9);

void setup() {
  softwareSerial.begin(115200);
  softwareSerial.println("Bluetooth Ready.");
  softwareSerial.println("Waiting...");

 delay(1000);
}

void loop() {
  int readByte;

  int bytes[10];

  int i = 0;
  boolean readSomething = false;

  softwareSerial.listen();

  long l = millis();
  while (millis() - l < 1000) {
    while (softwareSerial.available() > 0) {
      readByte = softwareSerial.read();
      bytes[i] = readByte;
      i++;

      readSomething = true;
    }
  }

  if (readSomething == true) {
    delay(20);
    readSomething = false;
    for (int c = 0; c < i; c++) {
      softwareSerial.print(bytes[c]);
      softwareSerial.println(" ");
      bytes[c] = 0;
    }

    i = 0;
  } 
}

Now I'll connect through bluetooth via a terminal and send a string to Arduino, which will be written back.

If I send a string of '11111' for example, I would like something consistent back - the problem is that I get something that is not consistent!

  int bytes[10];
      readByte = softwareSerial.read();
      bytes[i] = readByte;
      i++;

It would be a good idea to confirm that there is room in the array for the value. Since you are reading bytes (chars, presumably), why are you storing them in an int array?

If I send a string of '11111' for example, I would like something consistent back - the problem is that I get something that is not consistent!

We can't see what you do get, though...

We can't see what you do get, though...

The post on stackexchange.com actually contains a lot of information. I've copy/pasted a comment I made there; For example, if i send five 1s (11111) I get this back; 49, 152, 76, 166, 230 first time. Second transmit of the same string I get this; 49, 166, 166, 70, 243. I find it a bit strange that I get different numbers.

It would be a good idea to confirm that there is room in the array for the value. Since you are reading bytes (chars, presumably), why are you storing them in an int array?

True. However, this is a small test program, made for me sending a few chars at a time. And weather it is int or chars does not matter, since my main concern right now is just to get some consistency. When I send five of the same chars, I would expect to get five equal chars/ints back. This is my main concern - the lack of consistency.

Thanks for your effort.

And weather it is int or chars does not matter, since my main concern right now is just to get some consistency.

It DOES matter, since Serial.print(int) and Serial.print(char) do different things.

Here's your problem:

  softwareSerial.begin(115200);

Don't expect SoftwareSerial to work above 38400 baud, if your device is picky about serial timing above 9600 baud. I even have a WiFi Bee (RN-XV from Roving Networks) that doesn't work with SoftwareSerial with 9600 baud but has absolutely no problem even with higher baud rates when connected to the hardware serial. The timing is different with the SoftwareSerial as you can check with a scope yourself. It has some random time slips which seem to be enough for those picky devices to not work correctly. If you need an debug interface over USB, take an Arduino Leonardo, it's handling the USB serial directly in the processor leaving the USART free for you to use.

If you don't wanna buy another Arduino, use the SoftwareSerial for debugging and the hardware USART for the device connection. You just need a USB2Serial device like an FTDI cable or an Arduino USB2Serial Light adapter.

This is interesting. I thought that serial interface would work faster than 115K.

I thought that serial interface would work faster than 115K.

The hardware serial will, but this is an emulation in software. This emulation's timing is by far not as exact as the hardware version and picky devices react on that and give you wrong readings. Although you can specify speeds up to 115k for the SoftwareSerial you probably won't find many devices tolerant enough to be able to read what you written on that interface.