gobot.io/x/gobot@v1.16.0/examples/megapi_motor.go (about)

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