freq += 500000/dt. why??
I want to know that in the program of color sensor there is a line
freq = +=500000/dt
why we are taken 500000 and divided with input sensor data
In this case we are measuring the frequency of the pulses coming from the light sensor. The frequency of the pulses produced by the sensor is proportional to the light intensity. The pulses have a 50% duty cycle - so the pulses are high 50% of the time and low 50% of the time. The dt value returned from the pulseIn function on line 34 of ColorSensor.cpp
dt = pulseIn(FO, HIGH, pulseInWait);
returns the time the pulse is high in microseconds. The period the pulses coming from the sensor (in microseconds) is then 2*dt. This comes from the fact that we have a 50% duty cycle - i.e. one full cycle consists of the pulse being high for dt (us) and then low for dt (us). So we have period_us = 2*dt. To convert the period to seconds we divide by 1,000,000 which gives period_s = period_us/1000000. Replacing period_us with 2*dt we get the period in seconds as period_s = (2*dt)/1000000 = dt/500000. Finally, we want the frequency (in Hz) which is freq _hz = 1/period_s = (2*dt)/1000000 = 500000/dt.