Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. byte sensorInterrupt = 1;
  2. byte sensorPin = 1;
  3. float calibrationFactor = 4.5; // According to docs: the sensor outputs 4.5 pulses/sec per L/min of flow
  4. volatile byte pulseCount;
  5. float flowRate;
  6. unsigned int flowMilliLitres;
  7. unsigned long totalMilliLitres;
  8. unsigned long oldTime;
  9. void setup()
  10. {
  11.  
  12. Serial.begin(9600);
  13.  
  14. pinMode(sensorPin, INPUT);
  15. digitalWrite(sensorPin, HIGH);
  16. pulseCount = 0;
  17. flowRate = 0.0;
  18. flowMilliLitres = 0;
  19. totalMilliLitres = 0;
  20. oldTime = 0;
  21. attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  22. }
  23. void loop()
  24. {
  25.  
  26. if((millis() - oldTime) > 1000) // one counter persecond
  27. {
  28. detachInterrupt(sensorInterrupt);
  29. flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
  30. oldTime = millis();
  31. flowMilliLitres = (flowRate / 60) * 1000;
  32. totalMilliLitres += flowMilliLitres;
  33.  
  34. unsigned int frac;
  35.  
  36. Serial.print("Flow rate: ");
  37. Serial.print((flowRate));
  38. Serial.print("L/min");
  39. Serial.print("\t");
  40. Serial.print("Output Liquid Quantity: ");
  41. Serial.print(totalMilliLitres);
  42. Serial.println("mL");
  43. Serial.print("\t");
  44. Serial.print(totalMilliLitres/1000);
  45. Serial.print("L");
  46.  
  47. pulseCount = 0;
  48. attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  49. }
  50. }
  51. void pulseCounter()
  52. {
  53. pulseCount++;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement