hybridgroup.gobot/examples/firmata_led_brightness_with...

30 lines
833 B
Go

package main
import (
"fmt"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/firmata"
"github.com/hybridgroup/gobot/platforms/gpio"
)
func main() {
gbot := gobot.NewGobot()
firmataAdaptor := firmata.NewFirmataAdaptor("firmata", "/dev/ttyACM0")
sensor := gpio.NewAnalogSensorDriver(firmataAdaptor, "sensor", "0")
led := gpio.NewLedDriver(firmataAdaptor, "led", "3")
work := func() {
gobot.On(sensor.Events["data"], func(data interface{}) {
brightness := uint8(gobot.ToScale(gobot.FromScale(float64(data.(int)), 0, 1024), 0, 255))
fmt.Println("sensor", data)
fmt.Println("brightness", brightness)
led.Brightness(brightness)
})
}
gbot.Robots = append(gbot.Robots,
gobot.NewRobot("sensorBot", []gobot.Connection{firmataAdaptor}, []gobot.Device{sensor, led}, work))
gbot.Start()
}