Skip to content

Commit

Permalink
Added save interval to protect SD card
Browse files Browse the repository at this point in the history
  • Loading branch information
jordancrubin committed Jul 23, 2022
1 parent 7d114b5 commit 6607a7f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
26 changes: 14 additions & 12 deletions src/Watermeter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@
can be adjusted as parameters in the instance creation.
https://www.youtube.com/c/jordanrubin6502
2020 - 2022 Jordan Rubin.
*/
*/

#include <Arduino.h> // Required for Platform.io
#include <Watermeter.h>
#include <SD.h>
#include <FS.h>

int meterSignal;
bool gallon;
bool gallon = 0;
bool update;
long lastDebounce = 0; // Holds last debounce
long debounceDelay; // Between re-polling
long lastUpdate = 0;
double meter = 0;
float increment;
int saveint;
bool useSDcard;
void IRAM_ATTR respondInterrupt();

//CONSTRUCTOR -----------------------------------------------------------

//WATERMETER::WATERMETER(int signalGPIOpin, bool useInternalPullups,char measure, long dbounce ,bool useSD, float incr)
WATERMETER::WATERMETER(int signalGPIOpin, bool useInternalPullups,char measure, long dbounce ,bool useSD, float incr)
//WATERMETER::WATERMETER(int signalGPIOpin, bool useInternalPullups,char measure, long dbounce ,bool useSD, float incr, int saveInterval)
WATERMETER::WATERMETER(int signalGPIOpin, bool useInternalPullups,char measure, long dbounce ,bool useSD, float incr, int saveInterval)
{
//----------------- initialize initial parameters
if (useInternalPullups){
Expand All @@ -35,10 +36,10 @@ WATERMETER::WATERMETER(int signalGPIOpin, bool useInternalPullups,char measure,
pinMode(signalGPIOpin, INPUT);
}
if (measure == 'g'){gallon = 1;}
else {gallon = 0;}
attachInterrupt(signalGPIOpin, respondInterrupt, FALLING);
meterSignal = signalGPIOpin;
increment = incr;
saveint = saveInterval * 1000;
debounceDelay = dbounce;
useSDcard = useSD;
}
Expand All @@ -47,13 +48,14 @@ WATERMETER::WATERMETER(int signalGPIOpin, bool useInternalPullups,char measure,
// FUNCTION - [updated] - [Checks and responds to the update flag--------------]
bool WATERMETER::updated(void){
if (update){
writeFile();
if ((millis() - lastUpdate) > saveint) {
writeFile();
lastUpdate = millis();
}
update = 0;
return 1;
}
else {
return 0;
return 1;
}
return 0;
}
// ----------------------------------------------------------------------------]

Expand Down Expand Up @@ -157,7 +159,7 @@ void WATERMETER::setDebounce(int value){
// FUNCTION - [setMeter] - [Sets the meter from the physical display-----------]
void WATERMETER::setMeter(double value){
meter = value;
update = 1;
writeFile();
}
// ----------------------------------------------------------------------------]

Expand Down
6 changes: 3 additions & 3 deletions src/Watermeter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
class WATERMETER
{
////////////// user-accessible "public" interface
//WATERMETER(SignalGPIOpin,useInternalPullupResistor,measure[g|l],dbouncedelay,useSD,increment)
//WATERMETER(SignalGPIOpin,useInternalPullupResistor,measure[g|l],dbouncedelay,useSD,increment,saveinterval)
public:
WATERMETER(int,bool,char,long,bool,float);
WATERMETER(int,bool,char,long,bool,float,int);
bool updated(void);
int getDebounce(void);
const char* initFilesys(void);
Expand All @@ -27,7 +27,7 @@ class WATERMETER
void setMeter(double);
void setDebounce(int);

// ////////////// library-accessible "private" interface
//////////////// library-accessible "private" interface
private:
int value;
bool readFile(void);
Expand Down

0 comments on commit 6607a7f

Please sign in to comment.