Alternate delay when using blynk

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();
}

RyanC6:
Also, is there a risk of damaging anything by testing code like this with a board?

Hmmm. How do you suppose that might happen? :astonished:

What you have written is still a delay; as in "it isn't able to do anything else while in the while-loop.

sterretje:
What you have written is still a delay; as in "it isn't able to do anything else while in the while-loop.

In fact, it is pretty much the exact implementation of "delay()" itself. :grinning:

(Except for the muck-up with "prevtime". :astonished: )

(And the unnecessary "currenttime".)

Paul__B:
Hmmm. How do you suppose that might happen? :astonished:

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. :grinning:

(Except for the muck-up with "prevtime". :astonished: )

(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:

void walkone() {

  for (int i = 0; i < 100; i = i + s) {
    if (i < 51) {
      Movement(x, y + ((i + 1) / s) * (currenty - y) / (100 / s), (lcurrentz1 + ((i + 1) / s) * ((lz1 - lcurrentz1) / (100 / s))) + elegdisp, 1);
      Movement(x, y + ((i + 1) / s) * (currenty - y) / (100 / s), (rcurrentz1 + ((i + 1) / s) * ((rz1 - rcurrentz1) / (100 / s))) - elegdisp, 6);
    }
    else {
      Movement(x, currenty + ((i + 1) / s) * ((y - currenty) / (100 / s)), (lcurrentz1 + ((i + 1) / s) * ((lz1 - lcurrentz1) / (100 / s))) + elegdisp, 1);
      Movement(x, currenty + ((i + 1) / s) * ((y - currenty) / (100 / s)), (rcurrentz1 + ((i + 1) / s) * ((rz1 - rcurrentz1) / (100 / s))) - elegdisp, 6);
    }

    Movement(x, y, (lcurrentz2 + ((i + 1) / s) * ((lz2 - lcurrentz2) / (100 / s))), 2);
    Movement(x, y, (rcurrentz2 + ((i + 1) / s) * ((rz2 - rcurrentz2) / (100 / s))) + elegdisp, 4);
    Movement(x, y, (lcurrentz3 + ((i + 1) / s) * ((lz3 - lcurrentz3) / (100 / s))) - elegdisp, 3);
    Movement(x, y, (rcurrentz3 + ((i + 1) / s) * ((rz3 - rcurrentz3) / (100 / s))), 5);
    delay(20);
  }
}

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.

Thanks

Below is a possible solution with a modified walkone

/*
  walk one
  Returns:
    false while cycle not complete, else true
*/
bool walkone()
{

  // counter; replaces i in your code
  static int counter;

  if (counter < 51) {
    Movement(x, y + ((counter + 1) / s) * (currenty - y) / (100 / s), (lcurrentz1 + ((counter + 1) / s) * ((lz1 - lcurrentz1) / (100 / s))) + elegdisp, 1);
    Movement(x, y + ((counter + 1) / s) * (currenty - y) / (100 / s), (rcurrentz1 + ((counter + 1) / s) * ((rz1 - rcurrentz1) / (100 / s))) - elegdisp, 6);
  }
  else {
    Movement(x, currenty + ((counter + 1) / s) * ((y - currenty) / (100 / s)), (lcurrentz1 + ((counter + 1) / s) * ((lz1 - lcurrentz1) / (100 / s))) + elegdisp, 1);
    Movement(x, currenty + ((counter + 1) / s) * ((y - currenty) / (100 / s)), (rcurrentz1 + ((counter + 1) / s) * ((rz1 - rcurrentz1) / (100 / s))) - elegdisp, 6);
  }

  Movement(x, y, (lcurrentz2 + ((counter + 1) / s) * ((lz2 - lcurrentz2) / (100 / s))), 2);
  Movement(x, y, (rcurrentz2 + ((counter + 1) / s) * ((rz2 - rcurrentz2) / (100 / s))) + elegdisp, 4);
  Movement(x, y, (lcurrentz3 + ((counter + 1) / s) * ((lz3 - lcurrentz3) / (100 / s))) - elegdisp, 3);
  Movement(x, y, (rcurrentz3 + ((counter + 1) / s) * ((rz3 - rcurrentz3) / (100 / s))), 5);

  counter += s;

  if (counter >= 100)
  {
    counter = 0;
    return true;
  }

  return false;
}

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");
    }
  }
}

Not compiled, not tested.

Just for reference,

// 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.

Both of those posts help a heap, thanks.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.