The sensor isn't perfect, if you aim even a really, really good sensor (Better than the, um, 'classic' Sharp IR sensors) at the same spot on the wall and take a few readings, there will be some variation. If you discard some number of least significant bits, it's possible to get the same reading every time, but your readings will be more granular. You should probably keep the maximum precision, and then try to fix errors in software (i.e., change your data so that an almost straight line really is straight).
What sensor are you using, and what range of measurement do you expect? The output of the sensor is very much nonlinear. This graph (from the datasheet) compares the output voltage on a linear scale with the distance:
You'll notice that it looks a lot like the graph of y=1/x, i.e volts=k(1/distance). This can be used to get a decent first approximation, but division on an Arduino is expensive. You'll have better luck calibrating the sensor and storing the voltage/distance pairs in a look-up table (in program memory, of course). This one comes calibrated from the factory such that a measurement at precisely 24cm measures to 24cm +/- 3cm. Unfortunately, they don't give you a way of knowing what the voltage at 24cm should be except by squinting at this graph.
The sensor will have higher precision at certain ranges. Imagine that there is a random variation of, say, +/- 250mV in your readings (Gaussian if you like, but random is easier). It's hopefully a lot smaller than that, but it makes it easy to visualize. The variation means that your readings with this sensor at, say, 50cm or 70cm will vary over 20 or 30cm, but measurements at 15cm should be within a few cm. This sensor is rated for 10-80cm, but you'll get more accurate readings if you only trust it for 10-25cm or so. If you're controlling the robot, you should be able to move to these distances and get the best readings.
The sensor draws current in large bursts and is probably mounted on a fairly long cable. A capacitor will help stabilize the readings. Don't jam it into the JST; solder it to the PCB on the back. It's drawing a lot of current and it's pretty slow, so small capacitances traditionally used for decoupling (0.1uF) probably won't work. I'd use a 10uF 1206 or 1210 ceramic SMD cap in parallel with a 100uF electrolytic. If you find that high frequency noise is still present, add a 0.1uF 1206 on top of the 10uF. Here's a picture of the locations for soldering (original image from Sparkfun):
You could also try adding a small capacitance on the Vo line to smooth the output. You should experiment to see whether or not that helps. I'd keep it below 100pF, starting at around 10pF. This will create a moving average of the readings in hardware. More circuitry (a series resistor) would enhance the effect, post a comment or another question if you want to build a more complex low-pass filter system for this purpose. Note: The Sharp sensor may not handle driving into a large capacitance well, and might balk or break if asked to change the voltage quickly. It's also possible that it's a single-sided output, and might require a resistor to ground to discharge the capacitor if you add more capacitance than is currently present in the parasitics.
Twisting the cables together will help reduce noise coupled on from external sources like 60Hz mains. Keeping them short will also help mitigate noise.
Why do you need it to be sent as a string?
Just send a byte value and treat it as so in the Arduino. The serialPort1.Write
method will likely have different overloads for various formats including byte.
If you need to represent a value >255 then you can send it over multiple bytes and concatenate into e.g. an int at the Arduino.
NOTE - you seem to have changed the code in your question since the below was written, but I'll leave it there as the concept is still valid.
EDIT
At the moment you are sending in ASCII format from the PC, so 0 - 0x30, 1 = 0x31 etc. To send in plain binary check the overloads for the method.
Try putting something like:
private void button1_Click(object sender, System.EventArgs e)
{
// Say we want to send 23456 (0x5BA0)
//serialPort1.Write(200); // 200 is "send value" command
//serialPort1.Write(91); // 100 (0x5B) is your upper byte value
//serialPort1.Write(160); // 160 (0xA0) is your lower byte value
// this uses the serialPort1.write overload correctly
// 1st byte is command, 2nd and 3rd are upper and lower bytes of int
byte[] data = new byte[] {200, 91, 160};
serialPort1.Write(data, 0, 3); // 0 is the offset and 3 is the size of array
}
and see if it is recieved correctly at the Arduino (do not read it as a string, e.g. if incoming byte == 200)
Something like the below should work - you will have to adjust accordingly to make it do whatever it is you are trying to do, this is just an example of transferring bytes:
if (incomingByte == 200) {
upper_byte = (unsigned char)Serial.read();
lower_byte = (unsigned char)Serial.read();
// full_value should be 23456
int full_value = (upper_byte << 8) + lower_byte; // reconstruct int
}
Notes:
You can write code to split an int into the two bytes (I just put an example "test value")
Again, there may be overloads for this. Also you can use any multiple byte receive capability in the Serial.read routine.
Best Answer
You're not terminating your string properly.
The Arduino code is looking for 0x31 0x00.
The PC is sending just 0x31.
You need to send the 1 and a terminating character (say a carriage return) and look for that on the Arduino. That would, for example, be sending 0x31 0x0d, and the Arduino would look for 0x0d as the terminating character.
Or you could tell the PC to transmit a 1 followed by a null character 0x00. I don't know C# but if it follows standard escaping procedures you may be able to send something like "1\000".