Skip to content

Commit 01c86cf

Browse files
committed
Merge pull request arduino#175 from gizmocuz/master
initialize digital pins internal pullup resistor
2 parents c506533 + 86f7109 commit 01c86cf

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

libraries/MySensors/examples/WaterMeterPulseSensor/WaterMeterPulseSensor.ino

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*
2121
* REVISION HISTORY
2222
* Version 1.0 - Henrik Ekblad
23+
* Version 1.1 - GizMoCuz
2324
*
2425
* DESCRIPTION
2526
* Use this sensor to measure volume and flow of your house watermeter.
@@ -33,8 +34,8 @@
3334
* http://www.mysensors.org/build/pulse_water
3435
*/
3536

36-
#include <SPI.h>
3737
#include <MySensor.h>
38+
#include <SPI.h>
3839

3940
#define DIGITAL_INPUT_SENSOR 3 // The digital input you attached your sensor. (Only 2 and 3 generates interrupt!)
4041
#define SENSOR_INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
@@ -47,7 +48,7 @@
4748

4849
#define CHILD_ID 1 // Id of the sensor child
4950

50-
unsigned long SEND_FREQUENCY = 20000; // Minimum time between send (in milliseconds). We don't want to spam the gateway.
51+
unsigned long SEND_FREQUENCY = 30000; // Minimum time between send (in milliseconds). We don't want to spam the gateway.
5152

5253
MySensor gw;
5354
MyMessage flowMsg(CHILD_ID,V_FLOW);
@@ -72,8 +73,11 @@ void setup()
7273
{
7374
gw.begin(incomingMessage);
7475

76+
// initialize our digital pins internal pullup resistor so one pulse switches from high to low (less distortion)
77+
pinMode(DIGITAL_INPUT_SENSOR, INPUT_PULLUP);
78+
7579
// Send the sketch version information to the gateway and Controller
76-
gw.sendSketchInfo("Water Meter", "1.2");
80+
gw.sendSketchInfo("Water Meter", "1.1");
7781

7882
// Register this device as Waterflow sensor
7983
gw.present(CHILD_ID, S_WATER);
@@ -85,15 +89,15 @@ void setup()
8589

8690
lastSend = lastPulse = millis();
8791

88-
attachInterrupt(SENSOR_INTERRUPT, onPulse, RISING);
92+
attachInterrupt(SENSOR_INTERRUPT, onPulse, FALLING);
8993
}
9094

9195

9296
void loop()
9397
{
9498
gw.process();
9599
unsigned long currentTime = millis();
96-
100+
97101
// Only send values at a maximum frequency or woken up from sleep
98102
if (SLEEP_MODE || (currentTime - lastSend > SEND_FREQUENCY))
99103
{
@@ -124,7 +128,7 @@ void loop()
124128
}
125129

126130
// Pulse count has changed
127-
if (pulseCount != oldPulseCount) {
131+
if ((pulseCount != oldPulseCount)||(!SLEEP_MODE)) {
128132
oldPulseCount = pulseCount;
129133

130134
Serial.print("pulsecount:");
@@ -133,7 +137,7 @@ void loop()
133137
gw.send(lastCounterMsg.set(pulseCount)); // Send pulsecount value to gw in VAR1
134138

135139
double volume = ((double)pulseCount/((double)PULSE_FACTOR));
136-
if (volume != oldvolume) {
140+
if ((volume != oldvolume)||(!SLEEP_MODE)) {
137141
oldvolume = volume;
138142

139143
Serial.print("volume:");
@@ -152,6 +156,7 @@ void incomingMessage(const MyMessage &message) {
152156
if (message.type==V_VAR1) {
153157
unsigned long gwPulseCount=message.getULong();
154158
pulseCount += gwPulseCount;
159+
flow=oldflow=0;
155160
Serial.print("Received last pulse count from gw:");
156161
Serial.println(pulseCount);
157162
pcReceived = true;
@@ -178,4 +183,3 @@ void onPulse()
178183
}
179184
pulseCount++;
180185
}
181-

0 commit comments

Comments
 (0)