mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-04 19:17:47 +00:00
example: naive debouncing for pininterrupt example (#2233)
This commit is contained in:
@@ -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 (
|
||||
@@ -15,6 +15,8 @@ const (
|
||||
led = machine.LED
|
||||
)
|
||||
|
||||
var lastPress time.Time
|
||||
|
||||
func main() {
|
||||
|
||||
// Configure the LED, defaulting to on (usually setting the pin to low will
|
||||
@@ -29,6 +31,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()
|
||||
|
||||
led.Set(!led.Get())
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user