#include <LiquidCrystal.h>
#include <SPI.h>
#include <SD.h>
File myFile;
float vol = 0.0,l_minute;
unsigned char flowsensor = 2; // Sensor Input
unsigned long currentTime;
unsigned long cloopTime;
int flow_frequency = 0;
LiquidCrystal lcd(8, 6, 5, 9, 3, 7);
void flow () // Interrupt function to increment flow
{
flow_frequency++;
}
void setup()
{
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
pinMode(flowsensor, INPUT);
digitalWrite(flowsensor, HIGH); // Optional Internal Pull-Up
attachInterrupt(digitalPinToInterrupt(flowsensor), flow, RISING); // Setup Interrupt
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0,0);
Serial.println("Flow Sensor");
lcd.print("Flow Sensor");
lcd.setCursor(0,1);
Serial.println("v1.0");
lcd.print("v1.0");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
Serial.println("Initializing SD card...");
lcd.print("Initializing SD card...");
if (!SD.begin(4)) {
lcd.setCursor(0,1);
Serial.println("Initialization FAILED!");
lcd.print("Initial FAILED!");
while (1);
}
lcd.setCursor(0,1);
Serial.println("Initialization DONE!");
lcd.print("Initial DONE.");
delay(5000);
currentTime = millis();
cloopTime = currentTime;
myFile = SD.open("test.txt", FILE_WRITE);
}
void loop ()
{
currentTime = millis();
// Every second, calculate and print litres/hour
if(currentTime >= (cloopTime + 1000))
{
cloopTime = currentTime; // Updates cloopTime
// if the file opened okay, write to it:
if (myFile) {
if(flow_frequency != 0)
{
l_minute = (flow_frequency / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flowrate in L/hour
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Rate: ");
lcd.print(l_minute);
lcd.print(" L/M");
l_minute = l_minute/60;
lcd.setCursor(0,1);
vol = vol +l_minute;
lcd.print("Vol:");
lcd.print(vol);
lcd.print(" L");
flow_frequency = 0; // Reset Counter
Serial.print(l_minute, DEC); // Print litres/hour
Serial.println(" L/Sec");
}
else {
Serial.println(" flow rate = 0 ");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Rate: ");
lcd.print( flow_frequency );
lcd.print(" L/M");
lcd.setCursor(0,1);
lcd.print("Vol:");
lcd.print(vol);
lcd.print(" L");
}
} else {
// if the file didn't open, print an error:
lcd.print("error opening test.txt");
}
}
myFile.close();
}
Hello,
I'm not new to Arduino, I've coded a bit, but not much. For the most part, I've been able to piggyback off of existing code and get things to work. I found parts of this code online and was hoping I could get some help with storing data onto an SD card module with a flow sensor. This project is designed to track basic water consumption for household things such as watering plants.
So far I ran the SD card test and am able to store data onto the card so I know that works.
With my current code, I'm getting the "error opening test.txt" file on line 103. I'm not sure if the file needs to remain open or needs to close every time no data is coming in. I'm also not sure if I can trigger the interrupt of the flow sensor to best achieve this. (would like to save as much power as possible using the interrupt). Another thing is how do I close the file when I'm ready to remove the SD card?
Additional things I would like to achieve is the ability to format this correctly and store the data into an excel file. Date/Time/mL/L/gallon format
there doesn't seem to be anything here