Wrong Results!

Hi guys, I am using my Board with ultrasonic maxbotix sensor, in order to calculate distance ! but the thing is I keep getting wrong results, it either adds inches or takes inches it reads a let's say 44 inches distance as 120 inches or more and sometimes it reads it as 10 inches and so on, and when there is no distance it starts reading from 7.44, the reading also changes a lot though the sensor is put in the same unchanged distance ! any help?

My Code :
int configPin = 13; //Set the sonar Calibration Pin
void setup() { //begin of program
Serial.begin(9600); //serial baud rate 9600
pinMode(configPin,OUTPUT); //make Calibration pin output
}
void loop(){ //looping of program
digitalWrite(configPin,HIGH); //raise the reset pin high
delay(120); //start of calibration ring
float sensorvalue = analogRead(0); //get analog sensor value from pin 0
float inchvalue = (254.0/1024.0) 2.0 sensorvalue; //convert to inches
//float cm=inchvalue*2.54;
//Serial.print("Sensed a-d value:"); //print a-d text
//Serial.println(sensorvalue); //print a-d value
Serial.print("Inch value="); //print inch text
Serial.println(inchvalue);
//Serial.print("cm=");
//Serial.println(cm);
delay(1000); //optional delay 1 second
digitalWrite(configPin,LOW); //turn off Calibration ring and sensor
delay(1000); //delay 1 second
}

Show us the wiring and your code. If you use some libraries provide links to the ones you used. Which maxbotix sensor are you using?

And put your code into code tags, else it may get mangled by the forum system.

I am using UltraSonic MaxBotix LV-Ez1, I have connected pin A0 on arduino to the Analog pin on the sensor, and the same to the GND, 5V (voltage) and pin 13 on the board with the RX pin on the sensor, and the board is connected to the pc via USB!..
and am not using any libraries , just the code I've posted
thanks

Hi guys, I am using my Board with ultrasonic maxbotix sensor, in order to calculate distance ! but the thing is I keep getting wrong results, it either adds inches or takes inches it reads a let's say 44 inches distance as 120 inches or more and sometimes it reads it as 10 inches and so on, and when there is no distance it starts reading from 7.44, the reading also changes a lot though the sensor is put in the same unchanged distance ! any help?

My Code :
int configPin = 13; //Set the sonar Calibration Pin
void setup() { //begin of program
Serial.begin(9600); //serial baud rate 9600
pinMode(configPin,OUTPUT); //make Calibration pin output
}
void loop(){ //looping of program
digitalWrite(configPin,HIGH); //raise the reset pin high
delay(120); //start of calibration ring
float sensorvalue = analogRead(0); //get analog sensor value from pin 0
float inchvalue = (254.0/1024.0) 2.0 sensorvalue; //convert to inches
//float cm=inchvalue*2.54;
//Serial.print("Sensed a-d value:"); //print a-d text
//Serial.println(sensorvalue); //print a-d value
Serial.print("Inch value="); //print inch text
Serial.println(inchvalue);
//Serial.print("cm=");
//Serial.println(cm);
delay(1000); //optional delay 1 second
digitalWrite(configPin,LOW); //turn off Calibration ring and sensor
delay(1000); //delay 1 second
}

Duplicate topics merged.

CROSS POSTING WASTES TIME - DON'T DO IT.

I was going to remove one of the posts !!! XD

GSim:
I was going to remove one of the posts !!! XD

Instead, you can update one of them to make the code readable, I.E Code tags and indentations.

GSim:
Hi guys, I am using my Board with ultrasonic maxbotix sensor, in order to calculate distance ! but the thing is I keep getting wrong results, it either adds inches or takes inches it reads a let's say 44 inches distance as 120 inches or more and sometimes it reads it as 10 inches and so on, and when there is no distance it starts reading from 7.44, the reading also changes a lot though the sensor is put in the same unchanged distance ! any help?

I don't know whether you're suffering from floating point rounding, or just a bug in your conversion.

Can you explain the calculation you intend to perform to get from a raw analog measurement to a distance value in whatever units you want it in? Preferably with a worked example.