gobot.io/x/gobot@v1.16.0/examples/firmata_buzzer.go (about)

     1  // +build example
     2  //
     3  // Do not build by default.
     4  
     5  /*
     6   How to run
     7   Pass serial port to use as the first param:
     8  
     9  	go run examples/firmata_buzzer.go /dev/ttyACM0
    10  */
    11  
    12  package main
    13  
    14  import (
    15  	"os"
    16  	"time"
    17  
    18  	"gobot.io/x/gobot"
    19  	"gobot.io/x/gobot/drivers/gpio"
    20  	"gobot.io/x/gobot/platforms/firmata"
    21  )
    22  
    23  func main() {
    24  	firmataAdaptor := firmata.NewAdaptor(os.Args[1])
    25  	buzzer := gpio.NewBuzzerDriver(firmataAdaptor, "3")
    26  
    27  	work := func() {
    28  		type note struct {
    29  			tone     float64
    30  			duration float64
    31  		}
    32  
    33  		song := []note{
    34  			{gpio.C4, gpio.Quarter},
    35  			{gpio.C4, gpio.Quarter},
    36  			{gpio.G4, gpio.Quarter},
    37  			{gpio.G4, gpio.Quarter},
    38  			{gpio.A4, gpio.Quarter},
    39  			{gpio.A4, gpio.Quarter},
    40  			{gpio.G4, gpio.Half},
    41  			{gpio.F4, gpio.Quarter},
    42  			{gpio.F4, gpio.Quarter},
    43  			{gpio.E4, gpio.Quarter},
    44  			{gpio.E4, gpio.Quarter},
    45  			{gpio.D4, gpio.Quarter},
    46  			{gpio.D4, gpio.Quarter},
    47  			{gpio.C4, gpio.Half},
    48  		}
    49  
    50  		for _, val := range song {
    51  			buzzer.Tone(val.tone, val.duration)
    52  			time.Sleep(10 * time.Millisecond)
    53  		}
    54  	}
    55  
    56  	robot := gobot.NewRobot("bot",
    57  		[]gobot.Connection{firmataAdaptor},
    58  		[]gobot.Device{buzzer},
    59  		work,
    60  	)
    61  
    62  	robot.Start()
    63  }