pleasedontcode

Bluetooth Flowmeter rev_02

Sep 9th, 2025
34
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: Bluetooth Flowmeter
  13.     - Source Code compiled for: Arduino Nano
  14.     - Source Code created on: 2025-09-09 14:05:33
  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.  
  28.  
  29. /* START CODE */
  30.  
  31. /****** DEFINITION OF LIBRARIES *****/
  32. #include <SoftwareSerial.h>
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37. void flowISR(void);
  38.  
  39. /****** GLOBAL CONSTANTS AND VARIABLES *****/
  40. #define FLOW_PIN 2
  41. #define LED_PIN 13
  42. #define PULSE_PER_LITER 450.0
  43.  
  44. // Bluetooth module connected to pins 10 (RX) and 11 (TX)
  45. SoftwareSerial BTSerial(10, 11); // RX, TX
  46.  
  47. volatile unsigned long pulseCount = 0;
  48. unsigned long lastPulseCount = 0;
  49. unsigned long lastMillis = 0;
  50. float flowRate = 0.0;
  51. float totalLiters = 0.0;
  52. static unsigned long ledOnTime = 0; // LED on indicator for a short pulse
  53.  
  54. /****** INTERRUPT SERVICE ROUTINE *******/
  55. void flowISR(void)
  56. {
  57.   pulseCount++;
  58. }
  59.  
  60. void setup(void)
  61. {
  62.   // put your setup code here, to run once:
  63.   Serial.begin(115200);    // USB serial for debugging
  64.   BTSerial.begin(9600);    // Bluetooth module serial
  65.  
  66.   pinMode(FLOW_PIN, INPUT_PULLUP);
  67.   pinMode(LED_PIN, OUTPUT);
  68.   digitalWrite(LED_PIN, LOW);
  69.  
  70.   // Attach interrupt on rising edge of flow sensor signal
  71.   attachInterrupt(digitalPinToInterrupt(FLOW_PIN), flowISR, RISING);
  72.  
  73.   lastMillis = millis();
  74.   lastPulseCount = 0;
  75. }
  76.  
  77. void loop(void)
  78. {
  79.   unsigned long now = millis();
  80.   static unsigned long lastOutputMillis = 0;
  81.  
  82.   // Update and transmit data once per second
  83.   if ((now - lastOutputMillis) >= 1000)
  84.   {
  85.     unsigned long currentCount;
  86.     unsigned long delta;
  87.     unsigned long dt;
  88.  
  89.     // Copy pulse count atomically
  90.     noInterrupts();
  91.     currentCount = pulseCount;
  92.     delta = currentCount - lastPulseCount;
  93.     lastPulseCount = currentCount;
  94.     interrupts();
  95.  
  96.     dt = now - lastOutputMillis;
  97.     if (dt == 0) dt = 1;
  98.  
  99.     // Calculate flow rate in L/min and total liters
  100.     flowRate = (delta) * (60000.0 / (float)dt) / PULSE_PER_LITER;
  101.     totalLiters = currentCount / PULSE_PER_LITER;
  102.  
  103.     char msg[64];
  104.     snprintf(msg, sizeof(msg), "FLOW:%.3f L/min; TOTAL:%.3f L\n", flowRate, totalLiters);
  105.  
  106.     Serial.print(msg);
  107.     BTSerial.print(msg);
  108.  
  109.     // Visual indicator: brief LED pulse on data update
  110.     if (delta > 0)
  111.     {
  112.       digitalWrite(LED_PIN, HIGH);
  113.       ledOnTime = now;
  114.     }
  115.  
  116.     if (ledOnTime && (now - ledOnTime) > 40)
  117.     {
  118.       digitalWrite(LED_PIN, LOW);
  119.       ledOnTime = 0;
  120.     }
  121.  
  122.     lastOutputMillis = now;
  123.   }
  124. }
  125.  
  126. /* END CODE */
  127.  
Advertisement
Add Comment
Please, Sign In to add comment