gobot.io/x/gobot/v2@v2.1.0/examples/chip_mpu6050.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/i2c"
    15  	"gobot.io/x/gobot/v2/platforms/chip"
    16  )
    17  
    18  func main() {
    19  	board := chip.NewAdaptor()
    20  	mpu6050 := i2c.NewMPU6050Driver(board)
    21  
    22  	work := func() {
    23  		gobot.Every(100*time.Millisecond, func() {
    24  			mpu6050.GetData()
    25  
    26  			fmt.Println("Accelerometer", mpu6050.Accelerometer)
    27  			fmt.Println("Gyroscope", mpu6050.Gyroscope)
    28  			fmt.Println("Temperature", mpu6050.Temperature)
    29  		})
    30  	}
    31  
    32  	robot := gobot.NewRobot("mpu6050Bot",
    33  		[]gobot.Connection{board},
    34  		[]gobot.Device{mpu6050},
    35  		work,
    36  	)
    37  
    38  	robot.Start()
    39  }