hybridgroup.gobot/examples/firmata_led_brightness_with...

36 lines
837 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("led", firmataAdaptor, "3")
work := func() {
gobot.On(sensor.Event("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)
})
}
robot := gobot.NewRobot("sensorBot",
[]gobot.Connection{firmataAdaptor},
[]gobot.Device{sensor, led},
work,
)
gbot.AddRobot(robot)
gbot.Start()
}