problem about my code (ir sensor and dc motor)

I am using IR sensor for measure speed of motor(Hz) but it doesn’t work about detection of sensor.
I try to detect my hand by pass sensor 1 time but in serial monitor show 1 or 2 or 3 times.

#define motorPin1      5                
#define motorPin2      6  
#define pd             2    // photodiode                              
#define senRead        0   
 
 int           limit = 800;  // theshold of value(read from sensor)
 int           val   =   0; 
 int           temp  =   0;
 
 unsigned long count =   0;
 unsigned long timer =   0; 
 
 void setup()    
 {  
  pinMode(motorPin1,OUTPUT);
  pinMode(motorPin2,OUTPUT);
  pinMode(pd,OUTPUT);  
  digitalWrite(motorPin1,HIGH); 
  digitalWrite(motorPin2,LOW); 
  digitalWrite(pd,HIGH);                 
  Serial.begin(9600);          
  timer = millis()+1000; 
 }  

 void loop()  
 { 
  temp = val; 
  val=analogRead(senRead);    
    
  if(val<limit && temp>limit)        
  {  
     count++;    
  }
  
    if(millis() >= timer)
  {
    Serial.print("Hertz of motor = ");
    Serial.println(count);
    timer = millis()+1000;
    count = 0;
  }  

 }

Your condition will never become true when the value will equal limit during transition from low to high.

You're better off with a digital sensor, so that you can compare if (temp > val) or if (temp && !val).

Because the Arduino digital inputs have a hysteresis, you can connect an analogous sensor to an digital pin to obtain true transitions even from noisy signals. Provided that the sensor values exceed the threshold values of 30% and 70% of Vcc.

DrDiettrich: Your condition will never become true when the value will equal limit during transition from low to high.

You're better off with a digital sensor, so that you can compare if (temp > val) or

if (temp && !val)

.

Because the Arduino digital inputs have a hysteresis, you can connect an analogous sensor to an digital pin to obtain true transitions even from noisy signals. Provided that the sensor values exceed the threshold values of 30% and 70% of Vcc.

Thank you very much . It's work!!!