I have programmed myself into a corner and I am wondering if this concept is even logically possible.
I am creating a program to complement a DS1302 based on off timer.
If the output of that timer is ON and the power is cut to the board it will delay restart if a prescribed amount of time has yet to ellaspe.
So the idea so far is this:
At power up the mega checks the DS1302 SRAM where a unix timestamp has previously been stored.
The time stamp is compared to now and if enough time hasn't elapsed it sets a ready bit to false until it has. it then start writing Unix to SRAM and the cylcle starts again storing UNIX to SRAM
Except that's the problem I have created a loop.
The issue is if I re write the timestamp to SRAM once the timer has finished the difference between the stored timestamp and now time resets and I just have a loop.
One idea I had was to create two places to store a timestamp and alternate their use each time this function is triggered using a flag bit stored on SRAM. so storing the the new time wont effect the current delay under test.
#include<DS1302RTC.h>
#include<TimeLib.h>
DS1302RTC RTC(27, 29, 31);
#define DS1302_GND_PIN 33
#define DS1302_VCC_PIN 35
boolean ready= false;
int8_t power_upDelay= 20;
unsigned long preMillis = 0;
tmElements_t tm;
//----------------------------------SRAM WRITE Function---------------------------------------------------//
void sramWrite(uint32_t intVal){
union {
uint32_t splitInt;
byte byteVal[4];
} intAsBytes;
intAsBytes.splitInt = intVal;
RTC.writeEN(true);
if(RTC.writeEN())
Serial.println("ready");
else
Serial.println("No write");
for (int i=0;i<4;i++){
RTC.writeRTC(DS1302_RAM_START+(i << 1),intAsBytes.byteVal[i]);
}
}
//------------------------------------SRAM READ Function-----------------------------------------------//
uint32_t sramRead()
{
union {
uint8_t readBuffer[4];
uint32_t merge_var;
}result;
for (int x=0;x<4;x++){
result.readBuffer[x]= RTC.readRTC(DS1302_RAM_START+(x << 1));
}
uint32_t ram_read = result.merge_var;
return ram_read;
}
//--------------------------------------write unix to SRAM every second-------------------------------------------------//
void writeUnix ()
{
unsigned long curMillis = millis();
if(curMillis-preMillis>=1000){
sramWrite(RTC.get());
preMillis= curMillis;
}
}
void setup() {
digitalWrite(DS1302_GND_PIN, LOW);
pinMode(DS1302_GND_PIN, OUTPUT);
digitalWrite(DS1302_VCC_PIN, HIGH);
pinMode(DS1302_VCC_PIN, OUTPUT);
Serial.begin(9600);
if (! RTC.read(tm)) {
} else {
Serial.println("DS1302 read error.");
Serial.println();
}
}
void loop() {
// compare sramRead with unix if more than SetPoint continue to sramWrite unix and set timer ready true else false
int32_t diff = RTC.get() - sramRead();
if (diff <= power_upDelay) {
uint32_t countDown=(sramRead()+power_upDelay)- RTC.get();
ready= false;
Serial.println("Waiting");
Serial.println("remaining");
Serial.println(countDown);
}
else
{
Serial.println("ready");
ready=true;
writeUnix(); //write unix to SRAM
}
//Serial.println(sramRead()); //used for testing
//while(true); //just used for testing
}