I'm trying to use a sensor that has two inputs while also getting inputs from the serial monitor
I'm using a library of <DFRobot_TFmini.h> and I want to set the initial settings using a serial monitor but I can't figure out how to use both the sensor and the serial monitor as inputs.
I'm intrigued. What sensor? Please state the make and model and if you can a link to where you bought it.
I'm sure your sensor has outputs of some kind that also need handling.
a7
If you need multiple serial ports there are Arduino boards that have more than one hardware serial port. Like the Mega or Mega Pro that has 4 hardware serial ports. If you just need 1 extra serial port the are software serial libraries like SoftwareSerial. It is possible to use more than 1 software serial port, but it is not recommended.
Its
TFmini mini lidar
it returns distance and strength data by using this code
#include <DFRobot_TFmini.h>
SoftwareSerial mySerial(8, 7); // RX, TX
DFRobot_TFmini TFmini;
uint16_t distance, strength;
void setup() {
Serial.begin(9600);
Serial.print("asdf");
TFmini.begin(mySerial);
}
void loop() {
if (TFmini.measure()) {
distance = TFmini.getDistance();
strength = TFmini.getStrength();
Serial.print("Distance = ");
Serial.print(distance);
Serial.println("cm");
Serial.print("Strength = ");
Serial.println(strength);
}
delay(100);
}
What I want to make is to use serial monitor to setup the inital sensing conditions and then switch the serial communication channel to sensor inputs. Would I need multiple serial communication ports?
Do you need to add
mySerial.begin(9600);
to setup() to initialize the software serial port? That is assuming 9600 is the baud rate of the sensor. Is 9600 the baud rate?
It looks like you posted the linrary example and it does not have a begin for SoftwareSerial. The library takes care of that.
Yes it is.
The test code
#include <DFRobot_TFmini.h>
SoftwareSerial mySerial(8, 7);
DFRobot_TFmini TFmini;
uint16_t distance, strength;
void setup() {
Serial.begin(9600);
Serial.print("asdf");
TFmini.begin(mySerial);
}
void loop() {
if (TFmini.measure()) {
distance = TFmini.getDistance();
strength = TFmini.getStrength();
Serial.print("Distance = ");
Serial.print(distance);
Serial.println("cm");
Serial.print("Strength = ");
Serial.println(strength);
}
delay(100);
}
runs fine, but while making additional functions to it, I wanted to set the sensing conditions using serial monitor in Arduino IDE 2.1.1. And this is what I've tried
#include <DFRobot_TFmini.h>
SoftwareSerial mySerial(8,7);
SoftwareSerial Setup(0,1);
DFRobot_TFmini TFmini;
uint16_t strength;
int j, k = 0;
int n;
float x,y,z;
float R, r, ang, w, tot_ang, und_ang;
// sampled along radius = R
void setup() {
Serial.begin(9600);
Serial.println("Data required");
Serial.print("R = ");
R = Serial.write(Setup.read());
Serial.println(R);
Serial.print("w = ");
w = Serial.write(Setup.read());
Serial.println(w);
Serial.print("Sensor moving along the radius of ");
Serial.println(R);
Serial.print("Sensor moving on the wheel radius of ");
Serial.println(w);
tot_ang = (2*PI*R)/w;
und_ang = 2;
n = tot_ang/und_ang;
TFmini.begin(mySerial);
}
void loop() {
if (TFmini.measure()) {
r = TFmini.getDistance();
}
if (n < k) {
x = (R-r)*cos(radians(k*und_ang));
y = (R-r)*sin(radians(k*und_ang));
k++;
Serial.print("X = ");
Serial.print(x);
Serial.print("Y = ");
Serial.print(y);
}
delay(100);
}
My initial thought was that the port 0, 1 returns data to the uno processor so in order to use the Serial monitor, I used SoftwareSerial Setup(0,1); but it doesn't seem to do the trick
There is nothing that you can send to the TFmini sensor. It only has 4 functions: begin(), measure(), getDistance() and getStrength(). Those function only return values. The sensor is read only. Look at the DFRobot_TFmini.h file.
Those calculations would be done on the Arduino with values returned from the sensor.
The values of variables to use for the calculation could be entered using the parseFloat() function of Serial. Now I am not real familiar with the parseFloat() function, but below shows how it may be used. Untested.
#include <DFRobot_TFmini.h>
SoftwareSerial mySerial(8,7);
DFRobot_TFmini TFmini;
uint16_t strength;
int j, k = 0;
int n;
float x,y,z;
float R, r, ang, w, tot_ang, und_ang;
// sampled along radius = R
void setup() {
Serial.begin(9600);
TFmini.begin(mySerial);
Serial.println("Data required");
Serial.print("R = ");
R = Serial.parseFloat();
Serial.println(R);
Serial.print("w = ");
w = Serial.parseFloat();
Serial.println(w);
Serial.print("Sensor moving along the radius of ");
Serial.println(R);
Serial.print("Sensor moving on the wheel radius of ");
Serial.println(w);
tot_ang = (2*PI*R)/w;
und_ang = 2;
n = tot_ang/und_ang;
}
void loop() {
if (TFmini.measure()) {
r = TFmini.getDistance();
}
if (n < k) {
x = (R-r)*cos(radians(k*und_ang));
y = (R-r)*sin(radians(k*und_ang));
k++;
Serial.print("X = ");
Serial.print(x);
Serial.print("Y = ");
Serial.print(y);
}
delay(100);
}
You don't have a need for a second SoftwareSerial; you can just read from the serial port.
if(Serial.available() >0)
{
// read incoming byte
char ch = Serial.read();
// echo it back
Serial.write(ch);
}
I thinkt this is the solution haha thanks
Oh, but I can't seem to read it as a form of float, is there any recommended way to read a value from the serial monitor and store it in a variable it the form of float?
Did you read my response or am I wasting my time?
Did you try the code in post #8?
The Serial.parseFloat() function reads a float entered in the serial monitor. I linked the reference for that function in that post.
I've tried with the parseFloat() but I can't seem to make it wait for the input and just set the values as 0 and move on.
Post your full sketch that you used to test the parseFloat() function
#include <DFRobot_TFmini.h>
SoftwareSerial mySerial(8,7);
DFRobot_TFmini TFmini;
uint16_t strength;
int j, k = 0;
int n;
float x,y,z;
float R, r, ang, w, tot_ang, und_ang;
// sampled along radius = R
void setup() {
Serial.begin(9600);
Serial.println("Data required");
Serial.print("R = ");
if (Serial.available()) {
float R = Serial.parseFloat();
Serial.println(R);
}
Serial.print("w = ");
if (Serial.available()) {
w = Serial.parseFloat();
Serial.println(w);
}
Serial.print("Sensor moving along the radius of ");
Serial.println(R);
Serial.print("Sensor moving on the wheel radius of ");
Serial.println(w);
tot_ang = (2*PI*R)/w;
und_ang = 2;
n = tot_ang/und_ang;
TFmini.begin(mySerial);
}
I am prepared to bet that you have a line ending set in the Serial monitor. Set it to use no line ending and try the sketch again
It was but doing the same when I changed it to no line ending haha...
Describe exactly what happens, step by step
What prompts do you see, what do you enter (if anything), what output do you see ?
Sure, here's a shortened version using serial communication in 3 lines:
- Initialize serial communication and set baud rate:
cppCopy code
void setup() { Serial.begin(9600); }
- Send data via serial:
cppCopy code
void loop() { Serial.println(analogRead(A0)); }
- Receive data via serial:
cppCopy code
void loop() { if (Serial.available()) { String data = Serial.readString(); } }
Please note that these shortened versions assume that you have already defined the necessary pin connections and included the required libraries in your code.
So before I enter anything into the Serial Monitor, this pops up and R, w values are set as 0.
The setup code is
#include <DFRobot_TFmini.h>
SoftwareSerial mySerial(8,7);
DFRobot_TFmini TFmini;
uint16_t strength;
int j, k = 0;
int n;
float x,y,z;
float R, r, ang, w, tot_ang, und_ang;
// sampled along radius = R
void setup() {
Serial.begin(9600);
Serial.println("Data required");
Serial.print("R = ");
if (Serial.available()) {
float R = Serial.parseFloat();
Serial.println(R);
}
Serial.print("w = ");
if (Serial.available()) {
float w = Serial.parseFloat();
Serial.println(w);
}
Serial.print("Sensor moving along the radius of ");
Serial.println(R);
Serial.print("Sensor moving on the wheel radius of ");
Serial.println(w);
tot_ang = (2*PI*R)/w;
und_ang = 2;
n = tot_ang/und_ang;
TFmini.begin(mySerial);
}
Your code does not wait until there is Serial data available. If none is available then it simply moves on and because setup() only runs once the code depending on Serial data being available is never executed