URGENT: Cannot get cm values from a proximity sensor

Hello, I am building a project for school and I require the use of a proximity sensor, I’ve coded it and it comes up in the serial monitor but I would like to know how to change the values I get into a cm value so I can interpret how far away something is from the sensor.

The name of the sensor is:- Infrared Proximity Sensor - Sharp GP2Y0A21YK
and here is a pic of it

#include <SharpIR.h>
SharpIR sharp(A0, 25, 93, 1080);
int dis=sharp.distance();

void setup() 
{

 Serial.begin(9600); 
}

void loop() 
{ 
 int sensorValue = analogRead(A0);

 Serial.println(sensorValue);
 delay (500);
 
}

You need to look at the data sheet for that device. You will see a graph of voltage against distance, you will also see it is not linear and below a certain distance the readings are ambiguous.

To convert the reading into distance you need to use a look up table. This is an array with numbers representing the reading and the position in the array representing distance. You get the reading and search through the array for the closest value. When you find it the position it is in the array is a distance measurement.

If you have all the possible readings in the look up table it will be big and maybe you will want to put this into program memory.

This is quite an advanced topic for a beginner.

oh man..... I literally started working with arduino a month ago when the project began lol, could you guide me as to how I could put the look up table into the memory? Just to clarify how can I make a lookup table based on the values I get from the sensor?

I used to teach degree level at university and this was a second year project.
At the moment it is 5:30 in the morning in the UK and I am on a mobile device so there is not too much I can do to help. So you write down a list of readings for each cm distance and do some googling while I try and get some sleep.
Good luck.

Depending on the accuracy you need, put the sensor value of say each cm in an array.

When you get a reading from the sensor, start a loop that counts through the array and compares the reading to the array value. If greater than the array value, inc the count and compare until you reach an array value that is larger than the test value. You then know the distance is between the current count and the previous count.

You could the estimate the distance between.

EG, Array. { 1, 2, 3, 3.9, 4.9, 5..........}
Test value is 3.3

Start count at 0 and compare with 1, test is larger.
Inc count and compare with 2, test is larger.
Inc count and compare with 3, test is larger.
Inc count and compare with 3.9, test is smaller. Test must be between 3 and 3.9. Count is 3 so is less than 4 and larger than 3cms. .3 is approx 1/3 of 0.9 so actual value is 3.3cms.

In my EG, it worked out same as test value but this depends on the sensor values.

Weedpharma

This could be a X-Y Problem

You need to be absolute certain you need the results from the sensor in cm. Most beginners are obsessed with this sort of thing and it is not necessary. For example if you want to do something when a object is closer that 20cm then it is not necessary to convert every reading into a distance just so you can say when the distance is greater than this, then do .....

Most problems you can just work of the raw reading. For example if 20cm gives you a reading of X then simply say in your program if reading is greater than X.

If you do need a reading of distance for say a digital tape measure, then this is the wrong sensor for this sort of application. This is why we often insist in knowing what you project actually is.