tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/buzzer/main.go (about)

     1  package main
     2  
     3  import (
     4  	"machine"
     5  	"time"
     6  
     7  	"tinygo.org/x/drivers/buzzer"
     8  )
     9  
    10  type note struct {
    11  	tone     float64
    12  	duration float64
    13  }
    14  
    15  func main() {
    16  	speaker := machine.PA30
    17  	speaker.Configure(machine.PinConfig{Mode: machine.PinOutput})
    18  	speaker.Set(true)
    19  
    20  	bzrPin := machine.A0
    21  	bzrPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
    22  
    23  	bzr := buzzer.New(bzrPin)
    24  
    25  	song := []note{
    26  		{buzzer.C3, buzzer.Quarter},
    27  		{buzzer.D3, buzzer.Quarter},
    28  		{buzzer.E3, buzzer.Quarter},
    29  		{buzzer.F3, buzzer.Quarter},
    30  		{buzzer.G3, buzzer.Quarter},
    31  		{buzzer.A3, buzzer.Quarter},
    32  		{buzzer.B3, buzzer.Quarter},
    33  		{buzzer.C3, buzzer.Quarter},
    34  	}
    35  
    36  	for _, val := range song {
    37  		bzr.Tone(val.tone, val.duration)
    38  		time.Sleep(10 * time.Millisecond)
    39  	}
    40  }