Task repeat every 10 seconds

Hi, I am using p5 js and I am trying to have some code execute every 10 seconds. I am having trouble finding the solution. Thanks.

1 Like

Hello,

Explore using:
https://p5js.org/reference/#/p5/millis

:)

2 Likes
1 Like

I just realized that what I meant was to have a 10 second wait then have code executing for ten seconds, then not execute for 10 seconds and repeat.

You can use the library called “Timing Utilities”.

Hello,

Ideally you should make a flowchart.

To get you started:

  • What time is it at start? Do this in setup()
    timeLast = millis()

  • What time is it now? You are now in draw()
    timeNow = millis()

  • What is elapsed time?
    timeElapsed = timeNow - timeLast

  • Is elapsedTime 10s or more?
    Do something!
    and reset timer to start over when this condition is met
    timeLast = millis()

  • Rinse and repeat

:)

2 Likes

is elapsedTime >= 10s (= 10000 ms)

1 Like