gobot.io/x/gobot@v1.16.0/examples/firmata_led_brightness_with_analog_input.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_led_brightness_with_analog_input.go /dev/ttyACM0 10 */ 11 12 package main 13 14 import ( 15 "fmt" 16 "os" 17 18 "gobot.io/x/gobot" 19 "gobot.io/x/gobot/drivers/aio" 20 "gobot.io/x/gobot/drivers/gpio" 21 "gobot.io/x/gobot/platforms/firmata" 22 ) 23 24 func main() { 25 firmataAdaptor := firmata.NewAdaptor(os.Args[1]) 26 sensor := aio.NewAnalogSensorDriver(firmataAdaptor, "0") 27 led := gpio.NewLedDriver(firmataAdaptor, "3") 28 29 work := func() { 30 sensor.On(aio.Data, func(data interface{}) { 31 brightness := uint8( 32 gobot.ToScale(gobot.FromScale(float64(data.(int)), 0, 1024), 0, 255), 33 ) 34 fmt.Println("sensor", data) 35 fmt.Println("brightness", brightness) 36 led.Brightness(brightness) 37 }) 38 } 39 40 robot := gobot.NewRobot("sensorBot", 41 []gobot.Connection{firmataAdaptor}, 42 []gobot.Device{sensor, led}, 43 work, 44 ) 45 46 robot.Start() 47 }