Sharp Sensor giving odd numbers

Hey guys, I’m hoping someone can help me out with this issue…

I’m using a Sharp IR sensor to detect the distance between a changing object. The goal is for a neopixel LED strip to change colours the farther or closer the object gets.

I’ve got down the code that would create the change.
The only problem, is that whenever there is -NO- object in sight the sensor begins to ‘‘see’’ an object near.

In my code I have written so that if an object is closer, the percentage is displayed in serial.

If its closer it could look something like this

45
44
36
46
47
48
48
48
48

as soon as the object is taken away from it’s path, and there is -NOTHING- within the range limit (which is 150CM) it begins to detect like so:

21
20
19
11
11
11

is there maybe something wrong in how I’m declaring my range limit?

#include <Adafruit_NeoPixel.h>
#include <EasingLibrary.h>
#include <avr/power.h>

#define PIN            11
#define NUMPIXELS      60
#define SPEAKER         8

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int sharpIR = 0;                                              
int maxDistance = 150;  
int myColor = 0;  
int limitValue = 20;
float colorMultiplier = 2.55;
int isOff = 0;
int isRed = 0;
int delayTime = 10;
int fading = 0; 
int fadedOut = 0; 
int fadingSteps = 20;    
int actualRed;
int actualGB;
int OnOff = 0;
int apagar = 0;
int apagado = 0;
int apagarLimit = 60;


void setup(){

  pixels.begin();
  pixels.setBrightness(255);
  pixels.show();

  colorWipe(pixels.Color(0,0,0)); 

  Serial.begin(9600);
  Serial.println("Inicio App");
}

//////////////*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*///////////////////
void loop()
{
  if (fading == 0){

    int distance = getDist2(); 
    int cmDistance = 9462/(distance - 16.92);
    int distPerc = ((cmDistance-limitValue) * 100) / (maxDistance-limitValue) ;

    Serial.println("Distance Percentage:");
    Serial.println(distPerc);
    Serial.println("----------------------");

    if(distPerc > 100 && isOff == 0){

      //myColor = distPerc * colorMultiplier;
      apagado = 0;
      OnOff = 0;
      fadeOut();
      //colorWipe(pixels.Color(0,0,0));
      Serial.print("-------off------Maximum Distance, turn off ");
      Serial.println(fading);
      //do nothing, Distancia maxima superada agagar
    } 
    else if (distPerc > 0 && distPerc <= 100){
      //fadeIn();
      apagado = 0;
      OnOff = 0;
      myColor = distPerc * colorMultiplier;

      isOff = 0;

      colorWipe(pixels.Color(255,myColor,myColor));     

    } 
    else if (distPerc <= 0 && apagado == 0 ){
      ////PITAR
      if (OnOff == 5){
        colorWipe(pixels.Color(255,0,0));
        OnOff = 0;
      }
      else{
        OnOff++;
        apagar++;
        colorWipe(pixels.Color(0,0,0));
        tone (SPEAKER,1000,30);
        if(apagar == apagarLimit){
          apagar=0;
          apagado = 1;
        }
      }
    }
    delay(10);
  }
}
//////////////*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*///////////////////
void fadeOut (){
  Serial.println("----------fadeOut---------");

  fading = 1;
  fadedOut = 1;

  int red =  255 / fadingSteps;
  int greenBlue = myColor / fadingSteps;

  isOff = 1;

  for (int i = fadingSteps; i >= 0; i--){
    actualRed = red*i;
    actualGB = greenBlue*i;
    colorWipe(pixels.Color(actualRed,actualGB,actualGB));

    if (i== 0){

      fading = 0;
      Serial.print("faded!!!!! ");
      Serial.println(fading);

    }   

    delay(delayTime);
  }


}

void fadeIn (){
  Serial.println("----------fadein---------");


void colorWipe(uint32_t c) {
  for(int i=0; i<pixels.numPixels(); i++) {
    pixels.setPixelColor(i, c); 
  }
  pixels.show();
}


int getDist()
{
  int distRead = analogRead(sharpIR);
  int distAvg = distRead;
  return distAvg;
}

int getDist2()
{
  int distRead;
  int distAvg = distRead;
  int averaging = 0;             //  Holds value to average readings
  for (int i=0; i<5; i++) {
    distRead = analogRead(sharpIR);
    averaging = averaging + distRead;
    delay(0);      // Wait 55 ms between each read
    // According to datasheet time between each read
    //  is -38ms +/- 10ms. Waiting 55 ms assures each
    //  read is from a different sample
  }
  distRead = averaging / 5;      // Average out readings
  return(distRead);              // Return value
}

I must also add, that whenever there is an object NEAR the UP CLOSE limit of the sensor,
I receive also similar numbers like

21
20
19
11
11
11

as soon as the object is taken away from it's path, and there is -NOTHING- within the range limit (which is 150CM) it begins to detect like so:

Yes it will. It is detecting noise. The only way round it is to ignore the very small numbers, they are ambiguous anyway so you should not be acting on them.

I’ve figured out the problem…
what needs to happen is to average out the values to create a steadier response.

#include <Adafruit_NeoPixel.h>
#include <EasingLibrary.h>
#include <avr/power.h>
#include <SharpIR.h>

#define PIN            11
#define NUMPIXELS      60
#define SPEAKER         8
#define sharpIR         0

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int maxDistance = 100;  
int myColor = 0;  
int limitValue = 20;
float colorMultiplier = 2.55;
int isOff = 0;
int isRed = 0;
int delayTime = 10;
int fading = 0; 
int fadedOut = 0; 
int fadingSteps = 20;    
int actualRed; 
int actualGB;
int OnOff = 0;
int apagar = 0;
int apagado = 0;
int apagarLimit = 60;

float sensorValue, inches;
int currDistance, prevDistance, offDelay;
bool IsFaded;

SharpIR sharp(sharpIR, 25, 93, 20150);


void setup(){

  pixels.begin();
  pixels.setBrightness(255);
  pixels.show();

  colorWipe(pixels.Color(0,0,0)); 

  Serial.begin(9600);
  Serial.println("Inicio App");
  
  IsFaded = false;
  
}


void loop()
{

  int dis = sharp.distance();
  currDistance = dis;
  
  delay(100);
  
  if (dis >= 20 && dis <= 160){
     Serial.print("Centimeters: ");
     Serial.println(dis);
     if((currDistance >= 20 || currDistance >= 30) && (prevDistance <= 25 && prevDistance > 0)) {
       // Pitar;
       Serial.println("Pitar");
       tone (SPEAKER, 1000, 30);
       if (offDelay <= 0){
         offDelay = 100;
       }else{
         offDelay -= 10;
       }
       flick(offDelay);
       prevDistance = dis;
     }else if(currDistance >= 145 && prevDistance >= 140) {
       // Apagar
       Serial.println("Apagar");
       if(!IsFaded){
         fadeOut();
       }
       prevDistance = dis;
     }else{
       prevDistance = dis;
       float color = dis * 0.66;
       colorWipe(pixels.Color(255, color, color));
     }
  } else{
      Serial.println("Not in range");
      prevDistance = 0;

      colorWipe(pixels.Color(0, 0, 0));

  }
 

}
void flick(int d){
  if (d % 20 == 0){
    colorWipe(pixels.Color(255, 0, 0));
  } else{
    colorWipe(pixels.Color(255, 255, 255));
  }
}



void fadeOut (){

  fading = 1;
  fadedOut = 1;

  int red =  255 / fadingSteps;
  int greenBlue = myColor / fadingSteps;

  isOff = 1;

  for (int i = fadingSteps; i >= 0; i--){
    actualRed = red*i;
    actualGB = greenBlue*i;
    colorWipe(pixels.Color(actualRed,actualGB,actualGB));

    if (i== 0){
      fading = 0;
      Serial.print("faded!!!!! ");
      Serial.println(fading);
    } 
    delay(delayTime);
  }
  
  IsFaded = true;
}


// NEOPIXEL METHOD: fill the dots one after the other with a color
void colorWipe(uint32_t c) {
  for(int i=0; i<pixels.numPixels(); i++) {
    pixels.setPixelColor(i, c); 
  }
  pixels.show();
}


int getDist()
{
  int distRead = analogRead(sharpIR);
  int distAvg = distRead;
  return distAvg;
}

int getDist2()
{
  int distRead;
  int distAvg = distRead;
  int averaging = 0;             //  Holds value to average readings
  for (int i=0; i<5; i++) {
    distRead = analogRead(sharpIR);
    averaging = averaging + distRead;
    delay(55);      // Wait 55 ms between each read
    // According to datasheet time between each read
    //  is -38ms +/- 10ms. Waiting 55 ms assures each
    //  read is from a different sample
  }
  distRead = averaging / 5;      // Average out readings
  return(distRead);              // Return value
}

for anyone trying to solve a similar issue,

i’ve also used the SharpIR library.

hey, i've been trying to replicate this project except using Sharp 1080 IR sensor..

i'm new to this arduino game, can you give some guidance on the easing cause I'm also getting lots of bouncing values from the sensor.