Trim can corrigate the timedrift
i.e. when the counter counts to slow
trim must be negativ ( - 1....10....)
and vice versa
ADDRnumber
Location from the internal eeprom
to save and reload the elapsed time
NSAVEboolean
nsave 'true' = power OK
nsave 'false' = power off -> counter values save to eeprom
CLEARpulse
Snumber
counted seconds
Mnumber
counted minutes
Hnumber
counted hours
Dnumber
counted days
To use the node in your project you should have the gst/workingtimecounter library installed. Use the “File → Add Library” menu item in XOD IDE if you don’t have it yet. See Using libraries for more info.
C++ implementation
#pragma XOD require "https://github.com/dancojocaru2000/ArduinoEEPROM"
#include <EEPROM.h>
node {
boolean saved ; // indicator for eeprom write done
boolean zero ;
TimeMs nextTrig;
void evaluate(Context ctx) {
TimeMs tNow = transactionTime();
auto addr = getValue<input_ADDR>(ctx);
auto trim = getValue<input_TRIM>(ctx);
auto notsave = getValue<input_NSAVE>(ctx);
TimeMs dt = 1000 + trim;
TimeMs tNext = tNow + dt;
auto isEnabled = getValue<input_EN>(ctx);
//------------------------------------------------------------------------------------------------------------
// clear the operatinghourscounter to zero
if (isInputDirty<input_CLEAR>(ctx)) {
uint16_t eeaddr = addr;
uint8_t countm = 0;
uint8_t counth = 0;
zero = true; // countervalues will cleared zo zero
EEPROM.write(eeaddr,countm); // write minutes to eeprom
eeaddr += 1;
EEPROM.write(eeaddr,counth); // write hours to eeprom
uint16_t countvalue = 0;
byte lowbyte = 0;
byte highbyte = 0;
word hlbyte = 0;
hlbyte = word(countvalue);
lowbyte =(( hlbyte) & 0x00FF);
highbyte =((hlbyte >> 8) & 0xFF);
eeaddr = addr;
eeaddr += 2;
EEPROM.write(eeaddr,lowbyte); // write days lowbyte to eeprom
eeaddr += 1;
EEPROM.write(eeaddr,highbyte); // write days highbyte to eeprom
}
//-------------------------------------------------------------------------------------------------------------
// reload the counter of minutes, hours and days after power on (seconds dont care)
if ((isSettingUp())|| ((zero) == true)){ // triggers reading of minutes, hours and days value from eeprom
uint16_t eeaddr = addr;
auto result = EEPROM.read(eeaddr); // low byte = minutes, high byte = hours
byte lowbyte = result;
eeaddr +=1;
result = EEPROM.read(eeaddr);
byte highbyte = result;
emitValue<output_S>(ctx,0);
uint8_t countm = ((lowbyte << 0) & 0x00FF) ;
emitValue<output_M>(ctx, countm);
uint8_t counth = ((highbyte << 0) & 0x00FF);
emitValue<output_H>(ctx, counth);
eeaddr +=1;
result = EEPROM.read(eeaddr); // low and highbyte = word = days
lowbyte = result;
eeaddr +=1;
result = EEPROM.read(eeaddr);
highbyte = result;
Number countd = ((lowbyte << 0) & 0x00FF) + ((highbyte << 8) & 0xFF00);
emitValue<output_D>(ctx, countd);
zero = false;
}
//------------------------------------------------------------------------------------------------------------
/* write the counter data in the eeprom befor power breaks down
use an input to detect power off and buffer the mc with an suitable capacitor */
if ((notsave) == true) {
saved = false;
}
if ( ( notsave) == false &&(saved) == false) {
uint8_t countm = getValue<output_M>(ctx);
uint8_t counth = getValue<output_H>(ctx);
Number countd = getValue<output_D>(ctx);
byte lowbyte = countm;
byte highbyte = counth;
uint16_t eeaddr = addr;
EEPROM.write(eeaddr,lowbyte); // write minutes to eeprom
eeaddr += 1;
EEPROM.write(eeaddr,highbyte); // write hours to eeprom
uint16_t countvalue = countd;
lowbyte = 0;
highbyte = 0;
word hlbyte = 0;
hlbyte = word(countvalue);
lowbyte =(( hlbyte) & 0x00FF);
highbyte =((hlbyte >> 8) & 0xFF);
eeaddr = addr;
eeaddr += 2;
EEPROM.write(eeaddr,lowbyte); // write days lowbyte to eeprom
eeaddr += 1;
EEPROM.write(eeaddr,highbyte); // write days highbyte to eeprom
saved = true; //after values saved in the eeprom variable 'saved' = true
}
//-------------------------------------------------------------------------------------------------------
if (isTimedOut(ctx) && isEnabled) {
uint8_t counts = getValue<output_S>(ctx);
uint8_t countm = getValue<output_M>(ctx);
uint8_t counth = getValue<output_H>(ctx);
Number countd = getValue<output_D>(ctx);
boolean ovh =false;
counts += 1 ; // seconds
if (counts > 59) {
counts = 0;
countm +=1;} //minutes
if (countm > 59) {
countm = 0;
counth +=1; //hours
if (counth > 23) {
counth = 0;
countd += 1;
}
}
nextTrig = tNext;
setTimeout(ctx, dt);
emitValue<output_S>(ctx, counts);
emitValue<output_M>(ctx, countm);
emitValue<output_H>(ctx, counth);
emitValue<output_D>(ctx, countd);
}
if (nextTrig < tNow || nextTrig > tNext) {
// Start timeout from scratch
nextTrig = tNext;
setTimeout(ctx, dt);
}
}
}
//________________________________________________________________________