gobot.io/x/gobot/v2@v2.1.0/examples/gopigo3_servo.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  	"fmt"
    11  	"time"
    12  
    13  	"gobot.io/x/gobot/v2"
    14  	"gobot.io/x/gobot/v2/drivers/gpio"
    15  	"gobot.io/x/gobot/v2/platforms/dexter/gopigo3"
    16  	"gobot.io/x/gobot/v2/platforms/raspi"
    17  )
    18  
    19  func main() {
    20  	raspiAdaptor := raspi.NewAdaptor()
    21  	gpg3 := gopigo3.NewDriver(raspiAdaptor)
    22  	servo := gpio.NewServoDriver(gpg3, "SERVO_1")
    23  
    24  	work := func() {
    25  		gobot.Every(1*time.Second, func() {
    26  			i := uint8(gobot.Rand(180))
    27  			fmt.Println("Turning", i)
    28  			servo.Move(i)
    29  		})
    30  
    31  	}
    32  
    33  	robot := gobot.NewRobot("gopigo3servo",
    34  		[]gobot.Connection{raspiAdaptor},
    35  		[]gobot.Device{gpg3, servo},
    36  		work,
    37  	)
    38  
    39  	robot.Start()
    40  }