I think you should be a bit more clear about what your problem is, why you think you have one, and what you are really trying to do. Both programmes appear to be for a flow meter, the first because it says it is, the second because it looks like one.
The loop has to run continuously, that is what loops do. If you want to do something with the data, you include the relevant commands in the loop, and the interrupt keeps counting in the meantime.
/**
- Water Flow Gauge
- Uses a hall-effect flow sensor to measure the rate of water flow
- Copyright 2009 Jonathan Oxer jon@oxer.com.au
- Copyright 2009 Hugh Blemings hugh@blemings.org
*/
#include <LiquidCrystal_I2C.h>
#include “Wire.h”
#include <SD.h>
#define DS1307_ADDRESS 0x68
LiquidCrystal_I2C lcd(0x27,20,4);
File myFile;
char filename = “00000000.CSV”;
#include “Wire.h”
byte sensorInterrupt = 0; // 0 = pin 2; 1 = pin 3
byte sensorPin = 2; //
// The flow sensor outputs approximately 4.5 Hz/litre/minute of flow.
float calibrationFactor = 9.0;
volatile byte pulseCount;
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long oldTime;
int second, minute, hour, weekDay, monthDay, month, year;
int daily, maxflow, yesterday;
void setup()
{
lcd.setCursor (0,0);
lcd.print(“Initializing”);
delay(2000);
lcd.setCursor (0,1);
pinMode(10, OUTPUT);
if (!SD.begin(4))
{
lcd.print(“failed!”);
delay (2000);
return;
}
lcd.print(“init. OK!”);
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Flow: ");
lcd.setCursor(11, 0);
lcd.print(“L/min”);
lcd.setCursor(0, 1);
lcd.print("Total: ");
lcd.setCursor(14, 1);
lcd.print(“Litres”);
lcd.setCursor(0, 2);
lcd.print(“daily”);
lcd.setCursor(14, 2);
lcd.print(“Litres”);
// Initialize a serial connection for reporting values to the host
// Set up the pair of counter reset buttons and activate internal pull-up resistors
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH);
pulseCount = 0;
flowRate = 0.0;
flowMilliLitres = 0;
totalMilliLitres = 0;
oldTime = 0;
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
void loop()
{
GetClock();
if (hour == 6 && minute == 0 && second <2)
{
filewrite();
}
if((millis() - oldTime) > 1000) // Only process counters once per second
{
// Disable the interrupt while calculating flow rate and sending the value to
// the host
detachInterrupt(sensorInterrupt);
flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
oldTime = millis();
// Divide the flow rate in litres/minute by 60 to determine how many litres have
// passed through the sensor in this 1 second interval, then conv. to ml.
flowMilliLitres = (flowRate / 60) * 1000;
// Add the litres passed in this second to the cumulative total
totalMilliLitres += flowMilliLitres;
unsigned int frac;
frac = (flowRate - int(flowRate)) * 10;
lcd.setCursor(6, 0);
if(int(flowRate) < 10)
{
lcd.print(" ");
}
lcd.print((int)flowRate); // Print the integer part of the variable
lcd.print(’.’); // Print the decimal point
lcd.print(frac, DEC) ; // Print the fractional part of the variable
lcd.setCursor(7, 1);
lcd.print(int(totalMilliLitres / 1000));
// Reset the pulse counter so we can start incrementing again
pulseCount = 0;
// Enable the interrupt again now that we’ve finished sending output
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
}
/**
- Invoked by interrupt0 once per rotation of the hall-effect sensor. Interrupt
- handlers should be kept as small as possible so they return quickly.
*/
void pulseCounter()
{
// Increment the pulse counter
pulseCount++;
}
byte bcdToDec(byte val) {
// Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );
}
void GetClock(){
// Reset the register pointer
Wire.beginTransmission(DS1307_ADDRESS);
byte zero = 0x00;
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
second = bcdToDec(Wire.read());
minute = bcdToDec(Wire.read());
hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
weekDay = bcdToDec(Wire.read()); //0-6 → sunday - Saturday
monthDay = bcdToDec(Wire.read());
month = bcdToDec(Wire.read());
year = bcdToDec(Wire.read());
}
void filewrite() {
daily=totalMilliLitres-yesterday;
yesterday=totalMilliLitres;
myFile = SD.open(filename, FILE_WRITE);//<<<<<<<<<<<<< OPEN
myFile.print(year);
myFile.print(":");
myFile.print(month);
myFile.print(":");
myFile.print(monthDay);
myFile.print(",");
myFile.print(int(daily / 1000));
myFile.print(",");
myFile.println(int(totalMilliLitres / 1000));
myFile.close();//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CLOSE
}