changing calculating time depending on speed of readings


I started with Arduino half a year ago, I am still quit new, so please forgive me if I have missed something basic.

My Project

I am playing around with a little RPM counter project to later hopefully use some of the code to make a Liter pro hour meter for my car by connecting it to a flow sensor in place of the infrared rpm sensor.

Foremost I am trying to make an RPM counter that can read low rpm as-well such as 1 rpm
(this in it self is not so hard I just set the the delay to a minute
delay(60000); ) However doing this means that I need to wait a minute to get every other value that is higher than 1 rpm)
the solution to this could be to make more readings pro revolution E.g. 10 readings pro revolution would cut the reading time down to 6 seconds, but this is not an option as the reason I am playing around with this counter is to use it later with a flow sensor where I can’t just ad more readings pro rotation/liter flow.

My point:

So what I am asking is if it is possible to change the delay between calculations depending on the frequency of the readings?

I have tried to use the “if” writing code like this:

if (rpm > 5)

if (rpm > 10)

This does not seem to affect my delay time or am I doing it wrong.

For refference

the code I am basing it on was borrowed from Arduino RPM Counter / Tachometer
which was originally based on Chris Palmers project

I changed it from LCD to TV-output using the libraries of

int ledPin = 13;                // IR LED connected to digital pin 13
volatile byte rpmcount;
unsigned int rpm;
unsigned long timeold;

// include the library code:
#include <TVout.h>
#include <fontALL.h>
#include "schematic.h"
#include "TVOlogo.h"
// initialize the library with the numbers of the interface pins

TVout TV;

void rpm_fun()
   //Each rotation, this interrupt function is run twice, so take that into consideration for 
   //calculating RPM
   //Update count

void setup()
  TV.select_font(font6x8);  // intialise the tvout

   //Interrupt 0 is digital pin 2, so that is where the IR detector is connected
   //Triggers on FALLING (change from HIGH to LOW)
   attachInterrupt(0, rpm_fun, FALLING);

   //Turn on IR LED
   pinMode(ledPin, OUTPUT);
   digitalWrite(ledPin, HIGH);

   rpmcount = 0;
   rpm = 0;
   timeold = 0;

 void loop()
   //Update RPM every second
   //Don't process interrupts during calculations
   //Note that this would be 60*1000/(millis() - timeold)*rpmcount if the interrupt
   //happened once per revolution instead of twice. Other multiples could be used
   //for multi-bladed propellers or fans
   rpm = 1.8*1000/(millis() - timeold)*rpmcount;
   timeold = millis();
   rpmcount = 0;
   if (rpm > 5)
   if (rpm > 10)

   //Print out result to lcd

   //Restart the interrupt processing
   attachInterrupt(0, rpm_fun, FALLING);


You don’t want to be using delay(), that prevents the processor from doing anything.
Instead, capture the current “time” using millis(), and check if enough time time passed for your next event.
What you can also do is set a flag to indicate which rpm cycle you are checking.


I looked up millis()this I can use to count the time from start up of the arduino board. but can I then just use mills() to replace delay()

So I can adjust the calculation delay time to fit the amount of pulses the arduino recieves by using this.

I will try it tomorrow. right now it is passed mid night.

set a flag to indicate which rpm cycle you are checking

What is a flag? I have not come across this expression before.

A flag is usually a normal Boolean (true/false) variable that is used to indicate an event has happen or a condition has been reached.

The event 'sets' the flag so that it is seen by other parts of the code and can be 'reset' when the event is processed. Think of the US postal service setting the 'flag' on a letterbox so that you don't need to walk out to the letterbox to check but just need to see, from a distance, check if the flag is set.

Most useful to capture one-off events (like an interrupt), or to create a proxy for a complex checking sequence (like walking out to the letterbox).