Converting pulses on INT0 to MPH

Hi

I am trying to convert the incoming pulses on INT0 into MPH and display the current speed on serial monitor.

I've worked out that at 1mph there are 1.7133333 pulses per second from my speed sensor which generates 4 pulses per revolution and at 1mph is doing 25.7 RPM.

I can count the pulses coming in on INT0 and display the count but that's where my knowledge ends with interrupts.

My aim is to turn Pin 13 on and off at different speeds.

Please, post your codes/sketch with code tags (</>).

/*Program to read the speed in mph on int 0 and turn on LED on D13 when Speed reaches 15mph.

  V1- Read Speed on int0 and display on serial monitor.

  V2- Add LED on D13 and turn on at 15mph.

  V3- Add potentiometer to A0 and adjust the speed at which the LED turns on between 15mph and 40mph.

  Speed sensor generates 4 pulses per revolution. At 1mph sensor is doing 25.7rpm and 102.8 pulses

  HARWARE USED - Brantz BR3 Speed Sensor, Arduino Nano V3
*/

#define speedPin 2 // Input from speed sensor
#define LED 13 // Output to onboard LED
#define potPin A0 // Input from potentiometer.

unsigned long PulseCount;
unsigned long LastPulseTime = 0;
const unsigned MaxPulseLength = 20;


void PulseISR()
{
  PulseCount++;
  LastPulseTime = millis();
}

void setup() {

  Serial.begin (9600);

  pinMode(LED, OUTPUT);
  pinMode(potPin, INPUT);

  attachInterrupt(digitalPinToInterrupt(speedPin), PulseISR, RISING);

}

void CountPulsesUntilTheyStop()
{
   while (1)
  {
    noInterrupts();
    unsigned long LPT = LastPulseTime;
    interrupts();
    if (millis() - LPT >= MaxPulseLength)
    {
      // No pulse state change for a while.  Must have hit a stop
      return;
    }
  }
}
void loop() {

    Serial.println(PulseCount);
    Serial.println("-------");
    delay(100);
  }

Any variable which is updated by an interrupt routine should be declared as volatile.

What range of speeds do you need to measure and with what accuracy? How often do you want to update the measured speed? For example, you might want to measure speeds between 20 and 100MPH with an accuracy of 5MPH and update twice per second.

There are at least two ways to approach this, and the best way will depend on your answers.

One way is to measure the gap between pulses in microseconds or milliseconds. This will give frequent updates but the accuracy may not be great.

Another way is to measure, for example, 10 pulses from the first pulses to the last pulse. This will increase the accuracy but the update frequency may be too low.

A third way is to count the pulses over a period. The longer the period, the more accurate the reading but the slower the updates.

That's half the information you need. Can you also use millis() to determine the time it took for a number of pulses?

It sounds like you are stuck on the maths, please assure me that you are not.

a7

Only really need to measure between 15 and 100mph. Before 15mph nothing will happen.

Between 15 and 40mph the switch point of the output can be varied via a pot.

It would be better to state how many pulses you get per mile and what speed range and accuracy you are interested in .
You can then think about how many pulses you need to count or/and over what period of time to give you that accuracy and response time compromise you want .

Do a few sums

Pulse per mile is 2641.96

So …..

At say 15mph over what time do you need to count pulses to give the required accuracy ? ( the effect of an extra pulse in the time period or missing one )

That i don't know. An accuracy of +/- 1mph would be ok.

You could try measuring the interval between pulses using micros(). At 15mph the time should be 38910us and at 100mph it should be 5836us.

mph = 1000000000UL / (interval * 1713UL)

When I try adding the code to convert to mph, without any pulses I'm getting 4294967295 mph on serial monitor.

I've got this to work. Could not figure it out with interrupt.

How would I incorporate the pot so that the speed in which the LED turns on can be varied between 15 and 40mph

/*Program to read the speed in mph on pin 2 and turn on LED on D13 when Speed reaches 15mph.

  V1- Read Speed on int0 and display on serial monitor.

  V2- Add LED on D13 and turn on between 15 and 40mph.

  V3- Add potentiometer to A0 and adjust the speed at which the LED turns on between 15mph and 40mph.

  Speed sensor generates 4 pulses per revolution. At 1mph sensor is doing 25.7rpm and 102.8 pulses

  HARWARE USED - Brantz BR3 Speed Sensor, Arduino Nano V3
*/

int pin = 2;
int LED = 13;

unsigned long interval = 0;
unsigned long mph = 0;
unsigned long rpm = 0;

void setup() {
  Serial.begin(9600);
  pinMode(pin, INPUT);
  pinMode(LED,OUTPUT);
}

void loop() {
  interval = pulseIn(pin, HIGH);
  mph = 1000000000UL / (interval * 1713UL);
  rpm = mph*25.7;

   if ((mph >= 15) && (mph <= 40))
   {
    digitalWrite(LED,HIGH);
   }
   else
   {
    digitalWrite(LED,LOW);
   }
  Serial.println(mph);
  Serial.print("\t       ");
  Serial.println(rpm);
  Serial.println("---------------");
  delay(100);
  
}