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

     1  // +build example
     2  //
     3  // Do not build by default.
     4  
     5  /*
     6   How to run
     7   Pass serial port to use as the first param:
     8  
     9  	go run examples/firmata_motor.go /dev/ttyACM0
    10  */
    11  
    12  package main
    13  
    14  import (
    15  	"os"
    16  	"time"
    17  
    18  	"gobot.io/x/gobot"
    19  	"gobot.io/x/gobot/drivers/gpio"
    20  	"gobot.io/x/gobot/platforms/firmata"
    21  )
    22  
    23  func main() {
    24  	firmataAdaptor := firmata.NewAdaptor(os.Args[1])
    25  	motor := gpio.NewMotorDriver(firmataAdaptor, "3")
    26  
    27  	work := func() {
    28  		speed := byte(0)
    29  		fadeAmount := byte(15)
    30  
    31  		gobot.Every(100*time.Millisecond, func() {
    32  			motor.Speed(speed)
    33  			speed = speed + fadeAmount
    34  			if speed == 0 || speed == 255 {
    35  				fadeAmount = -fadeAmount
    36  			}
    37  		})
    38  	}
    39  
    40  	robot := gobot.NewRobot("motorBot",
    41  		[]gobot.Connection{firmataAdaptor},
    42  		[]gobot.Device{motor},
    43  		work,
    44  	)
    45  
    46  	robot.Start()
    47  }