Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Bluetooth Flowmeter
- - Source Code compiled for: Arduino Nano
- - Source Code created on: 2025-09-09 14:05:33
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* I need to have an Arduino Nano indicate the flow */
- /* value of a YF-S201Y flowmeter that connects to a */
- /* cell phone via Bluetooth to be able to pass the */
- /* flow information in real time */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <SoftwareSerial.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void flowISR(void);
- /****** GLOBAL CONSTANTS AND VARIABLES *****/
- #define FLOW_PIN 2
- #define LED_PIN 13
- #define PULSE_PER_LITER 450.0
- // Bluetooth module connected to pins 10 (RX) and 11 (TX)
- SoftwareSerial BTSerial(10, 11); // RX, TX
- volatile unsigned long pulseCount = 0;
- unsigned long lastPulseCount = 0;
- unsigned long lastMillis = 0;
- float flowRate = 0.0;
- float totalLiters = 0.0;
- static unsigned long ledOnTime = 0; // LED on indicator for a short pulse
- /****** INTERRUPT SERVICE ROUTINE *******/
- void flowISR(void)
- {
- pulseCount++;
- }
- void setup(void)
- {
- // put your setup code here, to run once:
- Serial.begin(115200); // USB serial for debugging
- BTSerial.begin(9600); // Bluetooth module serial
- pinMode(FLOW_PIN, INPUT_PULLUP);
- pinMode(LED_PIN, OUTPUT);
- digitalWrite(LED_PIN, LOW);
- // Attach interrupt on rising edge of flow sensor signal
- attachInterrupt(digitalPinToInterrupt(FLOW_PIN), flowISR, RISING);
- lastMillis = millis();
- lastPulseCount = 0;
- }
- void loop(void)
- {
- unsigned long now = millis();
- static unsigned long lastOutputMillis = 0;
- // Update and transmit data once per second
- if ((now - lastOutputMillis) >= 1000)
- {
- unsigned long currentCount;
- unsigned long delta;
- unsigned long dt;
- // Copy pulse count atomically
- noInterrupts();
- currentCount = pulseCount;
- delta = currentCount - lastPulseCount;
- lastPulseCount = currentCount;
- interrupts();
- dt = now - lastOutputMillis;
- if (dt == 0) dt = 1;
- // Calculate flow rate in L/min and total liters
- flowRate = (delta) * (60000.0 / (float)dt) / PULSE_PER_LITER;
- totalLiters = currentCount / PULSE_PER_LITER;
- char msg[64];
- snprintf(msg, sizeof(msg), "FLOW:%.3f L/min; TOTAL:%.3f L\n", flowRate, totalLiters);
- Serial.print(msg);
- BTSerial.print(msg);
- // Visual indicator: brief LED pulse on data update
- if (delta > 0)
- {
- digitalWrite(LED_PIN, HIGH);
- ledOnTime = now;
- }
- if (ledOnTime && (now - ledOnTime) > 40)
- {
- digitalWrite(LED_PIN, LOW);
- ledOnTime = 0;
- }
- lastOutputMillis = now;
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment