Go Down

Topic: Need help understanding what is happening on the slave arduino (Read 134 times) previous topic - next topic

TEAC

Hello everyone, you might know me already from the previous several posts about bluetooth communication using two HC05 bluetooth modules. The good thing is I have managed to pair them both and send actual data from one to another, currently I have a question about what is wrong that I cannot see on my receiver code that is: I'm collecting data from a flex sensor on the master arduino and sending it via SoftwareSerial library to the Slave but the problem is that the receiver doesn't appear to be getting anything rather than showing what it appears to be "random" values when I'm uploading new code to it. I'll attach both codes below.

// Master COM9

#include <SoftwareSerial.h>
SoftwareSerial ArduinoSlave(2,3);



int sensor = A0;
int val=0;
int map_val=0;
String test;

void setup(){
pinMode(sensor,INPUT);
Serial.begin(9600);
ArduinoSlave.begin(9600);
}




void readSensor(){
val=analogRead(sensor);
map_val=map(val,400,725,0,180);
Serial.print("Sensor 1: ");
Serial.print(map_val);
Serial.println(" ");
ArduinoSlave.print(map_val);
}

void loop(){
 
readSensor();//Read command from monitor

}

-------------------------------------------------------------

//SLAVE COM8
#include <SoftwareSerial.h>
SoftwareSerial ArduinoMaster(2,3);
String msg;
int val;

void setup(){
Serial.begin(9600);
ArduinoMaster.begin(9600);   
}

void loop(){
//readSerialPort();

while(ArduinoMaster.available()){
  delay(10);
  val=ArduinoMaster.read();
  /*if(ArduinoMaster.available()>0){
   
  }*/
  ArduinoMaster.flush();
}

Serial.print("Master sent : " );
Serial.println(val);
ArduinoMaster.print(val);
 

}
/*void readSerialPort(){
while (ArduinoMaster.available()) {
delay(10);
if (ArduinoMaster.available() >0) {
char c = ArduinoMaster.read();  //gets one byte from serial buffer
msg += c; //makes the string readString
}
}
ArduinoMaster.flush();
}*/

Note: The string on the receiver code was a previous test using a keyboard to type words on the master and get them on the slave that worked perfectly. I am now trying to read sensor values with the Int variable

PaulS

Ifyousenddatainacontinuousstreamhowdoyouthinkthereceiverisgoingtobeabletomakesenseofit?

You are sending the string representation of a value, with no delimiters, and expecting read() to do something meaningful with the data one character at a time. That won't ever work.

https://forum.arduino.cc/index.php?topic=396450.0

TEAC

Ifyousenddatainacontinuousstreamhowdoyouthinkthereceiverisgoingtobeabletomakesenseofit?

You are sending the string representation of a value, with no delimiters, and expecting read() to do something meaningful with the data one character at a time. That won't ever work.

https://forum.arduino.cc/index.php?topic=396450.0
That explains why it is not working, but how doesn't that work when I try sending it as an integer?

PaulS

That explains why it is not working, but how doesn't that work when I try sending it as an integer?
How are you sending "it" as an int? If your are using the Serial Monitor application to send data, the Serial Monitor app doesn't know an int from a hole in the ground. It understands how to send and receive text.

That the text looks like a human-readable value that could be stored in an int is complete irrelevant.

TEAC

How are you sending "it" as an int? If your are using the Serial Monitor application to send data, the Serial Monitor app doesn't know an int from a hole in the ground. It understands how to send and receive text.

That the text looks like a human-readable value that could be stored in an int is complete irrelevant.
I am reading the value from the sensor and supposedly printing it on the monitor created using the SoftwareSerial library and on the slave I get the information and print it to show that I am actually receiving the data. I am not so sure if I have to use any delimiter giving the fact that I'm only reading the sensor and sending the value straight away, I'm not storing the values in any array

PaulS

Quote
I am reading the value from the sensor
on the master...

Quote
and supposedly printing it on the monitor created using the SoftwareSerial library
The SoftwareSerial library creates a software serial port, not a monitor. You are print()ing, not write()ing, so a value like 485 becomes '4', '8', and '5'. The receiver then receives 3 characters, none of which has a value of 485.

You send the value over and over, with no delimiters, so the receiver sees
"485485485485486486486487...". How is it supposed to know when to stop reading and start using data from that stream? Apparently, you missed the meaning of my first sentence in my first reply, where I made it very difficult to decipher the stream of characters because I left out all the delimiters we normally expect to see in communications.

Quote
I am not so sure if I have to use any delimiter
Perhaps you'll rethink that.

Go Up