Hi, start playing with SHARP 2Y0A02 F 55 IR sensor and now I have problem:
cant get smooth distance. The distance does not change but in serial monitor distance jumping.
For example if distance is 10 in serial monitor distance jumping interval from 8 to 12
How can I avoid jumping and get real distance?
Code:
#define sensor A4 // Sharp IR GP2Y0A41SK0F (4-30cm, analog)
void setup() {
Serial.begin(9600); // start the serial port
}
void loop() {
// 5v
float volts = analogRead(sensor)*0.0048828125; // value from sensor * (5/1024)
int distance = 13*pow(volts, -1); // worked out from datasheet graph
delay(1000); // slow down serial port
if (distance <= 30){
Serial.println(distance); // print the distance
}
}
The SECOND thing to do is to simply print the value read from the sensor. If that is not (relatively) stable, for a given distance, then no amount of coding is going to correct the problem.
The FIRST thing to do is to learn how to properly post a link. Just underlining text does NOT make it a link. The icon to the left of the X2 icon does.
First of all, you should discard the first reading, second you should take multiple reading and calculate an average:
#define NUM_SAMPLES 5 //Cannot exceed 64
int readSensor()
{
unsigned int res = 0;
analogRead(sensor); //Discard
for (byte i = 0; i < NUM_SAMPLES; i++)
{
delay(2); //Find what works best, may be less or more
res += analogRead(sensor);
}
return res / NUM_SAMPLES;
}