Need help with rpm counter for Photon

Hi Charliemciver.
Still having a bit of difficulty.
Could you kindly give me the example code?
Merry Christmas to everyone!

I don't really get that comment - where is there any complex floating point in my code - and even if there was a floating point calculation, what's complex or difficult about that? :confused:

BTW, just count the lines of code used for either solution - which one is less complex?

#define LED        D7
#define RPM_SENSOR D4

volatile uint32_t usLastTime;
volatile uint32_t rpm = 0;

void rpm_shaft();

void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(RPM_SENSOR, INPUT_PULLUP);
  attachInterrupt(RPM_SENSOR, rpm_shaft, FALLING);
  
  //RGB.control(true);
  //RGB.color(0,0,0);
}

void loop()
{
  digitalWrite(LED, rpm > 40);
  //RGB.color(rpm <= 40 ?  0 : rpm, rpm, 0);
}

void rpm_shaft()                
{
  uint32_t usDelta = micros() - usLastTime;
  usLastTime = micros();
  rpm = 60000000 / usDelta;
}

Not a "complex" timer in sight nor needed :wink:

ScruffR…I have implemented your above example code in my project. I send the reading to a Blynk gauge and it measures the rpm correctly. The problem is that it displays the last reading even after the shaft has stopped spinning.
It will recommence rpm reading once shaft rotates again but then when it stops it shows the last reading.

since you are using micros() in your ISR, and usLastTime is a global var, you could leverage that and create a timer in loop() that checks for the moment of last rotation. If that moment is longer than your desired timeout, reset RPM to zero.

something like this:

void loop()
{
  digitalWrite(LED, rpm > 40);
  //RGB.color(rpm <= 40 ?  0 : rpm, rpm, 0);
  if(micros() - usLastTime > ROTATION_TIMEOUT)  // ROTATION_TIMEOUT should be less than 70mins, as micros() rolls over every 70mins (approx).
  {
    rpm = 0;
  }
}
1 Like

By the way, if you install Particle Dev on Windows 7, the built-in serial terminal (“Show Serial Monitor” button) also works just fine. You do have to remember to click Connect (do not remember at the moment if a disconnect is needed first) after rebooting or reflashing your Photon. I have not verified in other versions of Windows

An old thread, but in case anyone needs the updated code. :grinning:

// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>

#define RPM_SENSOR D6                  // rpm signal from hall effect sensor
#define rpms_Display V1                     // Blynk Value rpms display virtual pin V1                         

char auth[] = "xxxxxxxxxxxxxxxxxxxxxx";     

float revs;
float rpm;
volatile byte rpmcount;
long previousmicros = 0;
long interval = 1000000;

void setup()
{
  Blynk.begin(auth);
  pinMode(RPM_SENSOR, INPUT_PULLUP);                     
  attachInterrupt(RPM_SENSOR, SHAFT_RPM, FALLING);       
}

BLYNK_READ(rpms_Display)                                     // display rpm value on Blynk Value Widget V1 
{
Blynk.virtualWrite(rpms_Display, rpm);
}              

void loop()
{
  Blynk.run();

  unsigned long currentmicros =micros();
  int sensorValue = digitalRead(RPM_SENSOR);

  if (currentmicros - previousmicros > interval)
 {
      previousmicros = currentmicros;
      detachInterrupt(RPM_SENSOR);
      revs=1000000.0/rpmcount;
      rpm = 60000000/revs;
      rpmcount = 0;
      attachInterrupt(RPM_SENSOR, SHAFT_RPM, FALLING);
  }
}

void SHAFT_RPM() {                         
 rpmcount++;
 }
2 Likes