Accumulate hall sensor output

Hi All,

Good to be back. Managed to solve a number of challenges but now this lies before me…

I have a seemingly simple sketch.

Hall sensor attached to a digital pin. No interrupts as the application only calls for one sense action every 1 - 5 minutes and I need 8 sensors.

If the hall sensor is pulled LOW (0) by the magnet, I need to somehow register an INT (1), then when it is pulled LOW again increment by 1 and so on. I only need to accumulate, not calculate velocity or reset the value. The value only needs to increment by 1 every time it is pulled LOW. I can’t for the life of me solve this.

I have thought of incrementing the state of the sensor, i.e. when LOW increment 1 or something but I lack the logic to figure this out.

The snippet of code below is in loop(). There are a number of posts on ""how to print a value or text once in loop(), but all are inconclusive.

pinState1 = digitalRead(pin1);
  if (pinState1==0)  {
    pinState1=1;
    Serial.println(count1++);

Again to summarise the problem statement:

Read state of hall sensor
Register a 1 when LOW
Store the 1
Next time thru the loop
Register a 1 when LOW
Increment by 1

Simple heh?

The state change detection example in the IDE (File, Examples, Digital) might be what you need.

groundFungus:
The state change detection example in the IDE (File, Examples, Digital) might be what you need.

Perhaps, but that is not clear. The OP may want to accumulate the total number of consecutive periods (1 - 5mins) where the sensor is low. They may not wish to count the occasions on which the sensor changes from high to low.

@janniebunny please read the "please read" post. It will give you many hints how to get the help you need here. Good to see that you have used code tags, but the post will also tell you that we need to see the full code, fragments are usually not helpful. Also please clarify your requirements. As you can see, your explanation is ambiguous.

Thank you GroundFungus. You hit the nail on its head! All good, all 8 sensors do exactly what I intend.

My description is not ambiguous at all :o

Cheers

janniebunny:
My description is not ambiguous at all :o

Yes it is:

Read state of hall sensor
Register a 1 when LOW
Store the 1
Next time thru the loop
Register a 1 when LOW
Increment by 1

This clearly says that as long as the sensor remains LOW, the count is incremented each time. It does not say to check that the sensor goes back to HIGH and then back to LOW again before the counter can be incremented.

But now you have changed your mind and you are saying you do need to detect state changes? Are you sure, because you seem a little confused.