Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1s pause in the loop #1

Open
Godzil opened this issue Jun 29, 2018 · 3 comments
Open

1s pause in the loop #1

Godzil opened this issue Jun 29, 2018 · 3 comments

Comments

@Godzil
Copy link

Godzil commented Jun 29, 2018

delay(1000);

If I'm correct, delay() take time in millisecond; so here we are waiting for 1 second, is this really wanted or just an error? (I can understand a bit of delay to not spam the USB with useless data) but 1s seems a lot

@UnsafePointer
Copy link

UnsafePointer commented Mar 19, 2019

The polling time of gaming peripherals is usually in the neighbourhood of 125 Hz, or every 8 milliseconds. 1000 milliseconds is way too long if that's the case.

Someone is reporting improvements in the responsiveness by decreasing this delay: https://forum.clockworkpi.com/t/interface-speed/3015/8, this should be addressed.

@centuryglass
Copy link

It's definitely not waiting one second between loops, all you need to do to prove that wrong is to notice that it can definitely register more than one button press per second.

TIMSK0 &= !(1 << TOIE0);

TIMSK0 controls timer zero, the timer used to implement the delay function. I haven't looked into exactly how it works, but somehow the modification to TIMSK0 on line 93 speeds up the timer, making the delay function finish much more quickly.

That said, it is still entirely possible that decreasing the delay could still help, but modifying timer zero is an unusual move and I wouldn't want to make any recommendations without knowing why they're doing that.

@DanB91
Copy link

DanB91 commented May 25, 2019

@centuryglass That is an odd piece of code. That essentially boils down to TIMSK0 = 0 since they use ! and not ~. I would assume they meant to use ~.

Googling, it looks like setting TIMSK0 to 0 essentially disables delay(). I am trying to actually confirm this by outputting the value of micros() (which is what is called in delay()), but having difficulty setting up an Arduino project to compile everything. Will write back when I can.

So it's looking like this delay(1000) is essentially a no-op and TIMSK0 &= !(1 << TOIE0); seems to be a bug. The mystery of delay(1000) continues.

Update: Confirmed this to be the case. Assigning TIMSK0 to 0 causes the time to overflow really quickly. Thus, if you have :

auto start = micros();
... //code
Serial.println(micros()-start); //underflow

then, we will print a very large number. This leads to delay() to return almost immediately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants