I am attempting to measure the rpm of a propeller using a photo interrupter and have the circuit and code working reasonably well. I am able to calculate how many times the rotor passes through the interrupter however I am having trouble finding the time it takes for it all to happen.
Might anyone have some suggestions on how to keep track of the elapsed time or a method of sectioning off 30 second chunks to analyze separately?
Here's the code I am using at the moment:
int val;
int rpm;
const byte InterruptPin = 2;
volatile byte rev;
void setup() {
Serial.begin(9600);
pinMode(InterruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(InterruptPin), counter, RISING);
}
void loop() {
rpm = 60*rev/(millis()/1000);
val = analogRead(A0);
Serial.print("Value:");
Serial.print(val);
Serial.print("\t\t");
Serial.print("RPM:");
Serial.print(rpm);
Serial.print("\t\t");
Serial.print("Revolutions:");
Serial.println(rev);
delay(5);
}
void counter(){
rev++;
}