{"id":43,"date":"2004-10-07T03:07:12","date_gmt":"2004-10-07T08:07:12","guid":{"rendered":"http:\/\/www.tigoe.com\/pcomp\/code2\/category\/arduinowiring\/43"},"modified":"2007-08-07T09:37:14","modified_gmt":"2007-08-07T14:37:14","slug":"edge-detection","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/arduinowiring\/43\/","title":{"rendered":"Edge Detection"},"content":{"rendered":"<p>Most of the time, you don&#8217;t need to know whether a switch is on or off so much as you need to know when it changed.  You want to know when it turned on or when it turned off.  In other words, you want to find the edge of the transition from on to off, or vice versa.  This is often called edge detection. <\/p>\n<p>The basic idea is that you check not only what the state of the switch is, but what the state was the last time you checked, and compare the two.  Here&#8217;s an example in PicBasic Pro:<\/p>\n<p><!--more--><\/p>\n<pre>\nswitchStateVar var byte\nlastSwitchStateVar var byte\nswitchCountVar var byte\ninput portd.1        &#8216; the switch is on RD1\nswitchStateVar = 0\n\nmain:\n    switchStateVar = portd.1\n    ' if the switch isn't the same as it was last time through\n    ' the main loop, then you want to do something:\n\n    if switchStateVar <> lastswitchStateVar then    \n        if switchStateVar = 1 then\n            ' the switch went from off to on\n            switchCountVar = switchCountVar + 1\n            serout2 portc.6, 16468, [\"switch is pressed.\", 10, 13]\n        else\n            ' the switch went from on to off\n            serout2 portc.6, 16468, [\"switch is not pressed\", 10, 13]\n                            serout2 portc.6, 16468, [&#8220;switch hits: \", DEC switchCountVar, 10, 13]\n        endif\n            \n                  ' store the state of the switch for next check:\n        lastswitchStateVar = switchStateVar\n    endif\ngoto main\n<\/pre>\n<p>Here it is in Arduino\/Wiring<\/p>\n<pre>\nint switchPin = 2;\nint switchCounterVar = 0;\nint switchStateVar = 0;\nint lastSwitchStateVar = 0;\n\nvoid setup() {\n  pinMode(switchPin, INPUT);\n}\n\n\nvoid loop() {\n  \/\/ read the switch\n  switchStateVar = digitalRead(switchPin);\n  \/\/ compare the switch to its previous state\n  if (switchStateVar != lastSwitchStateVar) {\n    \/\/ if the state has changed, increment the counter\n    if (switchStateVar == HIGH) {\n      switchCounterVar++;\n    }\n    \/\/ save the current state as the last state, \n    \/\/for next time through the loop\n    lastSwitchStateVar = switchStateVar;\n  }\n}\n\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Most of the time, you don&#8217;t need to know whether a switch is on or off so much as you need to know when it changed&#8230;.  The basic idea is that you check not only what the state of the switch is, but what the state was the last time you checked, and compare the two.<\/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-43","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\/43","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=43"}],"version-history":[{"count":0,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/43\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}