I have an inclinometer sensor which I need to read out with arduino. If the sensor gets the command get---x it sends the value over serial connection.
With the serial monitor it works well when I send get---x. Now I'd like to do that in a function in may sketch like void getdata(). When I call that function the sensor should send the value.
PC is connected to arduino Serial and sensor is connected to Serial2 on my Mega.
How can I send that command in my function like the serial monitor does it? The sensor should send a 9byte string as answer like ±xxx.xxx which I need to read back on Serial to the PC.
Could someone help with a code snippet or give me a hint how to do that?
I use this sketch with my serial monitor that works
void setup() {
// initialize both serial ports:
Serial.begin(38400);
Serial2.begin(38400);
}
void loop() {
// read from port 1, send to port 0:
if (Serial2.available()) {
int inByte = Serial1.read();
Serial.write(inByte);
}
// read from port 0, send to port 1:
if (Serial.available()) {
int inByte = Serial.read();
Serial2.write(inByte);
}
}
Having several example of the returned results would help but have you tried parseFloat to see if it works?
It might stumble if you have a leading + character but then you could maybe read the results into a buffer using readBytesUntil and then start at the second character.