Advertisement
safwan092

Untitled

Dec 4th, 2021
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3. LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
  4. byte statusLed = 13;
  5.  
  6. byte sensorInterrupt = 0; // 0 = digital pin 2
  7. byte sensorPin = 2;
  8.  
  9. // The hall-effect flow sensor outputs approximately 4.5 pulses per second per
  10. // litre/minute of flow.
  11. float calibrationFactor = 4.5;
  12.  
  13. volatile byte pulseCount;
  14.  
  15. float flowRate;
  16. unsigned int flowMilliLitres;
  17. unsigned long totalMilliLitres;
  18.  
  19. unsigned long oldTime;
  20.  
  21. void setup()
  22. {
  23. lcd.init();
  24. lcd.init();
  25. lcd.backlight();
  26. // Initialize a serial connection for reporting values to the host
  27. Serial.begin(9600);
  28.  
  29. // Set up the status LED line as an output
  30. pinMode(statusLed, OUTPUT);
  31. digitalWrite(statusLed, HIGH); // We have an active-low LED attached
  32.  
  33. pinMode(sensorPin, INPUT);
  34. digitalWrite(sensorPin, HIGH);
  35.  
  36. pulseCount = 0;
  37. flowRate = 0.0;
  38. flowMilliLitres = 0;
  39. totalMilliLitres = 0;
  40. oldTime = 0;
  41.  
  42. // The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
  43. // Configured to trigger on a FALLING state change (transition from HIGH
  44. // state to LOW state)
  45. attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  46. }
  47.  
  48. /**
  49. Main program loop
  50. */
  51. void loop()
  52. {
  53.  
  54. if ((millis() - oldTime) > 1000) // Only process counters once per second
  55. {
  56. // Disable the interrupt while calculating flow rate and sending the value to
  57. // the host
  58. detachInterrupt(sensorInterrupt);
  59.  
  60. // Because this loop may not complete in exactly 1 second intervals we calculate
  61. // the number of milliseconds that have passed since the last execution and use
  62. // that to scale the output. We also apply the calibrationFactor to scale the output
  63. // based on the number of pulses per second per units of measure (litres/minute in
  64. // this case) coming from the sensor.
  65. flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
  66.  
  67. // Note the time this processing pass was executed. Note that because we've
  68. // disabled interrupts the millis() function won't actually be incrementing right
  69. // at this point, but it will still return the value it was set to just before
  70. // interrupts went away.
  71. oldTime = millis();
  72.  
  73. // Divide the flow rate in litres/minute by 60 to determine how many litres have
  74. // passed through the sensor in this 1 second interval, then multiply by 1000 to
  75. // convert to millilitres.
  76. flowMilliLitres = (flowRate / 60) * 1000;
  77.  
  78. // Add the millilitres passed in this second to the cumulative total
  79. totalMilliLitres += flowMilliLitres;
  80.  
  81. unsigned int frac;
  82. flowRate = flowRate/2.8;
  83. // Print the flow rate for this second in litres / minute
  84. Serial.print("Flow rate: ");
  85. Serial.print(int(flowRate)); // Print the integer part of the variable
  86. Serial.print("L/min");
  87. Serial.print("\t"); // Print tab space
  88. lcd.setCursor(0, 0);
  89. lcd.print("Flow Rate:");
  90. lcd.setCursor(0, 1);
  91. if (flowRate < 10) {
  92. lcd.print(int(flowRate));
  93. lcd.setCursor(1, 1);
  94. lcd.print(" L/min ");
  95. }
  96. else {
  97. lcd.print(int(flowRate));
  98. lcd.setCursor(2, 1);
  99. lcd.print(" L/min");
  100. }
  101. // Print the cumulative total of litres flowed since starting
  102. Serial.print("Output Liquid Quantity: ");
  103. Serial.print(totalMilliLitres);
  104. Serial.println("mL");
  105. Serial.print("\t"); // Print tab space
  106. Serial.print(totalMilliLitres / 1000);
  107. Serial.print("L");
  108.  
  109.  
  110. // Reset the pulse counter so we can start incrementing again
  111. pulseCount = 0;
  112.  
  113. // Enable the interrupt again now that we've finished sending output
  114. attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  115. }
  116. }
  117.  
  118. /*
  119. Insterrupt Service Routine
  120. */
  121. void pulseCounter()
  122. {
  123. // Increment the pulse counter
  124. pulseCount++;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement