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;
}
}