{"action":"created","issue":{"url":"https://api.github.com/repos/arduino/Arduino/issues/4320","labels_url":"https://api.github.com/repos/arduino/Arduino/issues/4320/labels{/name}","comments_url":"https://api.github.com/repos/arduino/Arduino/issues/4320/comments","events_url":"https://api.github.com/repos/arduino/Arduino/issues/4320/events","html_url":"https://github.com/arduino/Arduino/issues/4320","id":123086746,"number":4320,"title":"Delay yield","user":{"login":"vbextreme","id":9294014,"avatar_url":"https://avatars.githubusercontent.com/u/9294014?v=3","gravatar_id":"","url":"https://api.github.com/users/vbextreme","html_url":"https://github.com/vbextreme","followers_url":"https://api.github.com/users/vbextreme/followers","following_url":"https://api.github.com/users/vbextreme/following{/other_user}","gists_url":"https://api.github.com/users/vbextreme/gists{/gist_id}","starred_url":"https://api.github.com/users/vbextreme/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vbextreme/subscriptions","organizations_url":"https://api.github.com/users/vbextreme/orgs","repos_url":"https://api.github.com/users/vbextreme/repos","events_url":"https://api.github.com/users/vbextreme/events{/privacy}","received_events_url":"https://api.github.com/users/vbextreme/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/arduino/Arduino/labels/Architecture:%20AVR","name":"Architecture: AVR","color":"fbca04"},{"url":"https://api.github.com/repos/arduino/Arduino/labels/Component:%20Core","name":"Component: Core","color":"009800"},{"url":"https://api.github.com/repos/arduino/Arduino/labels/Type:%20Bug","name":"Type: Bug","color":"0052cc"},{"url":"https://api.github.com/repos/arduino/Arduino/labels/Type:%20Improvement","name":"Type: Improvement","color":"0052cc"}],"state":"open","locked":false,"assignee":{"login":"cmaglie","id":423515,"avatar_url":"https://avatars.githubusercontent.com/u/423515?v=3","gravatar_id":"","url":"https://api.github.com/users/cmaglie","html_url":"https://github.com/cmaglie","followers_url":"https://api.github.com/users/cmaglie/followers","following_url":"https://api.github.com/users/cmaglie/following{/other_user}","gists_url":"https://api.github.com/users/cmaglie/gists{/gist_id}","starred_url":"https://api.github.com/users/cmaglie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cmaglie/subscriptions","organizations_url":"https://api.github.com/users/cmaglie/orgs","repos_url":"https://api.github.com/users/cmaglie/repos","events_url":"https://api.github.com/users/cmaglie/events{/privacy}","received_events_url":"https://api.github.com/users/cmaglie/received_events","type":"User","site_admin":false},"milestone":null,"comments":43,"created_at":"2015-12-19T14:02:32Z","updated_at":"2015-12-28T10:56:03Z","closed_at":null,"body":"While writing a port official scheduler I realized that the delay() function has a bug.\r\nthis is official delay() function\r\n```\r\nvoid delay(unsigned long ms)\r\n{\r\n\tuint16_t start = (uint16_t)micros();\r\n\r\n\twhile (ms > 0) {\r\n\t\tyield();\r\n\t\tif (((uint16_t)micros() - start) >= 1000) {\r\n\t\t\tms--;\r\n\t\t\tstart += 1000;\r\n\t\t}\r\n\t}\r\n}\r\n```\r\nThis code assumes that the task called from delay lasts less than a millisecond , otherwise you can get up to an infinite loop.\r\nsimple solution:\r\n```\r\nvoid myDelay(unsigned long ms)\r\n{\r\n      if ( 0 == ms ) return;\r\n      \r\n      uint32_t sm = micros();\r\n      uint32_t sM = millis();\r\n\r\n      while( millis() - sM < ms - 1 ) yield();\r\n      ms *= 1000;\r\n      while( micros() - sm < ms - 1 );\r\n}\r\n```\r\nIt works with the same precision but should be free of bugs.\r\n\r\nor \r\n\r\n```\r\nvoid myDelay(unsigned long ms)\r\n{\r\n    uint32_t start = micros();\r\n\r\n    while (ms > 0) {\r\n        yield();\r\n        while ( ms > 0 && (micros() - start) >= 1000) {\r\n            ms--;\r\n            start += 1000;\r\n        }\r\n    }\r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/arduino/Arduino/issues/comments/167542106","html_url":"https://github.com/arduino/Arduino/issues/4320#issuecomment-167542106","issue_url":"https://api.github.com/repos/arduino/Arduino/issues/4320","id":167542106,"user":{"login":"PaulStoffregen","id":965463,"avatar_url":"https://avatars.githubusercontent.com/u/965463?v=3","gravatar_id":"","url":"https://api.github.com/users/PaulStoffregen","html_url":"https://github.com/PaulStoffregen","followers_url":"https://api.github.com/users/PaulStoffregen/followers","following_url":"https://api.github.com/users/PaulStoffregen/following{/other_user}","gists_url":"https://api.github.com/users/PaulStoffregen/gists{/gist_id}","starred_url":"https://api.github.com/users/PaulStoffregen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PaulStoffregen/subscriptions","organizations_url":"https://api.github.com/users/PaulStoffregen/orgs","repos_url":"https://api.github.com/users/PaulStoffregen/repos","events_url":"https://api.github.com/users/PaulStoffregen/events{/privacy}","received_events_url":"https://api.github.com/users/PaulStoffregen/received_events","type":"User","site_admin":false},"created_at":"2015-12-28T10:56:03Z","updated_at":"2015-12-28T10:56:03Z","body":"My inbox Arduino folder currently has 361 messages from \"VogonJ\" and \"VogonJeltz\", even though I've unsubscribed from most issues where he's posted.  This is a consistent pattern of off-topic, time-wasting messages."}}