Skip to content

Commit ad8f664

Browse files
petewillmfalkvidd
authored andcommitted
Fix array size issue
Updated variable types to optimize bytes used and fixed rainBucket array size issue
1 parent 7295a00 commit ad8f664

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

examples/RainGauge/RainGauge.ino

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*
22
Arduino Tipping Bucket Rain Gauge
33
4-
April 26, 2015
4+
October 6, 2017
55
6-
Version 2.0
6+
Version 2.1
77
88
Arduino Tipping Bucket Rain Gauge
99
@@ -33,7 +33,9 @@
3333
a cumulative total rainfall (day4 = day1+day2+day3+day4 etc)
3434
3535
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
3739
*/
3840

3941
//#define MY_DEBUG // Enable MySensors debug prints to serial monitor
@@ -42,6 +44,8 @@
4244
#define MY_RADIO_NRF24
4345
//#define MY_RADIO_RFM69
4446

47+
#define MY_RF24_PA_LEVEL RF24_PA_MAX //Options: RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH, RF24_PA_MAX
48+
4549
//#define MY_NODE_ID 7 //uncomment this line to assign a static ID
4650

4751
#include <math.h>
@@ -65,8 +69,8 @@
6569
#define CHILD_ID_RAIN_LOG 3 // Keeps track of accumulated rainfall
6670
#define CHILD_ID_TRIPPED_INDICATOR 4 // Indicates Tripped when rain detected
6771
#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
7074

7175
#ifdef DEBUG_ON
7276
#define DEBUG_PRINT(x) Serial.print(x)
@@ -111,29 +115,29 @@ MyMessage msgTrippedVar2(CHILD_ID_TRIPPED_INDICATOR, V_VAR2);
111115
#define CHILD_ID_LIGHT 2
112116
BH1750 lightSensor;
113117
MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
114-
unsigned int lastlux;
118+
uint16_t lastlux;
115119
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
116120
#endif
117121
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
121125
#ifdef DEBUG_ON
122126
unsigned long dataMillis;
123127
unsigned long serialInterval = 600000UL;
124128
#endif
125129
const unsigned long oneHour = 3600000UL;
126130
unsigned long lastTipTime;
127131
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;
130134
uint8_t rainWindow = 72; //default rain window in hours. Will be overwritten with msgTrippedVar1.
131135
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.
133137
uint8_t state = 0;
134138
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;
137141
bool gotTime = false;
138142
uint8_t lastHour;
139143
uint8_t currentHour;
@@ -190,7 +194,7 @@ void setup()
190194
//retrieve from EEPROM stored values on a power cycle.
191195
//
192196
bool isDataOnEeprom = false;
193-
for (int i = 0; i < E_BUFFER_LENGTH; i++)
197+
for (uint8_t i = 0; i < E_BUFFER_LENGTH; i++)
194198
{
195199
uint8_t locator = loadState(EEPROM_BUFFER_LOCATION + i);
196200
if (locator == 0xFE) // found the EEPROM circular buffer index
@@ -212,7 +216,7 @@ void setup()
212216
saveState(eepromIndex, 0xFE);
213217
saveState(eepromIndex + 1, 0xFE);
214218
//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++)
216220
{
217221
saveState(i, 0x00);
218222
}
@@ -229,7 +233,7 @@ void setup()
229233
//
230234
#ifdef DHT_ON
231235
dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
232-
metric = getControllerConfig().isMetric;
236+
metric = getControllerConfig().isMetric;
233237
wait(DWELL_TIME);
234238
#endif
235239
//
@@ -263,8 +267,8 @@ void loop()
263267
//
264268
// let's constantly check to see if the rain in the past rainWindow hours is greater than rainSensorThreshold
265269
//
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++)
268272
{
269273
measure += rainBucket [i];
270274
if (measure != lastMeasure)
@@ -316,7 +320,7 @@ void loop()
316320
saveState(eepromIndex + 1, lowByte(rainBucket[0]));
317321
DEBUG_PRINT(F("Saving rainBucket[0] to eeprom. rainBucket[0] = "));
318322
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
320324
{
321325
rainBucket [i + 1] = rainBucket [i];
322326
}
@@ -392,7 +396,7 @@ void doDHT(void)
392396
#ifdef LUX_ON
393397
void doLUX(void)
394398
{
395-
unsigned int lux = lightSensor.readLightLevel();// Get Lux value
399+
uint16_t lux = lightSensor.readLightLevel();// Get Lux value
396400
DEBUG_PRINT(F("Current LUX Level: "));
397401
DEBUG_PRINTLN(lux);
398402
heartbeat++;
@@ -419,10 +423,10 @@ void sensorTipped()
419423
lastTipTime = thisTipTime;
420424
}
421425
//
422-
int rainTotal(int hours)
426+
uint32_t rainTotal(uint8_t hours)
423427
{
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++)
426430
{
427431
total += rainBucket [i];
428432
}
@@ -436,7 +440,7 @@ void updateSerialData(int x)
436440
DEBUG_PRINT(x);
437441
DEBUG_PRINTLN(F(" hours: "));
438442
float tipCount = 0;
439-
for (int i = 0; i < x; i++)
443+
for (uint8_t i = 0; i < x; i++)
440444
{
441445
tipCount = tipCount + rainBucket [i];
442446
}
@@ -445,9 +449,9 @@ void updateSerialData(int x)
445449
}
446450
#endif
447451

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
449453
{
450-
for (int i = 1; i < RAIN_BUCKET_SIZE; i++)
454+
for (uint8_t i = 1; i < RAIN_BUCKET_SIZE; i++)
451455
{
452456
eValue = eValue - 2;
453457
if (eValue < EEPROM_BUFFER_LOCATION)
@@ -458,7 +462,7 @@ void loadRainArray(int eValue) // retrieve stored rain array from EEPROM on powe
458462
DEBUG_PRINTLN(eValue);
459463
uint8_t rainValueHigh = loadState(eValue);
460464
uint8_t rainValueLow = loadState(eValue + 1);
461-
unsigned int rainValue = rainValueHigh << 8;
465+
uint16_t rainValue = rainValueHigh << 8;
462466
rainValue |= rainValueLow;
463467
rainBucket[i] = rainValue;
464468
//
@@ -474,7 +478,7 @@ void transmitRainData(void)
474478
DEBUG_PRINT(F("In transmitRainData. currentHour = "));
475479
DEBUG_PRINTLN(currentHour);
476480
int rainUpdateTotal = 0;
477-
for (int i = currentHour; i >= 0; i--)
481+
for (int8_t i = currentHour; i >= 0; i--)
478482
{
479483
rainUpdateTotal += rainBucket[i];
480484
DEBUG_PRINT(F("Adding rainBucket["));
@@ -488,7 +492,7 @@ void transmitRainData(void)
488492
#ifdef USE_DAILY
489493
rainUpdateTotal = 0;
490494
#endif
491-
for (int i = currentHour + 24; i > currentHour; i--)
495+
for (uint8_t i = currentHour + 24; i > currentHour; i--)
492496
{
493497
rainUpdateTotal += rainBucket[i];
494498
DEBUG_PRINT(F("Adding rainBucket["));
@@ -502,7 +506,7 @@ void transmitRainData(void)
502506
#ifdef USE_DAILY
503507
rainUpdateTotal = 0;
504508
#endif
505-
for (int i = currentHour + 48; i > currentHour + 24; i--)
509+
for (uint8_t i = currentHour + 48; i > currentHour + 24; i--)
506510
{
507511
rainUpdateTotal += rainBucket[i];
508512
DEBUG_PRINT(F("Adding rainBucket["));
@@ -516,7 +520,7 @@ void transmitRainData(void)
516520
#ifdef USE_DAILY
517521
rainUpdateTotal = 0;
518522
#endif
519-
for (int i = currentHour + 72; i > currentHour + 48; i--)
523+
for (uint8_t i = currentHour + 72; i > currentHour + 48; i--)
520524
{
521525
rainUpdateTotal += rainBucket[i];
522526
DEBUG_PRINT(F("Adding rainBucket["));
@@ -530,7 +534,7 @@ void transmitRainData(void)
530534
#ifdef USE_DAILY
531535
rainUpdateTotal = 0;
532536
#endif
533-
for (int i = currentHour + 96; i > currentHour + 72; i--)
537+
for (uint8_t i = currentHour + 96; i > currentHour + 72; i--)
534538
{
535539
rainUpdateTotal += rainBucket[i];
536540
DEBUG_PRINT(F("Adding rainBucket["));

0 commit comments

Comments
 (0)