Hello all,
I'm attempting to move the code from a current project onto a wifi board which will be controlled using blynk, and through research the delay function cannot be use, so just wondering whether this alternate delay function I have created will be able to run with blynk? It works when I run any code normally but not sure if there will be any issues when using it in conjunction with blynk.
Also, is there a risk of damaging anything by testing code like this with a board?
Thanks
//prevtime is defined as 0 at the beginning of the sketch
void altdelay (int delaylength) {
currenttime = millis();
while ( (currenttime - prevtime) < delaylength) {
currenttime = millis();
}
prevtime = millis();
}
Paul__B:
Hmmm. How do you suppose that might happen?
Wasn't sure but didn't really want to risk a board trying to find out.
sterretje:
What you have written is still a delay; as in "it isn't able to do anything else while in the while-loop.
Thought so but wanted to give it a crack.
Paul__B:
In fact, it is pretty much the exact implementation of "delay()" itself.
(Except for the muck-up with "prevtime". )
(And the unnecessary "currenttime".)
Yeah the prevtime is because the original code was slightly different so wanted to try and extend the variables to be a bit clearer for a post and missed one.
Thanks for the quick replies everyone, still wondering if there is a function that can be written in a similar style to get the same behaviour as a delay function? It's being mostly used for a series of inverse kinematic calulcations that are sent to the motors and need a slight delay to allow the motors to catch up with the code, Here's an example:
The Movement function created basically tells a couple different motors which angle to move to. so the specific variables aren't too important, just the process that the walkone function goes through.
I understand one method is to "schedule" what move when but not sure how to incorporate this here.
You will need to call this repeatedly as shown below
void loop()
{
// remember the last time that the movement was done
static unsigned long lastUpdateTime;
// current time
unsigned long currentTime = millis();
// checkif 20 milliseconds have passed
if (currentTime - lastUpdateTime >= 20))
{
// update the last update time
lastUpdateTime = currentTime;
//call your function
bool rc = walkone();
// if the function did one complete cycle
if (rc == true)
{
Serial.println("walkone completed");
}
}
}
// prevtime is not relevant
void olddelay (unsigned long delaylength) {
unsigned long currenttime = millis();
while ( (millis() - currenttime) < delaylength);
}
The point is that there is a certain essential method to writing code to do a number of things.
The loop() function executes a series of checks or tests, none of which ever waits for anything. Rather, amongst other things, it tests for certain "scheduled" times (in millis() unsigned long values) which when they have elapsed ( (millis() - lasttime) >= thisdelay ) triggers the next desired immediate action and sets up those to follow.