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

     1  // +build example
     2  //
     3  // Do not build by default.
     4  
     5  /*
     6   To run this example, pass the device ID as first param,
     7   and the access token as the second param:
     8  
     9  	go run examples/particle_led_brightness.go mydevice myaccesstoken
    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/particle"
    21  )
    22  
    23  func main() {
    24  	core := particle.NewAdaptor(os.Args[1], os.Args[2])
    25  	led := gpio.NewLedDriver(core, "A1")
    26  
    27  	work := func() {
    28  		brightness := uint8(0)
    29  		fadeAmount := uint8(25)
    30  
    31  		gobot.Every(500*time.Millisecond, func() {
    32  			led.Brightness(brightness)
    33  			brightness = brightness + fadeAmount
    34  			if brightness == 0 || brightness == 255 {
    35  				fadeAmount = -fadeAmount
    36  			}
    37  		})
    38  	}
    39  
    40  	robot := gobot.NewRobot("spark",
    41  		[]gobot.Connection{core},
    42  		[]gobot.Device{led},
    43  		work,
    44  	)
    45  
    46  	robot.Start()
    47  }