gobot.io/x/gobot/v2@v2.1.0/examples/megapi_motor.go (about) 1 //go:build example 2 // +build example 3 4 // 5 // Do not build by default. 6 7 package main 8 9 import ( 10 "time" 11 12 "gobot.io/x/gobot/v2" 13 "gobot.io/x/gobot/v2/platforms/megapi" 14 ) 15 16 func main() { 17 // use "/dev/ttyUSB0" if connecting with USB cable 18 // use "/dev/ttyAMA0" on devices older than Raspberry Pi 3 Model B 19 megaPiAdaptor := megapi.NewAdaptor("/dev/ttyS0") 20 motor := megapi.NewMotorDriver(megaPiAdaptor, 1) 21 22 work := func() { 23 speed := int16(0) 24 fadeAmount := int16(30) 25 26 gobot.Every(100*time.Millisecond, func() { 27 motor.Speed(speed) 28 speed = speed + fadeAmount 29 if speed == 0 || speed == 300 { 30 fadeAmount = -fadeAmount 31 } 32 }) 33 } 34 35 robot := gobot.NewRobot("megaPiBot", 36 []gobot.Connection{megaPiAdaptor}, 37 []gobot.Device{motor}, 38 work, 39 ) 40 41 robot.Start() 42 }