gobot.io/x/gobot/v2@v2.1.0/examples/nanopi_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 "fmt" 11 "time" 12 13 "gobot.io/x/gobot/v2" 14 "gobot.io/x/gobot/v2/drivers/gpio" 15 "gobot.io/x/gobot/v2/platforms/nanopi" 16 ) 17 18 // Wiring 19 // PWR NanoPi: 1, 17 (+3.3V, VCC); 2, 4 (+5V, VDD); 6, 9, 14, 20 (GND) 20 // GPIO NanoPi: the fourth header pin at inner USB side, count from USB side, is the PWM output 21 // LED: the PWM output is NOT able to drive a 20mA LED with full brightness, so a custom driver or low current LED is needed 22 // Expected behavior: the LED fades in and out 23 func main() { 24 r := nanopi.NewNeoAdaptor() 25 led := gpio.NewLedDriver(r, "PWM") 26 27 work := func() { 28 brightness := uint8(0) 29 fadeAmount := uint8(15) 30 31 gobot.Every(100*time.Millisecond, func() { 32 if err := led.Brightness(brightness); err != nil { 33 fmt.Println(err) 34 } 35 brightness = brightness + fadeAmount 36 if brightness == 0 || brightness == 255 { 37 fadeAmount = -fadeAmount 38 } 39 }) 40 } 41 42 robot := gobot.NewRobot("pwmBot", 43 []gobot.Connection{r}, 44 []gobot.Device{led}, 45 work, 46 ) 47 48 robot.Start() 49 }