Shop OBEX P1 Docs P2 Docs Learn Events
doing something every X amount of time — Parallax Forums

doing something every X amount of time

ObnepecObnepec Posts: 20
edited 2011-01-31 09:51 in BASIC Stamp
All,

I would like to do something every X amount of time (say 10 seconds although it doesn't have to be exactly 10 seconds) however I have a PWM output that I need to keep up so I don't think I can do a sleep or wait for X amount of time.

So basically lets say I want to run subroutine DoStuff once every 10 seconds but I also need to keep updating the PWM subroutine. How would you do it?

If that is not very clear just let me know and I will try to rephrase.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-01-31 09:51
    It depends on how you're generating the PWM. If you're using a PWM statement, that includes a time duration and you can use that as your "clock tick". Let's say that your PWM subroutine does a little bit of calculation and then executes a PWM statement for 100ms. The little bit of calculation takes maybe 5-10ms so the whole subroutine lasts 105-110ms. Your main program calls this PWM subroutine over and over, each time taking 105 to 110ms. If you keep a count of the number of times it's called, after 100 times, 10.5 to 11 seconds will have gone by. If your DoStuff routine doesn't take much time and the PWM generation can tolerate a little extra delay, you can just call that after a count of 100 (and reset the counter). Obviously, you can adjust the length of the PWM statement or the maximum count of calls to the PWM subroutine to get closer timing, but you get the basic idea.
Sign In or Register to comment.