birddseedd
Posts: 43
Joined: Fri Jan 04, 2019 3:28 pm

What function would I use to do something every half second?

Mon Jan 21, 2019 6:52 pm

Like a loop that would run every half second.

thanks


User avatar
DougieLawson
Posts: 36322
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: What function would I use to do something every half second?

Mon Jan 21, 2019 7:36 pm

birddseedd wrote:
Mon Jan 21, 2019 6:52 pm
Like a loop that would run every half second.
What programming language are you using?
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

birddseedd
Posts: 43
Joined: Fri Jan 04, 2019 3:28 pm

Re: What function would I use to do something every half second?

Mon Jan 21, 2019 7:55 pm

DougieLawson wrote:
Mon Jan 21, 2019 7:36 pm
birddseedd wrote:
Mon Jan 21, 2019 6:52 pm
Like a loop that would run every half second.
What programming language are you using?
well..... im not sure. my first instinct is to use python because that's where ill have the most support. but C does have the advantage of speed.

birddseedd
Posts: 43
Joined: Fri Jan 04, 2019 3:28 pm

Re: What function would I use to do something every half second?

Mon Jan 21, 2019 8:10 pm

gordon77 wrote:
Mon Jan 21, 2019 7:07 pm
Try time.sleep(0.5) or time.time() functions.

https://stackoverflow.com/questions/474 ... -in-python
what I worry about that is that i will be storing motor position values in a file. then "playing' them back. I want to do it at the same rate. I worry about instruction execution times adding up and my times getting off.

say after a few hundred seconds my motor goes to xyz position. but on play back, it takes time proccessing and I end up being more than a few hundred seconds before its able to get to that part of the "recording file" and I end up going to a different position at a different time.

PhatFil
Posts: 1423
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: What function would I use to do something every half second?

Mon Jan 21, 2019 8:54 pm

search for "How to get current time in milliseconds in Python?" and you should be able to determine more precise differences in the time of events you want to replicate.

User avatar
DougieLawson
Posts: 36322
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: What function would I use to do something every half second?

Mon Jan 21, 2019 8:56 pm

I've done lots of useful (and accurate) timing in C++ with

Code: Select all

void wait3sec()
{
        using std::chrono::system_clock;
        std::time_t tt = system_clock::to_time_t (system_clock::now());
        struct std::tm *ptm = std::localtime(&tt);
        std::this_thread::sleep_for(std::chrono::seconds(3));
}
Don't know what the limits are for std::this_thread::sleep_for(...).
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Return to “Beginners”