pleasedontcode

Flow Telemetry rev_01

Sep 9th, 2025
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Flow Telemetry
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2025-09-09 13:19:28
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* I need to have an Arduino Nano indicate the flow */
  21.     /* value of a YF-S201Y flowmeter that connects to a */
  22.     /* cell phone via Bluetooth to be able to pass the */
  23.     /* flow information in real time */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <SoftwareSerial.h>
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void flowISR(void);
  36.  
  37. /****** GLOBAL CONSTANTS AND VARIABLES *****/
  38. #define FLOW_PIN 2
  39. #define LED_PIN 13
  40. #define PULSE_PER_LITER 450.0
  41.  
  42. // Bluetooth module connected to pins 10 (RX) and 11 (TX)
  43. SoftwareSerial BTSerial(10, 11); // RX, TX
  44.  
  45. volatile unsigned long pulseCount = 0;
  46. unsigned long lastPulseCount = 0;
  47. unsigned long lastMillis = 0;
  48. float flowRate = 0.0;
  49. float totalLiters = 0.0;
  50. static unsigned long ledOnTime = 0; // LED on indicator for a short pulse
  51.  
  52. /****** INTERRUPT SERVICE ROUTINE *******/
  53. void flowISR(void)
  54. {
  55.   pulseCount++;
  56. }
  57.  
  58. void setup(void)
  59. {
  60.   // put your setup code here, to run once:
  61.   Serial.begin(115200);    // USB serial for debugging
  62.   BTSerial.begin(9600);    // Bluetooth module serial
  63.  
  64.   pinMode(FLOW_PIN, INPUT_PULLUP);
  65.   pinMode(LED_PIN, OUTPUT);
  66.   digitalWrite(LED_PIN, LOW);
  67.  
  68.   // Attach interrupt on rising edge of flow sensor signal
  69.   attachInterrupt(digitalPinToInterrupt(FLOW_PIN), flowISR, RISING);
  70.  
  71.   lastMillis = millis();
  72.   lastPulseCount = 0;
  73. }
  74.  
  75. void loop(void)
  76. {
  77.   unsigned long now = millis();
  78.   static unsigned long lastOutputMillis = 0;
  79.  
  80.   // Update and transmit data once per second
  81.   if ((now - lastOutputMillis) >= 1000)
  82.   {
  83.     unsigned long currentCount;
  84.     unsigned long delta;
  85.     unsigned long dt;
  86.  
  87.     // Copy pulse count atomically
  88.     noInterrupts();
  89.     currentCount = pulseCount;
  90.     delta = currentCount - lastPulseCount;
  91.     lastPulseCount = currentCount;
  92.     interrupts();
  93.  
  94.     dt = now - lastOutputMillis;
  95.     if (dt == 0) dt = 1;
  96.  
  97.     // Calculate flow rate in L/min and total liters
  98.     flowRate = (delta) * (60000.0 / (float)dt) / PULSE_PER_LITER;
  99.     totalLiters = currentCount / PULSE_PER_LITER;
  100.  
  101.     char msg[64];
  102.     snprintf(msg, sizeof(msg), "FLOW:%.3f L/min; TOTAL:%.3f L\n", flowRate, totalLiters);
  103.  
  104.     Serial.print(msg);
  105.     BTSerial.print(msg);
  106.  
  107.     // Visual indicator: brief LED pulse on data update
  108.     if (delta > 0)
  109.     {
  110.       digitalWrite(LED_PIN, HIGH);
  111.       ledOnTime = now;
  112.     }
  113.  
  114.     if (ledOnTime && (now - ledOnTime) > 40)
  115.     {
  116.       digitalWrite(LED_PIN, LOW);
  117.       ledOnTime = 0;
  118.     }
  119.  
  120.     lastOutputMillis = now;
  121.   }
  122. }
  123.  
  124. /* END CODE */
  125.  
Advertisement
Add Comment
Please, Sign In to add comment