/*
Run time Calculator 09/17/2017 @ 07:40 EST
Developed for RobotDyn WiFi D1 R2 board with SPIFFS
Developed by Sirquil
*/
#include<FS.h>
#include <TimeLib.h>
#include <TimeAlarms.h>
int PIN2;
int count = 1;
int x;
int endOfCycle;
int hour1;
int minute1;
int second1;
int runTime = 0;
int totalRunTime;
int totalForEvent;
int event = 0;
int totalForDay;
int totalDay;
int totalForYear;
int totalYear;
String tFD;
String tFY;
AlarmId id;
void setup()
{
SPIFFS.begin();
//SPIFFS.format();
//SPIFFS.remove("/TOTAL.TXT");
//SPIFFS.remove("/YEAR.TXT");
Serial.begin(9600);
while (!Serial) ; // wait for Arduino Serial Monitor
id = Alarm.timerRepeat(5, Repeats5); // timer for every 5 seconds
}
void loop()
{
//digitalClockDisplay();
Alarm.delay(1000); // wait one second between clock display
}
void Repeats5()
{
readDay();
PIN2 = 1;
if(PIN2 = 1)
{
if (count != 6)
{
event++;
runTime++;
Serial.println("");
Serial.println(" 5 Seconds");
totalForEvent = 0;
totalForEvent = runTime;
Serial.println(" total for event " + ((String)(totalForEvent * 5)));
Serial.println(" " + (String)event + " of 5 for duration of on-time");
totalForDay = 0;
if(event != 6)
{
totalForDay = totalYear + totalForEvent;
Serial.println(" total for year " + ((String)(totalForDay * 5)) + " seconds");
Serial.println(" count " + (String)count + " of 5 events till newDay");
}
count++;
}
else
{
newDay();
}
}
}
////////////////////////
void newDay()
{
//Serial.println(" newDay ");
writeDay(); //write total for today
readDay(); //read --total for day from yesterday
writeYear(); //write --yesterday and today totals for day
readYear(); //read --yearly total for day current value
runTime = 0;
event = 0;
delay(1);
totalForEvent = 0;
count = 1;
}
/////////////
void writeDay() //write total for today
{
File logFile = SPIFFS.open("/TOTAL.TXT", "w");
if (!logFile)
{
Serial.println(" File failed to open");
}
logFile.print(totalForDay);
logFile.close();
//Serial.println(" writeDay " + (String)(totalForDay * 5));
}
///////////////
void readDay() //read --total for day from yesterday
{
File logFile = SPIFFS.open("/TOTAL.TXT", "r");
if (!logFile)
{
Serial.println(" readDay failed to open");
Serial.println("\n");
}
else
{
String tFD = logFile.readStringUntil('\n');
Serial.println("");
totalDay = tFD.toInt();
logFile.close();
//Serial.println(" readDay --totalDay " + (String)(totalDay * 5));
}
}
////////////////////////
void writeYear() //write --yesterday and today totals for day
{
File logFile = SPIFFS.open("/YEAR.TXT", "w");
if (!logFile)
{
Serial.println(" File fFY1 failed to open");
Serial.println("\n");
}
else
{
totalYear = totalYear + totalDay;
logFile.print(totalForYear);
logFile.close();
//Serial.println(" writeDay --current total for day " + (String)(totalForDay * 5) + " seconds");
}
}
///////////////
void readYear() //read --yearly total for day current value
{
File logFile = SPIFFS.open("/YEAR.TXT", "r");
if (!logFile)
{
Serial.println("File fFY2 failed to open");
Serial.println("\n");
}
else
{
String tFY = logFile.readStringUntil('\n');
totalYear = tFY.toInt();
totalYear = totalYear + totalDay;
Serial.println(" totalYear " + (String)(totalYear * 5) + " seconds");
Serial.println("");
}
}
Time interval is easily changed.
William
Attachments
(1.34 KiB) Downloaded 227 times