Calculate voltage differences per minute

Hello friends,

We have a project that needs to compare two output voltages:

Output #1 = V1 Output #2 = V2

If V1 > V2 then we light up an LED. Using a LM311 voltage comparator, we got things hooked up and worked perfectly.

We noticed the voltage differences happened so frequently that the LED blinks too fast to count, we solved the problem by hooking up LM311 output to Ardunio PIN2 and use digitalRead to read and count. No problem.

For the last part, we need to calculate voltage differences per minute. This is where we got stuck. We can increment a counter when there is a voltage difference but how to interpret this in difference per minute?

Any idea?

Thanks for your help in advance. BTF.

what about this:

uint32_t lstTS = 0;
uint32_t lstC = 0;
void loop() {
  const uint32_t now = millis();
  const uint32_t dt = now - lstTS;
  if (dt < 1000) return;
  const uint32_t dC = C - lstC;
  Serial.print(dC/(double)dt);
  Serial.print("kHz");
  lstTS = now;
  lstC = C;
}

?

Riddick has a good solution,

You can also make 10 timeslots of 100 milliseconds and count per 100 ms.
After 100ms you add up the 10 slots to get the total of the last second.
Then you “empty” the oldest slot and count for 100 millis there.

(If you want to code this yourself skip next block :wink:

#define BINS 10  // BINS is a divider of 1000 e.g. 2,4,5,10,20 seems reasonable values 
#define THRESHOLD 1000/BINS
#define PULSEPIN 2

unsigned long counter[BINS];
uint8_t idx = 0;
unsigned long lastTime = 0;
int previous = LOW;

void setup()
{
  Serial.begin(115200);
  pinMode(PULSEPIN , INPUT);
}

void loop()
{
  // READ THE PULSE, REMEMBER STATE
  if (digitalRead(PULSEPIN ) == HIGH && previous == LOW)
  {
    previous = HIGH;
    counter[idx]++;
  }
  if (digitalRead(PULSEPIN ) == LOW && previous == HIGH)
  {
    previous = LOW;
  }

  // DO THE ADMIN EVERY NOW & THEN
  if (millis() - lastTime > THRESHOLD)
  {
    lastTime = millis();
    unsigned long sum = 0;
    for (uint8_t i = 0; i< BINS ; i++) sum += counter[i];

    Serial.print(sum * 0.001, 3);
    Serial.println(" kHz");

    idx++;
    if (idx == BINS) idx = 0;
    counter[idx] = 0;
  }
}

Of course the counting can be interrupt based too