Hello, i'm currently working on a project on Arduino UNO, dynamometer and an inductive sensor. Everything should work like this: when inductive sensor senses anything, at the same time data from dynamometer shows on serial monitor, number of detections by inductive sensor also is displayed. I connected inductive sensor through transoptor to arduino, because it works at 6-36V(I will show the picture below on how it is connected). Dynamometer is connected through RS232 to TTL converter to virtual rx and tx on arduino. I wrote a software and everything seems to be working, data is read from dynamometer only when something is detected by inductive sensor, however this data is not always correct. I also have a display on dynamometer so i can tell. Let's say there is 0,5Nm on dynamometer display, when i put metal in front of inductive sensor monitor serial data on computer is showing 0,0Nm, but if i do this 3-4 times, monitor serial data on computer starts to show 0,5Nm, sometimes it catches the same value from dynamometer and sometimes not so much. I thought that maybe it's the issue with some delay, but there is another problem - sometimes some random value like 50, or 20, or 5 is showing on serial monitor on laptop while there is completely different value on dynamometer. So to sum up, it's kinda working, but there is some noise perhaps? I've tried many things and nothing seems to correct the reading. Im at dead end, i really would appreciate anyone suggestions. Here is the code that i'm using:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(4,5); //rx, tx
#define InductiveSensor 13 //inductive sensor pin
String str;
float x;
int sensorCounter = 0;
int currentSensorState = 0;
int previousSensorState = 0;
bool label = true;
String datalabel1 = "Number of detections";
String datalabel2 = "Dynamometer value";
void setup()
{
Serial.begin(9600);
mySerial.begin(4800);
pinMode(InductiveSensor, INPUT);
}
void loop()
{
while(label) {
Serial.print(datalabel1);
Serial.print(",");
Serial.println(datalabel2);
label=false;
}
currentSensorState = digitalRead(InductiveSensor);
if (currentSensorState != previousSensorState) {
if (currentSensorState == LOW) {
sensorCounter++;
Serial.print(sensorCounter);
Serial.print(",");
if(mySerial.available()) {
str = mySerial.readStringUntil('\n');
x = mySerial.parseFloat();
Serial.println(mySerial.parseFloat());
delay(5);
}
} else {
}
delay(10);
}
previousSensorState = currentSensorState; //zliczanie liczby obrotow
}
About this part:
if(mySerial.available()) {
str = mySerial.readStringUntil('\n');
x = mySerial.parseFloat();
Serial.println(mySerial.parseFloat());
delay(5);
}
I didn't use simple Serial.println(mySerial.read()) because it showed data in ascii format, so i had to convert it. I would also like to mention that there seems to be no noise if i use Serial.write(mySerial.read()) - also i don't need to convert that one - but i don't know how to apply that to my code so that only one verse of data is being read on serial monitor.
Here is the display from my serial monitor:
Number of detections,Dynamometer value
1,0.00
2,0.00
3,0.00
4,0.00
5,0.00
6,0.00
7,0.00
8,0.00
9,0.67 - value detected after 8 inductive sensor activations, why?
10,0.60
11,0.65
12,0.80
13,0.75
14,0.80
15,75.00 - noise?
16,1.10
17,1.15
18,5.00 - noise?
19,1.20
20,1.00
21,1.15
22,1.05
23,1.05
24,1.05
25,1.40
26,1.30
27,1.25
28,1.30
29,1.20
30,1.45
31,1.00
32,11.15 - noise?
33,0.95
Here is how i connect inductive sensor:

