I have two Arduino Unos, one of which is connected to a HC-05 bluetooth device and a temperature reader, the other has just a HC-05 bluetooth and is expected to output temperature readings in serial monitor.
This is first device:
#include <SoftwareSerial.h>
const int tempPin = 1;
SoftwareSerial BTSerial(10, 11);
void setup() {
BTSerial.begin(38400);
Serial.begin(9600);
}
void loop() {
int value = analogRead(tempPin);
float mv = value * (3300/1024);
float x = (mv - 500)/100;
Serial.print("Temperature is: ");
Serial.println(x);
BTSerial.print(x);
delay(5000);
}
This is the second device:
#include<SoftwareSerial.h>
void setup() {
BTSerial.begin(38400);
Serial.begin(9600);
}
void loop() {
if(BTSerial.available())
{
float x = BTSerial.read();
Serial.println(x);
}
}
The first program is able to give me accurate readings (around (15 degress Celsius) but the second program gives readings between 40 - 60 *C (as well as that it spits out 5 different readings between delay intervals). Why is that? How should I change this so both devices output the same readings
1 Answer 1
Two problems:
Your first program sends an ASCII representation of the temperature with no line ending. So after a few readings you get something like:
20.2520.2520.5021.0021.25
etc. You need some line endings in there to break them up.
Secondly your second program reads one byte at a time as a number (float) and prints it - so would get something like:
50.00
48.00
46.00
50.00
53.00
50.00
48.00
... etc ...
You need to, in association with sending line endings in your first program, read each character of a single line either into a string and then parse that as a float (with dtostrf()
) or directly into a float using a state machine.
You can read more about working with serial here and working with a number reading state machine here.
-
Hi, thanks for the response! Just a quick question; by line ending do you mean something along the lines of a "println()" statement or something else? thanks for the tips, will look into working with serial nowwalkman118– walkman1182018年03月11日 18:58:50 +00:00Commented Mar 11, 2018 at 18:58
-
Hi, I just read through your article. Okay I understand, still wrapping my head around the code but I understand the theory behind it. Thank you, this helps a lot!walkman118– walkman1182018年03月11日 19:09:30 +00:00Commented Mar 11, 2018 at 19:09
Explore related questions
See similar questions with these tags.