gobot.io/x/gobot/v2@v2.1.0/examples/chip_grove_accelerometer.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  	accel := i2c.NewGroveAccelerometerDriver(board)
    21  
    22  	work := func() {
    23  		gobot.Every(500*time.Millisecond, func() {
    24  			if x, y, z, err := accel.XYZ(); err == nil {
    25  				fmt.Println(x, y, z)
    26  				fmt.Println(accel.Acceleration(x, y, z))
    27  			} else {
    28  				fmt.Println(err)
    29  			}
    30  		})
    31  	}
    32  
    33  	robot := gobot.NewRobot("accelBot",
    34  		[]gobot.Connection{board},
    35  		[]gobot.Device{accel},
    36  		work,
    37  	)
    38  
    39  	robot.Start()
    40  }