Programming problem digitalRead

Hello,

I’m using a reed contact and I want to create a loop, which adds 1 to an int every time the signal is HIGH. I used digitalRead(pin) to check whether it is HIGH or LOW. During a measurement of 10 seconds, the signal is several times HIGH and I would like to receive this as an int. I hope someone can give me some help.

Just use:

if (digitalRead(x) == HIGH) count++;

But be careful, your code might execute several digitalRead()s while the signal is high - you might count the same pulse several times.

How do you want to count two HIGH in a row? If you read a HIGH to you want to wait for a LOW before counting a new HIGH or is the count just as fast as the loop can spin?

J-M-L:
How do you want to count two HIGH in a row? If you read a HIGH to you want to wait for a LOW before counting a new HIGH or is the count just as fast as the loop can spin?

If I read HIGH, I want to wait for a LOW before counting. If I use if (digitalRead(x) == HIGH) count++;
it counts just as fast as the loop can spin, I think.

In which case it's not counting number of times you read HIGH, it counts the number of loops while reading HIGH.

--Michael

edit: To say it more accurately, you are counting the number of times you read high while still high, not the number of times the input goes high.
--M.

If I read HIGH, I want to wait for a LOW before counting.

Look up the state change detection example in the IDE (File, Examples, Digital). It will show how to take action on transitions.