Ibrahim Kamal (IKALOGIC)
Measurement algorithm.
The whole is done with that simple algorithm, which is executed on each new ADC (Analog to Digital Converter) end of conversion:
ISR(ADC_vect)
{
//Global variables used :
// ir_value : contains the value of the intensity of IR reflection
// ain_th_high : High threshold (rising hysteresis)
// ain_th_low : Low threshold (falling hysteresis)
// t_capture & t_postscale : reading of the time elapsed since last detected cycle
// ir_history_b and ir_history_a: hold the last two values of ir_value
// last_state : used to remember which hysteresis level to use
ir_value = ADC>>2;
if ((ir_value > ain_th_high) && (last_state==0)) //a cycle is detected
{
last_state = 1; //for next edge detection, use the falling hysteresis
// for next detection
t_capture=TCNT1;TCNT1 = 0;
t_postscale=post_scaller; post_scaller=0;
}
if ((ir_value < ain_th_low) && (last_state==1)) //a low level is detected
{
last_state = 0; //for next edge detection, use the rising hysteresis for
//next detection
}
if (delta(ir_history_a,ir_value) > 10)
{ //the values having more than 10 adc
//quantums will be used to determine
ir_history_b = ir_history_a; //average and hysteresis levels
ir_history_a = ir_value;
ir_avg = (ir_history_a+ir_history_b)/2;
ain_th_high = ir_avg + 5;
ain_th_low = ir_avg - 5;
}
}
A button at the top right corner allow you to activate the IR LED and start measuring RPM data. Once released, the last data is held on screen.
Some tips and tricks for the road
Besides from showing you how to measure RPM of a shaft with IR LED, this project also show you how to do many other cool things like:
- Using an alpha numeric LCD without the need of a potentiometer for the contrast. A simply algorithm that you can find in the source code will adapt the contrast of the LCD according to the batter level, to make sure you alwyas have the brightest possible display (see AVR: Monitor power supply voltage, for free!)
- Using LCD is 4 bit mode to save some wires
- Implementing a hysteresis to enhance noise immunity of the system
Downloads
Source Code - download