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;}