summaryrefslogtreecommitdiffstatshomepage
path: root/docs/pyboard/tutorial/debounce.rst
diff options
context:
space:
mode:
authorDaniel Campora <daniel@wipy.io>2015-05-27 13:59:59 +0200
committerDamien George <damien.p.george@gmail.com>2015-06-04 23:44:35 +0100
commit7ca1bd314bd5e3146f8c868f91af54d17dd04d45 (patch)
treec30c7ccb9a8437a9e69f2716f196b87f27beca5d /docs/pyboard/tutorial/debounce.rst
parent031278f661e5d285c56359e355a96161bf6e1a9f (diff)
downloadmicropython-7ca1bd314bd5e3146f8c868f91af54d17dd04d45.tar.gz
micropython-7ca1bd314bd5e3146f8c868f91af54d17dd04d45.zip
docs: Generate a separate docs build for each port.
Using Damien's approach where conf.py and topindex.html are shared by all ports.
Diffstat (limited to 'docs/pyboard/tutorial/debounce.rst')
-rw-r--r--docs/pyboard/tutorial/debounce.rst37
1 files changed, 37 insertions, 0 deletions
diff --git a/docs/pyboard/tutorial/debounce.rst b/docs/pyboard/tutorial/debounce.rst
new file mode 100644
index 0000000000..f730e1d340
--- /dev/null
+++ b/docs/pyboard/tutorial/debounce.rst
@@ -0,0 +1,37 @@
+Debouncing a pin input
+======================
+
+A pin used as input from a switch or other mechanical device can have a lot
+of noise on it, rapidly changing from low to high when the switch is first
+pressed or released. This noise can be eliminated using a capacitor (a
+debouncing circuit). It can also be eliminated using a simple function that
+makes sure the value on the pin is stable.
+
+The following function does just this. It gets the current value of the given
+pin, and then waits for the value to change. The new pin value must be stable
+for a continuous 20ms for it to register the change. You can adjust this time
+(to say 50ms) if you still have noise. ::
+
+ import pyb
+
+ def wait_pin_change(pin):
+ # wait for pin to change value
+ # it needs to be stable for a continuous 20ms
+ cur_value = pin.value()
+ active = 0
+ while active < 20:
+ if pin.value() != cur_value:
+ active += 1
+ else:
+ active = 0
+ pyb.delay(1)
+
+
+Use it something like this::
+
+ import pyb
+
+ pin_x1 = pyb.Pin('X1', pyb.Pin.IN, pyb.Pin.PULL_DOWN)
+ while True:
+ wait_pin_change(pin_x1)
+ pyb.LED(4).toggle()