gobot.io/x/gobot/v2@v2.1.0/examples/beaglebone_led_brightness.go (about)

     1  //go:build example
     2  // +build example
     3  
     4  //
     5  // Do not build by default.
     6  
     7  package main
     8  
     9  import (
    10  	"time"
    11  
    12  	"gobot.io/x/gobot/v2"
    13  	"gobot.io/x/gobot/v2/drivers/gpio"
    14  	"gobot.io/x/gobot/v2/platforms/beaglebone"
    15  )
    16  
    17  func main() {
    18  	beagleboneAdaptor := beaglebone.NewAdaptor()
    19  	led := gpio.NewLedDriver(beagleboneAdaptor, "P9_14")
    20  
    21  	work := func() {
    22  		brightness := uint8(0)
    23  		fadeAmount := uint8(5)
    24  
    25  		gobot.Every(100*time.Millisecond, func() {
    26  			led.Brightness(brightness)
    27  			brightness = brightness + fadeAmount
    28  			if brightness == 0 || brightness == 255 {
    29  				fadeAmount = -fadeAmount
    30  			}
    31  		})
    32  	}
    33  
    34  	robot := gobot.NewRobot("pwmBot",
    35  		[]gobot.Connection{beagleboneAdaptor},
    36  		[]gobot.Device{led},
    37  		work,
    38  	)
    39  
    40  	robot.Start()
    41  }