gobot.io/x/gobot/v2@v2.1.0/examples/firmata_mpu6050.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_mpu6050.go /dev/ttyACM0
    12  */
    13  
    14  package main
    15  
    16  import (
    17  	"fmt"
    18  	"os"
    19  	"time"
    20  
    21  	"gobot.io/x/gobot/v2"
    22  	"gobot.io/x/gobot/v2/drivers/i2c"
    23  	"gobot.io/x/gobot/v2/platforms/firmata"
    24  )
    25  
    26  func main() {
    27  	firmataAdaptor := firmata.NewAdaptor(os.Args[1])
    28  	mpu6050 := i2c.NewMPU6050Driver(firmataAdaptor)
    29  
    30  	work := func() {
    31  		gobot.Every(100*time.Millisecond, func() {
    32  			mpu6050.GetData()
    33  
    34  			fmt.Println("Accelerometer", mpu6050.Accelerometer)
    35  			fmt.Println("Gyroscope", mpu6050.Gyroscope)
    36  			fmt.Println("Temperature", mpu6050.Temperature)
    37  		})
    38  	}
    39  
    40  	robot := gobot.NewRobot("mpu6050Bot",
    41  		[]gobot.Connection{firmataAdaptor},
    42  		[]gobot.Device{mpu6050},
    43  		work,
    44  	)
    45  
    46  	robot.Start()
    47  }