1
1
/*
2
2
Arduino Tipping Bucket Rain Gauge
3
3
4
- April 26, 2015
4
+ October 6, 2017
5
5
6
- Version 2.0
6
+ Version 2.1
7
7
8
8
Arduino Tipping Bucket Rain Gauge
9
9
33
33
a cumulative total rainfall (day4 = day1+day2+day3+day4 etc)
34
34
35
35
by @BulldogLowell and @PeteWill for free public use
36
-
36
+
37
+ Change Log
38
+ 2017-10-06 - Version 2.1 - Updated variable types to optimize bytes used and fixed rainBucket array size issue
37
39
*/
38
40
39
41
// #define MY_DEBUG // Enable MySensors debug prints to serial monitor
42
44
#define MY_RADIO_NRF24
43
45
// #define MY_RADIO_RFM69
44
46
47
+ #define MY_RF24_PA_LEVEL RF24_PA_MAX // Options: RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH, RF24_PA_MAX
48
+
45
49
// #define MY_NODE_ID 7 //uncomment this line to assign a static ID
46
50
47
51
#include < math.h>
65
69
#define CHILD_ID_RAIN_LOG 3 // Keeps track of accumulated rainfall
66
70
#define CHILD_ID_TRIPPED_INDICATOR 4 // Indicates Tripped when rain detected
67
71
#define EEPROM_BUFFER_LOCATION 0 // location of the EEPROM circular buffer
68
- #define E_BUFFER_LENGTH 240
69
- #define RAIN_BUCKET_SIZE 120
72
+ #define E_BUFFER_LENGTH 240 // Max size = 254
73
+ #define RAIN_BUCKET_SIZE 120 // Max size = 254
70
74
71
75
#ifdef DEBUG_ON
72
76
#define DEBUG_PRINT (x ) Serial.print(x)
@@ -111,29 +115,29 @@ MyMessage msgTrippedVar2(CHILD_ID_TRIPPED_INDICATOR, V_VAR2);
111
115
#define CHILD_ID_LIGHT 2
112
116
BH1750 lightSensor;
113
117
MyMessage msg (CHILD_ID_LIGHT, V_LIGHT_LEVEL);
114
- unsigned int lastlux;
118
+ uint16_t lastlux;
115
119
uint8_t heartbeat = 10 ; // Used to send the light lux to gateway as soon as the device is restarted and after the DHT_LUX_DELAY has happened 10 times
116
120
#endif
117
121
unsigned long sensorPreviousMillis;
118
- int eepromIndex;
119
- int tipSensorPin = 3 ; // Pin the tipping bucket is connected to. Must be interrupt capable pin
120
- int ledPin = 5 ; // Pin the LED is connected to. PWM capable pin required
122
+ uint8_t eepromIndex;
123
+ uint8_t tipSensorPin = 3 ; // Pin the tipping bucket is connected to. Must be interrupt capable pin
124
+ uint8_t ledPin = 5 ; // Pin the LED is connected to. PWM capable pin required
121
125
#ifdef DEBUG_ON
122
126
unsigned long dataMillis;
123
127
unsigned long serialInterval = 600000UL ;
124
128
#endif
125
129
const unsigned long oneHour = 3600000UL ;
126
130
unsigned long lastTipTime;
127
131
unsigned long lastRainTime; // Used for rainRate calculation
128
- unsigned int rainBucket [RAIN_BUCKET_SIZE] ; /* 24 hours x 5 Days = 120 hours */
129
- unsigned int rainRate = 0 ;
132
+ uint16_t rainBucket [RAIN_BUCKET_SIZE + 1 ] ; /* 24 hours x 5 Days = 120 hours */
133
+ uint16_t rainRate = 0 ;
130
134
uint8_t rainWindow = 72 ; // default rain window in hours. Will be overwritten with msgTrippedVar1.
131
135
volatile int wasTippedBuffer = 0 ;
132
- int rainSensorThreshold = 50 ; // default rain sensor sensitivity in hundredths. Will be overwritten with msgTrippedVar2.
136
+ uint16_t rainSensorThreshold = 50 ; // default rain sensor sensitivity in hundredths. Will be overwritten with msgTrippedVar2.
133
137
uint8_t state = 0 ;
134
138
uint8_t oldState = 2 ; // Setting the default to something other than 1 or 0
135
- unsigned int lastRainRate = 0 ;
136
- int lastMeasure = 0 ;
139
+ uint16_t lastRainRate = 0 ;
140
+ uint16_t lastMeasure = 0 ;
137
141
bool gotTime = false ;
138
142
uint8_t lastHour;
139
143
uint8_t currentHour;
@@ -190,7 +194,7 @@ void setup()
190
194
// retrieve from EEPROM stored values on a power cycle.
191
195
//
192
196
bool isDataOnEeprom = false ;
193
- for (int i = 0 ; i < E_BUFFER_LENGTH; i++)
197
+ for (uint8_t i = 0 ; i < E_BUFFER_LENGTH; i++)
194
198
{
195
199
uint8_t locator = loadState (EEPROM_BUFFER_LOCATION + i);
196
200
if (locator == 0xFE ) // found the EEPROM circular buffer index
@@ -212,7 +216,7 @@ void setup()
212
216
saveState (eepromIndex, 0xFE );
213
217
saveState (eepromIndex + 1 , 0xFE );
214
218
// then I will clear out any bad data
215
- for (int i = 2 ; i <= E_BUFFER_LENGTH; i++)
219
+ for (uint8_t i = 2 ; i <= E_BUFFER_LENGTH; i++)
216
220
{
217
221
saveState (i, 0x00 );
218
222
}
@@ -229,7 +233,7 @@ void setup()
229
233
//
230
234
#ifdef DHT_ON
231
235
dht.setup (HUMIDITY_SENSOR_DIGITAL_PIN);
232
- metric = getControllerConfig ().isMetric ;
236
+ metric = getControllerConfig ().isMetric ;
233
237
wait (DWELL_TIME);
234
238
#endif
235
239
//
@@ -263,8 +267,8 @@ void loop()
263
267
//
264
268
// let's constantly check to see if the rain in the past rainWindow hours is greater than rainSensorThreshold
265
269
//
266
- int measure = 0 ; // Check to see if we need to show sensor tripped in this block
267
- for (int i = 0 ; i < rainWindow; i++)
270
+ uint16_t measure = 0 ; // Check to see if we need to show sensor tripped in this block
271
+ for (uint8_t i = 0 ; i < rainWindow; i++)
268
272
{
269
273
measure += rainBucket [i];
270
274
if (measure != lastMeasure)
@@ -316,7 +320,7 @@ void loop()
316
320
saveState (eepromIndex + 1 , lowByte (rainBucket[0 ]));
317
321
DEBUG_PRINT (F (" Saving rainBucket[0] to eeprom. rainBucket[0] = " ));
318
322
DEBUG_PRINTLN (rainBucket[0 ]);
319
- for (int i = RAIN_BUCKET_SIZE - 1 ; i >= 0 ; i--)// cascade an hour of values back into the array
323
+ for (int16_t i = RAIN_BUCKET_SIZE - 1 ; i >= 0 ; i--)// cascade an hour of values back into the array
320
324
{
321
325
rainBucket [i + 1 ] = rainBucket [i];
322
326
}
@@ -392,7 +396,7 @@ void doDHT(void)
392
396
#ifdef LUX_ON
393
397
void doLUX (void )
394
398
{
395
- unsigned int lux = lightSensor.readLightLevel ();// Get Lux value
399
+ uint16_t lux = lightSensor.readLightLevel ();// Get Lux value
396
400
DEBUG_PRINT (F (" Current LUX Level: " ));
397
401
DEBUG_PRINTLN (lux);
398
402
heartbeat++;
@@ -419,10 +423,10 @@ void sensorTipped()
419
423
lastTipTime = thisTipTime;
420
424
}
421
425
//
422
- int rainTotal (int hours)
426
+ uint32_t rainTotal (uint8_t hours)
423
427
{
424
- int total = 0 ;
425
- for ( int i = 0 ; i <= hours; i++)
428
+ uint32_t total = 0 ;
429
+ for (uint8_t i = 0 ; i <= hours; i++)
426
430
{
427
431
total += rainBucket [i];
428
432
}
@@ -436,7 +440,7 @@ void updateSerialData(int x)
436
440
DEBUG_PRINT (x);
437
441
DEBUG_PRINTLN (F (" hours: " ));
438
442
float tipCount = 0 ;
439
- for (int i = 0 ; i < x; i++)
443
+ for (uint8_t i = 0 ; i < x; i++)
440
444
{
441
445
tipCount = tipCount + rainBucket [i];
442
446
}
@@ -445,9 +449,9 @@ void updateSerialData(int x)
445
449
}
446
450
#endif
447
451
448
- void loadRainArray (int eValue) // retrieve stored rain array from EEPROM on powerup
452
+ void loadRainArray (int16_t eValue) // retrieve stored rain array from EEPROM on powerup
449
453
{
450
- for (int i = 1 ; i < RAIN_BUCKET_SIZE; i++)
454
+ for (uint8_t i = 1 ; i < RAIN_BUCKET_SIZE; i++)
451
455
{
452
456
eValue = eValue - 2 ;
453
457
if (eValue < EEPROM_BUFFER_LOCATION)
@@ -458,7 +462,7 @@ void loadRainArray(int eValue) // retrieve stored rain array from EEPROM on powe
458
462
DEBUG_PRINTLN (eValue);
459
463
uint8_t rainValueHigh = loadState (eValue);
460
464
uint8_t rainValueLow = loadState (eValue + 1 );
461
- unsigned int rainValue = rainValueHigh << 8 ;
465
+ uint16_t rainValue = rainValueHigh << 8 ;
462
466
rainValue |= rainValueLow;
463
467
rainBucket[i] = rainValue;
464
468
//
@@ -474,7 +478,7 @@ void transmitRainData(void)
474
478
DEBUG_PRINT (F (" In transmitRainData. currentHour = " ));
475
479
DEBUG_PRINTLN (currentHour);
476
480
int rainUpdateTotal = 0 ;
477
- for (int i = currentHour; i >= 0 ; i--)
481
+ for (int8_t i = currentHour; i >= 0 ; i--)
478
482
{
479
483
rainUpdateTotal += rainBucket[i];
480
484
DEBUG_PRINT (F (" Adding rainBucket[" ));
@@ -488,7 +492,7 @@ void transmitRainData(void)
488
492
#ifdef USE_DAILY
489
493
rainUpdateTotal = 0 ;
490
494
#endif
491
- for (int i = currentHour + 24 ; i > currentHour; i--)
495
+ for (uint8_t i = currentHour + 24 ; i > currentHour; i--)
492
496
{
493
497
rainUpdateTotal += rainBucket[i];
494
498
DEBUG_PRINT (F (" Adding rainBucket[" ));
@@ -502,7 +506,7 @@ void transmitRainData(void)
502
506
#ifdef USE_DAILY
503
507
rainUpdateTotal = 0 ;
504
508
#endif
505
- for (int i = currentHour + 48 ; i > currentHour + 24 ; i--)
509
+ for (uint8_t i = currentHour + 48 ; i > currentHour + 24 ; i--)
506
510
{
507
511
rainUpdateTotal += rainBucket[i];
508
512
DEBUG_PRINT (F (" Adding rainBucket[" ));
@@ -516,7 +520,7 @@ void transmitRainData(void)
516
520
#ifdef USE_DAILY
517
521
rainUpdateTotal = 0 ;
518
522
#endif
519
- for (int i = currentHour + 72 ; i > currentHour + 48 ; i--)
523
+ for (uint8_t i = currentHour + 72 ; i > currentHour + 48 ; i--)
520
524
{
521
525
rainUpdateTotal += rainBucket[i];
522
526
DEBUG_PRINT (F (" Adding rainBucket[" ));
@@ -530,7 +534,7 @@ void transmitRainData(void)
530
534
#ifdef USE_DAILY
531
535
rainUpdateTotal = 0 ;
532
536
#endif
533
- for (int i = currentHour + 96 ; i > currentHour + 72 ; i--)
537
+ for (uint8_t i = currentHour + 96 ; i > currentHour + 72 ; i--)
534
538
{
535
539
rainUpdateTotal += rainBucket[i];
536
540
DEBUG_PRINT (F (" Adding rainBucket[" ));
0 commit comments