Skip to content

Instantly share code, notes, and snippets.

@LeonNardella
Last active May 5, 2018 00:47
Show Gist options
  • Select an option

  • Save LeonNardella/65b32a71f15893b61370d86cde77cd5c to your computer and use it in GitHub Desktop.

Select an option

Save LeonNardella/65b32a71f15893b61370d86cde77cd5c to your computer and use it in GitHub Desktop.
const byte interruptPin = 2;
volatile byte rpmcount;
unsigned int rpm;
unsigned long timeold;
void rpm_fun()
{
rpmcount++;
}
void setup()
{
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(interruptPin), rpm_fun, FALLING);
rpmcount = 0;
rpm = 0;
timeold = 0;
}
void loop()
{
delay(1000);
detachInterrupt(0);
rpm = 10 * 1000 / (millis() - timeold) * rpmcount;
timeold = millis();
rpmcount = 0;
Serial.println(rpm, DEC);
attachInterrupt(digitalPinToInterrupt(interruptPin), rpm_fun, FALLING);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment