Skip to content

Commit

Permalink
example: naive debouncing for pininterrupt example
Browse files Browse the repository at this point in the history
  • Loading branch information
ysoldak committed Nov 12, 2021
1 parent 335fb71 commit 2ba8542
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/examples/pininterrupt/pininterrupt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
// This example demonstrates how to use pin change interrupts.
//
// This is only an example and should not be copied directly in any serious
// circuit, because it lacks an important feature: debouncing.
// circuit, because it only naively implements an important feature: debouncing.
// See: https://en.wikipedia.org/wiki/Switch#Contact_bounce

import (
Expand All @@ -17,6 +17,8 @@ const (
led = machine.LED
)

var lastPress time.Time

func main() {
var lightLed volatile.Register8
lightLed.Set(0)
Expand All @@ -33,6 +35,13 @@ func main() {

// Set an interrupt on this pin.
err := button.SetInterrupt(buttonPinChange, func(machine.Pin) {

// Ignore events that are too close to the last registered press (debouncing)
if time.Since(lastPress) < 100*time.Millisecond {
return
}
lastPress = time.Now()

if lightLed.Get() != 0 {
lightLed.Set(0)
led.Low()
Expand Down

0 comments on commit 2ba8542

Please sign in to comment.