Smoothing out a signal

I have a distance sensor to measure whether an object is in one of two positions or not there at all. Basically the objects are passing the sensor and I want to count them. The sensor is a little noisy so I have set the code up to take three readings and average them then compare the first value to the last value to make sure it has consistent readings (ie isn’t in a transition phase). This works OK, but I don’t think it is a ‘neat’ solution. It needs a delay to keep it working and sometimes it loops a few times before getting a good value. Is there a better way to do this? I want to add a second sensor but if I use the same code again I suspect the delays and loops will cause a few issues.

... 
int difference = 25; 
  while (difference > 20) {
    PSD1value = analogRead(PSD1pin); //read signal from PSD1 sensor
   PSD1firstvalue = PSD1value;
   delay(50);
   PSD1value += analogRead(PSD1pin);
   delay(50);
   PSD1value += analogRead(PSD1pin);
   PSD1value /= 3;
   
   int difx = PSD1firstvalue - PSD1value;
   difference = abs(difx);
    }
     
     
   if (PSD1value >= PSD1MID){ if (PSD1state == 3) {PSD1state = 1;}} // target 1
   else if (PSD1value >= PSD1LOW && PSD1value < PSD1MID ){ if (PSD1state == 3) {PSD1state = 2;}} //target 2
   else {PSD1state = 3;} //gap

   if (PSD1laststate != PSD1state){
   if (PSD1state == 1) {   Serial.print("Target 1   "); }
   if (PSD1state == 2) {   Serial.print("Target 2   "); }
   if (PSD1state == 3) {   Serial.print("GAP  "); }
   
   Serial.println(PSD1value);
   }
   PSD1laststate = PSD1state;
...

So what is this sensor? How far away is it from the Arduino? How have you wired it up?

Without those details help is just guess work.

Its a sharp GP2D120XJ00F wired to an analogue pin giving a 0-5v ish signal depending on the distance. The output is something like the image below, and I want to count the two different peaks. The issue is that the signal is a little noisy and and I get a range of values during the transition phases between the three different levels. Just to add, I'm not really looking to modify the hardware, just some clever way of reading the signal I do have to count the peaks. They are approx 1 per second.

The waveform that you show appears to last 8 seconds. During that time four square waves have magnitudes with a low level of 90 and three high levels of 200, 260, and 460. From this, I assume the three levels are in three bins :

bin 0 : 0 to 140 bin 1 : 170 to 320 bin 2 : 380 to 700

since the signal is very slow, many analog reading can be taken when a bin is first seen. An algorithm will sample the waveform 100 times per second and collect bin data. A software algorithm will evaluate the data and use logical criteria to evaluate the confidence level about the pulse.

You are not limited to averaging three readings, take a hundred reading per second to provide a statistical confidence level of 99% that the result must be stable.

Is there something wrong with an appropriately sized cap or RC filter?

Maybe I shouldn't have mentioned 'noise', that's a bit of a red herring, is more minor fluctuations and its actually a very low signal to noise ratio. The real issue is when the objects come into range of the sensor. There is a transition period where I get a few mid numbers that I basically want to ignore. Maybe a hysteresis type algorithm will do it.