-
Notifications
You must be signed in to change notification settings - Fork 13.3k
delayMicroseconds() - crash #2240
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
Comments
reason is that your whole routine is fully blocking for the cpu and it takes more than the watch dog permits (thus triggering soft wdt reset). The reason why it does not crash when you add delay() is that delay() actually switches the context back to the system while you are delaying and feeding the watchdog this way. |
Yes but why is it working with delay(1) and not with delayMicroseconds(1000) ??? There must be something up right? Will change it lateron anyway to work without delay but just discovered the problem while testing. |
and I just explained it to you. delay(1) will let the system run for 1ms. delayMicroseconds(1000) will halt for 1ms and not give the system a chance to do it's thing. |
Ok, I assumed delayMicroseconds(1000) would do the same. So it doesn't? |
no it does not. The ESP as opposed to AVR Arduinos (and others) has WiFi and full network stack running in the background that needs to process the network often. When you delayMicroseconds you actually prevent that from happening and the longer you the, more the chance to soft wdt becomes. delay() on the other hand, returns power to the system and checks on return if the time for returning to your code has come and if not, gives power to the system again. void delayMicroseconds(uint32_t us){
uint32_t start = micros();
while(micros() - start < us){}
}
vod delay(uint32_t ms){
uint32_t start = millis();
while(millis() - start < ms){
yield();//give the system a chance to execute
}
} |
Thanks! Didn't know that. |
Hmmmm... Changed the PWM Routine now so it has no delay and removed the delay above but it crashes at the same point as it did before I added delay(1). It always crashes at "PWM_Wert 194" once don't add delay(1) in the counting loop.
|
but it MUST have delay! else your routine takes too long and you crash the system |
or at least |
@Gorkde I believe what @me-no-dev stated was that delay(1) MUST be used as your routine "takes too long to run" but that you can't substitute delayMicroseconds(1000) for delay(1) i.e. delay(1) is a very specific function. |
Ok, thanks, will include yield then! |
One last question: How often do I need to add delay(1) or yield() ? And... is delay(1) enough or does it need to be a larger time? |
even delay(0) will work. Should be as often as you can spare, but not more than 100ms let's say |
Ok, thanks, got 38ms at the moment. |
ESP12F
I'm working on a PWM Routine (not finished still in progress) which did crash for some reason (code#1).
Then I assumed it's too fast so I added delay(1) in the loop and no crash occured anymore (code#2).
Then I thought to make the delay smaller so I changed delay(1) to delayMicroseconds(100) and it did crash again.
So I changed delayMicroseconds(100) to delayMicroseconds(1000) which should be the same as delay(1) which didnt crash. But it does crash again with that! (code#3).
Code 1:
Code 2:
Code 3:
The text was updated successfully, but these errors were encountered: