-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tipping_Bucket_Final.ino
67 lines (54 loc) · 2.45 KB
/
Tipping_Bucket_Final.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//TIPPING BUCKET DEFINITIONS
#define RAIN_PIN 3 //Tipping bucket input
#define CALC_INTERVAL 1000 //Increment of measurements in milliseconds
#define DEBOUNCE_TIME 80 //Time in milliseconds to get through bounce noise
const double bucketAmount = 0.4089; //mm equivalent of ml to trip the bucket
//const double bucketAmount = 0.3537;
//const double bucketAmount = 0.3030;
//const double bucketAmount = 0.2794;
double totalRain = 0.0;
double rainRatemm_hr = 0.0;
unsigned long nextCalc; //"Average Switch closure time is 135 ms"
unsigned long timer; //"Bounce Settling Time: 0.75 ms"
unsigned int rainTrigger = 0;
unsigned long last_micros_rg; //will store last time bucket tipped
unsigned long tipTime;
void countingRain() {
if ((long)(micros() - last_micros_rg) >= DEBOUNCE_TIME) {
tipTime = (micros() - last_micros_rg); //Time taken to fill 1 bucket
rainTrigger += 1;
last_micros_rg = micros();
}
}
void setup() {
Serial.begin(115200);
attachInterrupt(digitalPinToInterrupt(RAIN_PIN), countingRain, RISING);
pinMode(RAIN_PIN, INPUT);
nextCalc = millis() + CALC_INTERVAL;
}
void loop() {
timer = millis();
if (timer > nextCalc) {
nextCalc = timer + CALC_INTERVAL;
totalRain = rainTrigger * bucketAmount;
//rainRate = bucketAmount(mm)/tipTime(hr) // 1 us = 2.777 x 10^-10 hr = 2.777e-10 hr
//rainRate = (bucketAmount / tipTime)*36*pow(10,8); // Multiplied by 10 later due to rain machine
rainRatemm_hr = 14688000.0 / tipTime; // Multiplied by 10 later due to rain machine
if (rainRatemm_hr >= 0) {
rainRatemm_hr = rainRatemm_hr;
}
else {
rainRatemm_hr = 0;
};
//Serial.print("Total Tips: ");
//Serial.print((float) rainTrigger);
//Serial.print("; ");
//Serial.print("Total Rain: ");
Serial.print((float) totalRain);
//Serial.print(" mm; ");
Serial.print(", ");
//Serial.print("Rain Rate: ");
Serial.println((float) rainRatemm_hr);
//Serial.println(" mm/hr.");
}
}