Yesterday I blogged about getting information from our gas meter with an Arduino that reads the reed switch. After using this setup for many hours, we noticed that our counter was not accurate. My first suspect was the IN-Z31 reed switch, it looked like the switch sometimes stayed closed for a very long time. To get more information, I changed the code so that when the sensor port changes to HIGH (1), I start an other function that checks for how long (in milliseconds) the switch stays closed.
Luckily, soon after changing the code and monitoring the serial (debugging) output, the gas meter stopped exactly at the digit 9 and the CountStateDuration() function showed me that the switch stayed closed. Yesterday we already noticed that we got a pulse every time when the digit 9 was shown, so that the switch stayed closed at 9, was not a very big surprise.
Now we learned that, I modified the code. In the loop() it calls the function CheckSwitch() which returns a 0 if the switch is open (not making contact). If the switch is closed, thus making contact, the function CountStateDuration() is called to check how long the switch is closed. This function returns when the switch is open again, resulting in a counter update.
The LCD screen now shows the real counter, but to get that right, you need to set the current reading before starting the Arduino. The new code can be downloaded here.