cordial saludo, ya he consultado sobre temas de rpm pero ahora estoy tratando de realizar la lectura "RPM" cambiando el numero de ranuras del disco que lee el control optico (creeria que si mas ranuras mejor resolucion).
cuando lo hago con 1 ranura(1pulso) trabaja perfecto pero al hacerlo con 2 ranuras (2 pulsos ) la lectura varia demasiado, el disco ranurado tiene originalmente 8 ranuras (8pulsos) pero asi mas se enloquece la lectura. que podria estar pasando, influye la velocidad del puerto serie para leer rpm? la coloco a 9600 y a 115200 y trabaja igual tambien cambio el delay y tampoco cambia. gracias de antemano por las sugerencias.
// Frequency timer
// Author: Nick Gammon
// Date: 10th February 2012
volatile boolean first;
volatile boolean triggered;
volatile unsigned long overflowCount;
volatile unsigned long startTime;
volatile unsigned long finishTime;
// here on rising edge
void isr ()
{
unsigned int counter = TCNT1; // quickly save it
// wait until we noticed last one
if (triggered)
return;
if (first)
{
startTime = (overflowCount << 16) + counter;
first = false;
return;
}
finishTime = (overflowCount << 16) + counter;
triggered = true;
detachInterrupt(0);
} // end of isr
// timer overflows (every 65536 counts)
ISR (TIMER1_OVF_vect)
{
overflowCount++;
} // end of TIMER1_OVF_vect
void prepareForInterrupts ()
{
// get ready for next time
EIFR = _BV (INTF0); // clear flag for interrupt 0
first = true;
triggered = false; // re-arm for next time
attachInterrupt(0, isr,FALLING );
} // end of prepareForInterrupts
void setup () {
Serial.begin(115200);
Serial.println("Frequency Counter");
// reset Timer 1
TCCR1A = 0;
TCCR1B = 0;
// Timer 1 - interrupt on overflow
TIMSK1 = _BV (TOIE1); // enable Timer1 Interrupt
// zero it
TCNT1 = 0;
// start Timer 1
TCCR1B = _BV (CS10); // no prescaling
// set up for interrupts
prepareForInterrupts ();
} // end of setup
void loop ()
{
if (!triggered)
return;
unsigned long elapsedTime = finishTime - startTime;
float freq = 1.0 / ((float (elapsedTime) * 62.5e-9)); // each tick is 62.5 nS
//Serial.print ("Took: ");
//Serial.print (elapsedTime);
// Serial.print (" counts. ");
//Serial.print ("Frequency: ");
Serial.println (freq);
Serial.println (freq/1*60);
Serial.println (freq/2*60);
//Serial.println (" Hz. ");
// so we can read it
delay (500);
prepareForInterrupts ();
} // end of loop