Count number of times pin goes high per minute

I am using a photo interrupter to count the number of bubbles my homebrewed beer produces per minute during fermentation.

I have the photo interrupter working and successfully connected to my arduino.

The following sketch lights an LED anytime something blocks the interrupter:

int ledPin = 13;                // LED connected to digital pin 13
int photoPin = 2;               // 
int val = 0;
unsigned int count = 0;

void setup()                    // run once, when the sketch starts
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
  pinMode(photoPin, INPUT);  
}

void loop()                     // run over and over again
{
  val = digitalRead(photoPin);
  if (val==LOW)
  {
    digitalWrite(ledPin, LOW);
  }
    else
    {
      digitalWrite(ledPin, HIGH);
      count ++;
    }
}

Any recommendations on how to stop the loop every minute to "count" the total number of bubbles?

It should be obvious that I am new to the arduino. Great community.

Thanks!!

Hi,

here’s your code changed:

int ledPin = 13;                // LED connected to digital pin 13
int photoPin = 2;               //
int val = 0;
int valold = 0;
int start;
unsigned int count = 0;

void setup()                    // run once, when the sketch starts
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
  pinMode(photoPin, INPUT);  
  valold = digitalRead(photoPin);
  Serial.begin(9600);
}

void loop()                     // run over and over again
{
  start = millis(); // take the start time
  count = 0; // reset couter
  // do the following loop for 60000ms = 1min
  while (millis()-start < 60000)
  {
     // check for overflow of millis()
     if (start > millis() {
       start = millis();
       count = 0;
     }
    val = digitalRead(photoPin);
    if (val==LOW)
    {
      digitalWrite(ledPin, LOW);
    }
    else
    {
      digitalWrite(ledPin, HIGH);
      if (val <> valold) {
        count ++;
        valold = val;
      }
    }
  }
  // 1 minute over. print conts
  Serial.println(count,DEC);
}

Good luck
Mike

  }
  // 1 minute over. print conts
  Serial.println(count,DEC);
}

This kind of thing bugs me. Numbers scroll by on the screen, but what do they mean. A few seconds more typing, and a few more bytes of memory used, and a slightly slower execution:

  }
  // 1 minute over. print conts
  Serial.print("Bubbles per minute: ");
  Serial.println(count,DEC);
}

Now the output looks like:

Bubbles per minute: 450

instead of

450

It does not really matter when there is only one value being printed, but it matters a lot when there are lots of numbers being printed. Which one is which?

This kind of thing bugs me. Numbers scroll by on the screen, but what do they mean.

On a quick test I would just have the values but on a finished project I would always give some indication of what the numbers were, unless it was just for debugging and I was short on space...

Mowcius

Philosophical ramblings prompted by the above…

The code given meets the needs stated.

But for others reading this thread later, who may be interested in counting in general…

The solution above requires that the Arduino loll at the sensor at least often enough to “see” every on/off.

For the sake of discussion, let’s say that the bubble sensor returns “low” most of the time, and “high” briefly as a bubble passes.

If the Arduino were doing more than just counting the bubbles, the following might happen:

Arduino checks bubble sensor: Finds it low
Arduino goes off for, say, 500ms, doing other things…
… and during that 500ms, the bubble sensor goes high and back to low.
Arduino checks bubble sensor again: Still low…

… but system has “missed” a bubble event.

One solution, if your needs require something fancier, something that is ALWAYS watching the bubble sensor, and thus never misses a quickly passing “high/low” is an external counter chip.

The 1-Wire family of chips has one that interfaces relatively easily to an Arduino. More on this at…

http://sheepdogsoftware.co.uk/sc2sn.htm

If you have enough spare I/O pins in whatever project needs the external counter, there are simpler chips which have, say, 1 input (to which you connect the sensor looking for bubbles or whatever) and, say, 4 outputs. Those outputs will “show” 0-15, depending on how many bubbles have passed, and you can check them any time. (When there’ve been 16 high/lows, the counter rolls over back to zero. You can get other chips with more output pins, so you can go longer between checks without missing a roll over.)

One little “gotcha”… you’ll probably want to endure the additional complexity required to “freeze” the counter chip’s outputs while you read them. If you were to read the outputs, one after the other, while the number shown by their combined states was changing, you would get “wrong” answers.

1 Like