unsigned long TimePeriod = 60000;
unsigned long LastCellNumber = 20000;
void setup() {
Serial.begin(9600);
Serial.println("Start sketch");
/* Please add EEPROM reading code at this line. */
}
void loop() {
myDelay();
}
void myDelay() {
delay(2000);
Serial.println("enter my delay");
Serial.println("StartTime");
static unsigned long StartTime = millis();
Serial.println(StartTime);
unsigned long now = millis();
if (now - StartTime < TimePeriod - LastCellNumber) return;
LastCellNumber = 0;
StartTime = now;
myTask();
}
void myTask() {
Serial.println("Do something here");
}
now this works fine if timer is being constantly being called, however this timer will be called at random intervals, how do i set the start time to refresh each time the function is called? previously i have used something like this
The function to handle the state machine should keep calling at short intervals.
Loops repeat at high speed, even if they require delayed processing.
The millis() is used to perform delayed processing even in repeated loops.
Otherwise coding with millis makes no sense.
That's why people avoid delay() function.
I think that is fine.
Can write as even more means as you like.
Yes, maybe it's like a trick...
I think It is standard to use flags to determine the condition of a state machine.
You have already figured out that in order to control the timer you must put the reset code in a conditional statement.
An if statement is only one kind of conditional statement.
How and when do you know the actual length of that next interval?
The usual pattern is if (now - starttime > interval) ...
to call myTask and reset starttime.
If the delay shall start only when the next interval length has been determined: if (armed && now - starttime > interval) ...
Then reset armed when myTask is started and set it again when the next interval shall begin.
const byte startSwitch = 2;
unsigned long TimePeriod = 6000;
unsigned long LastCellNumber = 2000;
bool timingFlag = false;
unsigned long StartTime;
//********************************************************
void setup()
{
Serial.begin(9600);
pinMode(startSwitch, INPUT_PULLUP);
Serial.println("Start sketch \n");
/* Please add EEPROM reading code at this line. */
} //END of setup()
//********************************************************
void loop()
{
//if we are not timing, should we start the TIMER ?
if(timingFlag == false && digitalRead(startSwitch) == LOW)
{
Serial.println("Timer has started");
//enable TIMER
timingFlag = true;
//restart the TIMER
StartTime = millis();
}
//if TIMER is enabled, check to see if it has timed out
if (timingFlag == true)
{
myDelay();
}
} //END of loop()
//********************************************************
//a one shot TIMER that starts when the startSwitch is first closed
void myDelay()
{
if (millis() - StartTime < TimePeriod - LastCellNumber)
{
//the TIMER has not timed out yet
return;
}
Serial.println("Timer has finished \n");
//don't know what this is ????????
LastCellNumber = 0;
//proceed to the task
myTask();
//disable the TIMER
timingFlag = false;
} //END of myDelay()
//********************************************************
void myTask()
{
Serial.println("Executing my Task \n");
} //END of myTask()