gobot.io/x/gobot/v2@v2.1.0/examples/firmata_led_brightness_with_analog_input.go (about) 1 //go:build example 2 // +build example 3 4 // 5 // Do not build by default. 6 7 /* 8 How to run 9 Pass serial port to use as the first param: 10 11 go run examples/firmata_led_brightness_with_analog_input.go /dev/ttyACM0 12 */ 13 14 package main 15 16 import ( 17 "fmt" 18 "os" 19 20 "gobot.io/x/gobot/v2" 21 "gobot.io/x/gobot/v2/drivers/aio" 22 "gobot.io/x/gobot/v2/drivers/gpio" 23 "gobot.io/x/gobot/v2/platforms/firmata" 24 ) 25 26 func main() { 27 firmataAdaptor := firmata.NewAdaptor(os.Args[1]) 28 sensor := aio.NewAnalogSensorDriver(firmataAdaptor, "0") 29 led := gpio.NewLedDriver(firmataAdaptor, "3") 30 31 work := func() { 32 sensor.On(aio.Data, func(data interface{}) { 33 brightness := uint8( 34 gobot.ToScale(gobot.FromScale(float64(data.(int)), 0, 1024), 0, 255), 35 ) 36 fmt.Println("sensor", data) 37 fmt.Println("brightness", brightness) 38 led.Brightness(brightness) 39 }) 40 } 41 42 robot := gobot.NewRobot("sensorBot", 43 []gobot.Connection{firmataAdaptor}, 44 []gobot.Device{sensor, led}, 45 work, 46 ) 47 48 robot.Start() 49 }