Acquiring period/time difference from a comparator?

Hello,

I am a beginner trying to calculate the time difference from a comparator output.

I am using the state change example as guide but am having difficulties

any assistance will be greatly appreciated

Below is my code:

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// constants won't change. They're used here to
// set pin numbers:
const int InputComparatorPin = 9; // the number of the input pin

// variables will change:
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
int ComparatorState = 0; // variable for reading the comparator's status
int delta_time=0; //time difference between two pulses
int BPM = 0; // will hold BPM
int lastComparatorState = 0; //
void setup() {

// set up the LCD's number of columns and rows:
lcd.begin(16, 2);

// initialize the comparaotr pin as an input:
pinMode(InputComparatorPin, INPUT);

}

void loop() {
// read the state of the pushbutton value:
ComparatorState = digitalRead(InputComparatorPin); // get the comparator stautus

// compare the buttonState to its previous state
if (ComparatorState != lastComparatorState) {
// if the state has changed, increment the counter
if (ComparatorState == HIGH) {
//first time through lets get the time

//buttonPushCounter++;

currentMillis = millis()/1000;

}

// Delay a little bit to avoid bouncing
delay(50);
// now we have a time difference in seconds
delta_time = currentMillis - previousMillis;

// to convert to bpm we need to inverse the time and multiply by 60
BPM = (1/delta_time)* 60;
lcd.clear();
// ok now we have the bpm from the comparator so lets print it to the lcd
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(BPM);
}
// save the current state as the last state,

//for next time through the loop
lastComparatorState = ComparatorState;
//save the last current time state as the last state
previousMillis = currentMillis;

}

You should have used the "Code" button (far left) instead of the "Quote" button.

Since 'delta_time' is an integer and 1 and 60 are integers your only results from:  BPM = (1 / delta_time) * 60;
will be infinite (delta_time == 0), 60 (delta_time == 1) or 0 (delta_time > 1). You would be better off with  BPM = 60 / delta_time;
or better yet keep everything in milliseconds, change the integers to unsigned long and change the formula to:  BPM = 60000 / delta_time;

Should look something like:

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// constants won't change. They're used here to
// set pin numbers:
const int InputComparatorPin = 9;     // the number of the input pin

// variables will change:
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
int ComparatorState = 0;         // variable for reading the comparator's status
unsigned long delta_time = 0; //time difference between two pulses
int BPM = 0;    // will hold BPM
int lastComparatorState = 0; //
void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);

  // initialize the comparaotr pin as an input:
  pinMode(InputComparatorPin, INPUT);
}

void loop() {
  // read the state of the comparator:
  ComparatorState = digitalRead(InputComparatorPin);

  // compare the buttonState to its previous state
  if (ComparatorState != lastComparatorState) {
    // if the state has changed, increment the counter
    if (ComparatorState == HIGH) {
      //first time through lets get the time
      currentMillis = millis();

      // now we have a time difference in seconds
      delta_time = currentMillis - previousMillis;
      previousMillis = currentMillis;

      // to convert to bpm
      BPM = 60000 / delta_time;
      lcd.clear();
      // ok now we have the bpm from the comparator so lets print it to the lcd
      // set the cursor to column 0, line 1
      // (note: line 1 is the second row, since counting begins with 0):
      lcd.setCursor(0, 1);
      // print the number of seconds since reset:
      lcd.print(BPM);
    }
  }
  // save the current state as the last state,

  //for next time through the loop
  lastComparatorState = ComparatorState;
  //save the last current time state as the last state
}

Thank you! you're help is much appreciated :slight_smile: