gobot.io/x/gobot/v2@v2.1.0/examples/gopigo3_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/dexter/gopigo3"
    15  	"gobot.io/x/gobot/v2/platforms/raspi"
    16  )
    17  
    18  func main() {
    19  	raspiAdaptor := raspi.NewAdaptor()
    20  	gpg3 := gopigo3.NewDriver(raspiAdaptor)
    21  	led := gpio.NewGroveLedDriver(gpg3, "AD_1_1")
    22  
    23  	work := func() {
    24  		brightness := uint8(0)
    25  		fadeAmount := uint8(15)
    26  
    27  		gobot.Every(100*time.Millisecond, func() {
    28  			led.Brightness(brightness)
    29  			brightness = brightness + fadeAmount
    30  			if brightness == 0 || brightness == 255 {
    31  				fadeAmount = -fadeAmount
    32  			}
    33  		})
    34  	}
    35  
    36  	robot := gobot.NewRobot("gopigo3pwm",
    37  		[]gobot.Connection{raspiAdaptor},
    38  		[]gobot.Device{gpg3, led},
    39  		work,
    40  	)
    41  
    42  	robot.Start()
    43  }