Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. /*
  2. description: Rain Gauge with arduino with serial monitoring
  3. Reports the daily-rain and rain-in-last-hour in inches
  4. */
  5.  
  6. #include "RTClib.h"
  7. #include <Wire.h>
  8. #define RainPin 12 // The Rain input is connected to digital pin 2 on the arduino
  9.  
  10.  
  11. bool bucketPositionA = false; // one of the two positions of tipping-bucket
  12. const double bucketAmount = 0.053; // 0.053 inches atau 1.346 mm of rain equivalent of ml to trip tipping-bucket
  13.  
  14. //note:
  15. //diketahui bahwa 1 inchi = 2.54 cm
  16. //diketahui bahwa 1 tip sensor ini = 2.6 mL
  17. //maka 0.053 inchi of rain didapat dari:
  18. //panjang rain collector = 5.4 cm atau 2.126 inchi
  19. //lebar rain collector = 3.6 cm atau 1.417 inchi
  20. //luas=pxl -> 2.16 inchi x 1.417 inchi = 3.012 inchi persegi
  21. //U.S. measures rain in inches so it would be 3.012 inchi kubik
  22. //lalu dikonversi dari inchi kubik mjd mL dan didapat bahwa 3.012 inchi kubik = 49.358 mL
  23. //yg artinya 1 inchi of rain = 49.358 mL
  24. //sehingga 1 tip sensor ini mewakili 2.6 mL/49.358 mL = 0.053 inchi of rain
  25.  
  26. double dailyRain = 0.0; // rain accumulated for the day
  27. double hourlyRain = 0.0; // rain accumulated for one hour
  28. double dailyRain_till_LastHour = 0.0; // rain accumulated for the day till the last hour
  29. bool first; // as we want readings of the (MHz) loops only at the 0th moment
  30.  
  31. RTC_Millis rtc; // software RTC time
  32.  
  33.  
  34. void setup(void) {
  35. Serial.begin(9600); // start the serial port
  36. rtc.begin(DateTime(_DATE, __TIME_)); // start the RTC
  37. pinMode(RainPin, INPUT); // set the Rain Pin as input.
  38. delay(4000); // wait the serial monitor
  39. Serial.println("Rain Gauge Ready !!"); // rain gauge measured per 1 hour
  40. Serial.println("execute calculations once per hour !!");
  41. }
  42.  
  43.  
  44. void loop(void){
  45. DateTime now = rtc.now();
  46.  
  47. // ++++++++++++++++++++++++ Count the bucket tips ++++++++++++++++++++++++++++++++
  48. if ((bucketPositionA==false)&&(digitalRead(RainPin)==LOW)){
  49. bucketPositionA=true;
  50. dailyRain+=bucketAmount; // update the daily rain
  51. }
  52.  
  53. if ((bucketPositionA==true)&&(digitalRead(RainPin)==HIGH)){
  54. bucketPositionA=false;
  55. }
  56. // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  57.  
  58. if(now.minute() != 0){
  59. first = true;
  60. //Serial.println("the fuck");// after the first minute is over, be ready for next read
  61. }
  62.  
  63. if(first == true){
  64.  
  65. hourlyRain = dailyRain - dailyRain_till_LastHour; // calculate the last hour's rain
  66. dailyRain_till_LastHour = dailyRain; // update the rain till last hour for next calculation
  67.  
  68. // fancy display for humans to comprehend
  69. Serial.println();
  70. Serial.print(now.minute());
  71. Serial.print(":");
  72. Serial.print(now.second());
  73. Serial.print(": Total Rain for the day = ");
  74. Serial.print(dailyRain,3); // the '3' ensures the required accuracy digit dibelakang koma
  75. Serial.print(" inches atau ");
  76. Serial.print(dailyRain*2.54*10,3);
  77. Serial.println(" mm");
  78. Serial.println();
  79. Serial.print(" : Rain in last hour = ");
  80. Serial.print(hourlyRain,3);
  81. Serial.print(" inches atau ");
  82. Serial.print(hourlyRain*2.54*10,3);
  83. Serial.println(" mm");
  84. Serial.println();
  85.  
  86. first = false; // execute calculations only once per hour
  87. }
  88.  
  89. if(now.minute()== 0) {
  90. dailyRain = 0.0; // clear daily-rain at midnight
  91. dailyRain_till_LastHour = 0.0; // we do not want negative rain at 01:00
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement