github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/examples/pininterrupt/pininterrupt.go (about) 1 package main 2 3 // This example demonstrates how to use pin change interrupts. 4 // 5 // This is only an example and should not be copied directly in any serious 6 // circuit, because it lacks an important feature: debouncing. 7 // See: https://en.wikipedia.org/wiki/Switch#Contact_bounce 8 9 import ( 10 "machine" 11 "time" 12 ) 13 14 const ( 15 led = machine.LED 16 ) 17 18 func main() { 19 20 // Configure the LED, defaulting to on (usually setting the pin to low will 21 // turn the LED on). 22 led.Configure(machine.PinConfig{Mode: machine.PinOutput}) 23 led.Low() 24 25 // Make sure the pin is configured as a pullup to avoid floating inputs. 26 // Pullup works for most buttons, as most buttons short to ground when 27 // pressed. 28 button.Configure(machine.PinConfig{Mode: buttonMode}) 29 30 // Set an interrupt on this pin. 31 err := button.SetInterrupt(buttonPinChange, func(machine.Pin) { 32 led.Set(!led.Get()) 33 }) 34 if err != nil { 35 println("could not configure pin interrupt:", err.Error()) 36 } 37 38 // Make sure the program won't exit. 39 for { 40 time.Sleep(time.Hour) 41 } 42 }