int inputPin = 2;
unsigned long time1;
unsigned long time2;
int half_revs = 0;
float time = 0;
float rps = 0;
int inBit;
void setup() {
// Open Serial Port
Serial.begin(9600);
while (!Serial) {
// wait for serial port to connect. Needed for Leonardo only
}
// Config Pins
pinMode(inputPin,INPUT);
attachInterrupt(0, tach, CHANGE);
}
void loop() {
half_revs = 0;
time1 = micros();
interrupts();
delay(750);
noInterrupts();
time2 = micros();
time = ((float)time2+(float)time1)/2/1e6;
rps = (half_revs/2)/((time2-time1)/1e6);
if (Serial.available()>0){
inBit = Serial.read();
Serial.println(time);
Serial.println(rps);
}
}
void tach()
{
half_revs++;
}