1

I am doing a project where I need to send data from ultrasonic sensor wirelessly present in one arduino to other arduino where I need these values in Serial monitor. But the problem is I cannot able to send these values through bluetooth. I tried to send one character, it is appearing in serial monitor.. But when I tried to the same for integer values it is not appearing in serial monitor. I have configured Master and Slave modes for the Bluetooth. I have uploaded the image of the code which I am using to send these values. Please help me on this. Thanks in advance .

 code 

//@ transmitting end
#define trigPin 12
#define echoPin 11

void setup() {

  Serial.begin(38400); // Default communication rate of the Bluetooth module
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {


 long duration;
  float distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;

 Serial.println(distance,2); // Sends floatValue 
 delay(500);

}


//@ receving end

#include <SoftwareSerial.h>
#define led 13
SoftwareSerial BTSerial(10, 11);
int data=0;
void setup() {

  pinMode(led,OUTPUT);
  Serial.begin(38400);
  BTSerial.begin(38400); // Default communication rate of the Bluetooth module
}

void loop() {
  int number;
 if(Serial.available() > 0){ // Checks data is  from the serial port
 data = BTSerial.read(); // Reads the data from the serial port
 //analogWrite(led,data);
 delay(10);
 //Serial.println(data);

 }
 Serial.println(data);
}

I need integer values at the serial monitor. But there I am getting some symbols like ?/<>..

Deepakvg
  • 71
  • 2
  • 9

2 Answers2

0

From the Arduino reference, Serial.read() only reads the first available byte available in the Serial buffer. As an int is coded on 8 bytes, I would say that you need to read the incoming bytes sequentially in order to get the full value.

Maybe you can implement this by putting (Serial.available() > 0) in a while loop, concatenate the values you get in a char[8] for instance and then convert this char to a integer value.

Also, beware that you are sending floats and not int.

Tom
  • 303
  • 3
  • 14
  • `an int is coded on 8 bytes` That depends. On a small Arduino, int is 2 bytes. Eventually easier send a readable text, including terminating character When sending binary, a single byte ( 0 .. 255 ) might be a possibility, too. You should understand data types anyway. – datafiddler Apr 30 '19 at 09:59
  • Yes, you're right. Anyway, it is not one byte so this might even be the problem. – Tom Apr 30 '19 at 11:50
0

Thanks for the help..! I modified the code in the receiver end to get the float values from the transmitter.. Here is my modified code

#include <SoftwareSerial.h>
int bluetoothTx = 10; 
int bluetoothRx = 11;
String content; //content buffer to concatenate characters

char character; //To store single character


SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup(){
  bluetooth.begin(38400);
  Serial.begin(9600);
}

void loop(){
  bluetooth();

}

void bluetooth(){ //
  while(bluetooth.available()){
    character = bluetooth.read();
    content.concat(character);
   if(character == '\r'){ // find if there is carriage return
     Serial.print(content); //display content (Use baud rate 9600)
     content = ""; //clear buffer
     Serial.println();
  }
  }
}
Deepakvg
  • 71
  • 2
  • 9