Electronic – arduino – Making an echo sketch using SoftwareSerial and Bluetooth

arduinobluetoothserial

In order to test my Arduino UNO + Sparkfun Bluetooth Mate Gold I've written this little sketch;

#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!

Best Answer

if your problem is only in characters you receive back just use char[] array instead of int e.g.

char bytes[10];

Now you'll receive the same data back.

Hopefully it solve your problem