{"id":63,"date":"2004-10-07T02:58:41","date_gmt":"2004-10-07T07:58:41","guid":{"rendered":"http:\/\/www.tigoe.com\/pcomp\/code2\/category\/arduinowiring\/63"},"modified":"2007-08-07T22:50:50","modified_gmt":"2007-08-08T03:50:50","slug":"debouncing-a-digital-input","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/arduinowiring\/63\/","title":{"rendered":"Debouncing a digital input"},"content":{"rendered":"<p>Any switch is made of two conductive mechanical contacts that are either touching each other (closed circuit) or not (open circuit).  When they make contact, the contact is not always perfect. For the fraction of s second before the contact is complete, the circuit may make contact and be broken several times.  When you read this input on a microcontroller, you&#8217;ll see a rapid switching from 0 to 1 over a few milliseconds until the contact is final.<\/p>\n<p><!--more--><\/p>\n<p>To avoid this, you can debounce the switch. This just means that if contact is made, you wait a few milliseconds, then check it again before taking action.  Here&#8217;s an example:<\/p>\n<pre>\nswitchClosedVar var byte\nInput portd.1\n\nmain:\n    if portd.1 = 1 then\n        pause 10    &#8216; change the time if your switch still bounces\n        if portd.1 = 1 then\n            switchClosedVar = 1\n        else\n            switchClosedVar = 0\n        endif\n    endif\ngoto main\n<\/pre>\n<p>Here it is in Arduino\/Wiring syntax:<\/p>\n<pre>\nint switchPin = 2;\nint switchState = 0;\nint switchClosedVar = 0;\n\nvoid setup() {\n   pinMode(switchPin, INPUT);\n}\n\nvoid loop() {\n   switchState = digitalRead(switchPin);\n   if (switchState == 1) {\n      delay(10);  \/\/ change the time if your switch still bounces\n      switchState = digitalRead(switchPin);\n      if (switchState == 1) {\n         switchClosedVar = 1;\n      } else {\n         switchClosedVar = 0;\n      }\n   }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>For the fraction of s second before the contact is complete, the circuit may make contact and be broken several times.  When you read this input on a microcontroller, you&#8217;ll see a rapid switching from 0 to 1 over a few milliseconds until the contact is final.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,10],"tags":[],"class_list":["post-63","post","type-post","status-publish","format-standard","hentry","category-arduinowiring","category-picbasic-pro"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/63","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=63"}],"version-history":[{"count":0,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/63\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=63"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=63"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=63"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}