{"id":228,"date":"2020-07-18T17:51:50","date_gmt":"2020-07-19T00:51:50","guid":{"rendered":"https:\/\/boom-tracker.com\/tracker\/?p=228"},"modified":"2020-07-18T17:58:42","modified_gmt":"2020-07-19T00:58:42","slug":"another-timing-bug-squashed","status":"publish","type":"post","link":"https:\/\/boom-tracker.com\/tracker\/blog\/another-timing-bug-squashed\/","title":{"rendered":"Another timing bug squashed"},"content":{"rendered":"\n<p>I spent several hours trying to figure out why my circuit on the second M0 microcontroller was having significant timing issues when the first M0 wasn&#8217;t.  The second M0 didn&#8217;t seem to exhibit the problem in my NTP test rig either.  I finally figured it out.  As you can see below, when I built the second M0, I didn&#8217;t have a second DHT-11 temperature and humidity sensor (the blue box&#8211;I forgot to order a second one), but I kept the code the same.  If I use the preprocessor flag I put in my code to disable all the DHT related calls, the timing issues go away.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/boom-tracker.com\/tracker\/wp-content\/uploads\/2020\/07\/8C468407-2333-4B71-98CD-052050C6431E.jpeg\" alt=\"Two microphones listening for booms\" class=\"wp-image-233\" width=\"633\" height=\"475\" srcset=\"https:\/\/boom-tracker.com\/tracker\/wp-content\/uploads\/2020\/07\/8C468407-2333-4B71-98CD-052050C6431E.jpeg 640w, https:\/\/boom-tracker.com\/tracker\/wp-content\/uploads\/2020\/07\/8C468407-2333-4B71-98CD-052050C6431E-300x225.jpeg 300w, https:\/\/boom-tracker.com\/tracker\/wp-content\/uploads\/2020\/07\/8C468407-2333-4B71-98CD-052050C6431E-250x188.jpeg 250w, https:\/\/boom-tracker.com\/tracker\/wp-content\/uploads\/2020\/07\/8C468407-2333-4B71-98CD-052050C6431E-550x413.jpeg 550w, https:\/\/boom-tracker.com\/tracker\/wp-content\/uploads\/2020\/07\/8C468407-2333-4B71-98CD-052050C6431E-240x180.jpeg 240w, https:\/\/boom-tracker.com\/tracker\/wp-content\/uploads\/2020\/07\/8C468407-2333-4B71-98CD-052050C6431E-400x300.jpeg 400w\" sizes=\"auto, (max-width: 633px) 100vw, 633px\" \/><\/figure>\n\n\n\n<p>Here&#8217;s my theory as to what&#8217;s happening.  The <a rel=\"noreferrer noopener\" href=\"https:\/\/www.adafruit.com\/product\/386\" target=\"_blank\">DHT11 product page<\/a> on Adafruit&#8217;s site says that reading the chip requires careful timing.  To get the timing right, I suspect that it disables interrupts while reading the chip.  If the chip is there then this only takes a few microseconds, and nobody is the wiser.  However, if the chip isn&#8217;t there, it has to wait for some timeout to happen, and that means interrupts are disabled for a while, and that throws off the <em>millis()<\/em> and <em>micros()<\/em> timing functions, which won&#8217;t increment while interrupts are disabled.  Since my NTP library uses <em>micros()<\/em> calls frequently to calculate the time since the last server sync, it was accumulating significant errors which were causing the readings to go all over the place.<\/p>\n\n\n\n<p>By the time I&#8217;d figured this out, I&#8217;d already ripped out a lot of the hairy math I had implemented for calculating gradual skews and correction factors, and replaced it with slightly less hairy math.  It&#8217;s far more straightforward and easy to understand, and the only downside is that a lot of it is now floating point math, which is slower on some microprocessors.<\/p>\n\n\n\n<p>I&#8217;m still seeing about a 1-2 millisecond skew between the readings on the two microphones, and I&#8217;m going to have to find a way to adjust for that. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I spent several hours trying to figure out why my circuit on the second M0 microcontroller was having significant timing issues when the first M0 wasn&#8217;t. The second M0 didn&#8217;t seem to exhibit the problem in my NTP test rig either. I finally figured it out. As you can see below, when I built the&hellip; <a class=\"more-link\" href=\"https:\/\/boom-tracker.com\/tracker\/blog\/another-timing-bug-squashed\/\">Continue reading <span class=\"screen-reader-text\">Another timing bug squashed<\/span> <span class=\"meta-nav\" aria-hidden=\"true\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":233,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"bgseo_title":"","bgseo_description":"","bgseo_robots_index":"index","bgseo_robots_follow":"follow","footnotes":""},"categories":[3,9,8],"tags":[],"class_list":["post-228","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-experimentation","category-implementation"],"_links":{"self":[{"href":"https:\/\/boom-tracker.com\/tracker\/wp-json\/wp\/v2\/posts\/228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/boom-tracker.com\/tracker\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/boom-tracker.com\/tracker\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/boom-tracker.com\/tracker\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/boom-tracker.com\/tracker\/wp-json\/wp\/v2\/comments?post=228"}],"version-history":[{"count":3,"href":"https:\/\/boom-tracker.com\/tracker\/wp-json\/wp\/v2\/posts\/228\/revisions"}],"predecessor-version":[{"id":237,"href":"https:\/\/boom-tracker.com\/tracker\/wp-json\/wp\/v2\/posts\/228\/revisions\/237"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/boom-tracker.com\/tracker\/wp-json\/wp\/v2\/media\/233"}],"wp:attachment":[{"href":"https:\/\/boom-tracker.com\/tracker\/wp-json\/wp\/v2\/media?parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/boom-tracker.com\/tracker\/wp-json\/wp\/v2\/categories?post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/boom-tracker.com\/tracker\/wp-json\/wp\/v2\/tags?post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}