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