Hi,
I am trying to figure out how to communicate with the Arduino using bluetooth, first I found and modified a program that uses my PC's serial port and the program worked as expected. This is the code:
#include <SoftwareSerial.h>
int readByte;
char bytes[20];
int i = 0;
int eol = 13; // carriage return
boolean readSomething = false;
SoftwareSerial softwareSerial(7, 6);
void setup() {
Serial.begin(57600);
delay(300);
Serial.println("\nBluetooth Ready.");
Serial.println("Waiting...");
Serial.println("");
delay(1000);
}
void loop() {
//Serial.listen();
long l = millis();
while (millis() - l < 1000) {
while (Serial.available() > 0) {
readByte = Serial.read();
bytes[i] = readByte;
Serial.print(bytes[i]);
if ( bytes[i] == eol ) i--;
i++;
readSomething = true;
}
}
if (readSomething == true) {
delay(20);
readSomething = false;
for (int c = 0; c < i; c++) {
Serial.println("");
Serial.print(c);
Serial.println(" <-- index");
Serial.print(bytes[c]);
Serial.println(" <-- single");
Serial.print("Print Bytes = ");
Serial.println(bytes);
Serial.print("Write Bytes = ");
Serial.write(bytes);
Serial.println("");
Serial.println("------------------");
}
for (int z = 0; z < 21; z++) {
bytes[z] = 0;
Serial.print(z);
}
Serial.println("");
i = 0;
}
}
I then modified the program for bluetooth communications, I am using a bluesmirf gold device and an android phone using a terminal emulator app. When I use singe character inputs, it works as expected, but when I send 'hello' the program stores diffent characters in my 'bytes' variable. Here is the code.
#include <SoftwareSerial.h>
int readByte;
char bytes[20];
int i = 0;
int eol = 13; // carriage return
boolean readSomething = false;
SoftwareSerial softwareSerial(7, 6);
void setup() {
softwareSerial.begin(115200);
delay(300);
softwareSerial.println("\nBluetooth Ready.");
softwareSerial.println("Waiting...");
softwareSerial.println("");
delay(1000);
}
void loop() {
softwareSerial.listen();
long l = millis();
while (millis() - l < 1000) {
while (softwareSerial.available() > 0) {
readByte = softwareSerial.read();
bytes[i] = readByte;
softwareSerial.print(bytes[i]);
//delay(10);
if ( bytes[i] == eol ) i--;
i++;
readSomething = true;
}
}
if (readSomething == true) {
delay(20);
readSomething = false;
for (int c = 0; c < i; c++) {
softwareSerial.println("");
softwareSerial.print(c);
softwareSerial.println(" <-- index");
softwareSerial.print(bytes[c]);
softwareSerial.println(" <-- single");
softwareSerial.print("Print Bytes = ");
softwareSerial.println(bytes);
softwareSerial.print("Write Bytes = ");
softwareSerial.write(bytes);
softwareSerial.println("");
softwareSerial.println("------------------");
}
for (int z = 0; z < 21; z++) {
bytes[z] = 0;
softwareSerial.print(z);
}
softwareSerial.println("");
i = 0;
}
}
Do I need to configure my bluetooth device differently? or do I need to change how I interpret the data I receive?