gobot.io/x/gobot/v2@v2.1.0/examples/firmata_motor.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_motor.go /dev/ttyACM0 12 */ 13 14 package main 15 16 import ( 17 "os" 18 "time" 19 20 "gobot.io/x/gobot/v2" 21 "gobot.io/x/gobot/v2/drivers/gpio" 22 "gobot.io/x/gobot/v2/platforms/firmata" 23 ) 24 25 func main() { 26 firmataAdaptor := firmata.NewAdaptor(os.Args[1]) 27 motor := gpio.NewMotorDriver(firmataAdaptor, "3") 28 29 work := func() { 30 speed := byte(0) 31 fadeAmount := byte(15) 32 33 gobot.Every(100*time.Millisecond, func() { 34 motor.Speed(speed) 35 speed = speed + fadeAmount 36 if speed == 0 || speed == 255 { 37 fadeAmount = -fadeAmount 38 } 39 }) 40 } 41 42 robot := gobot.NewRobot("motorBot", 43 []gobot.Connection{firmataAdaptor}, 44 []gobot.Device{motor}, 45 work, 46 ) 47 48 robot.Start() 49 }