Chat freely about anything...

User avatar
By Sirquil
#69908 Demo version; plan further development in new project. Sketch keeps track of elapsed time.
Code: Select all/*
 
  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
Demonstration --elapsed time tracker.
(1.34 KiB) Downloaded 227 times
User avatar
By Sirquil
#78119 ESP8266 Sketch using an Interrupt to find elapsed time:

Code: Select all/*******************************************

      RunTime II
      Will be used for Furnace runtime monitoring along with web server and data logging;
      Pin D6 will be connected to output of Opto-isolator..

Circuit to simulate furnace motor:   
3.3 Volts ____ push button, Switch   (N.O)    other, side of push button, Switch plus D6__/\/\/\/\__ 10K ohm_Resisitor_____Ground

September 6, 2018   William

*********************************************/



#define furnace D6

volatile int dayFlag = 0;

unsigned long int start, finished, elapsed;

int value;

void runTime()
{

     if (dayFlag == 0)
     {
          value = digitalRead (furnace);

          if (value == 1)
          {
               Serial.println("Motor ON");
               start = millis();
               dayFlag = 0;
          }

          if (value == 0)
          {
               Serial.print("");
               Serial.println("Motor OFF");
               finished = millis();
               elapsed = finished - start;
               dayFlag = 1;
          }
     }
}

void setup ()
{
     Serial.begin(9600);
     pinMode (furnace, INPUT_PULLUP);

     attachInterrupt (digitalPinToInterrupt (furnace), runTime, CHANGE);

}

void loop ()
{
     delay(0);

     if (dayFlag)
     {
          Serial.print("Total elapsed milliseconds:  ");
          Serial.println(elapsed);
          Serial.println("");
          elapsed = 0;
     }
     dayFlag = 0;

}




Better results of elapsed time using millis function; along with interrupt, rather than trying to use an alarm counter; as in my first post.

William
Attachments
4N25 OptoIsolator Schematic.JPG
Circuit used for testing "RunTime.ino."
Elapsed time using millis function; along with interrupt.
(739 Bytes) Downloaded 211 times