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

     1  // +build example
     2  //
     3  // Do not build by default.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"time"
    10  
    11  	"gobot.io/x/gobot"
    12  	"gobot.io/x/gobot/drivers/i2c"
    13  	"gobot.io/x/gobot/platforms/beaglebone"
    14  )
    15  
    16  func main() {
    17  	board := beaglebone.NewAdaptor()
    18  	accel := i2c.NewGroveAccelerometerDriver(board)
    19  
    20  	work := func() {
    21  		gobot.Every(500*time.Millisecond, func() {
    22  			if x, y, z, err := accel.XYZ(); err == nil {
    23  				fmt.Println(x, y, z)
    24  				fmt.Println(accel.Acceleration(x, y, z))
    25  			} else {
    26  				fmt.Println(err)
    27  			}
    28  		})
    29  	}
    30  
    31  	robot := gobot.NewRobot("accelBot",
    32  		[]gobot.Connection{board},
    33  		[]gobot.Device{accel},
    34  		work,
    35  	)
    36  
    37  	robot.Start()
    38  }