gobot.io/x/gobot/v2@v2.1.0/examples/tinkerboard_adxl345.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/tinkerboard"
    16  )
    17  
    18  // Wiring
    19  // PWR  Tinkerboard: 1 (+3.3V, VCC), 2(+5V), 6, 9, 14, 20 (GND)
    20  // I2C1 Tinkerboard: 3 (SDA-ws), 5 (SCL-gn)
    21  // ADXL345 plate: VCC, GND, SDL, SDA
    22  func main() {
    23  	a := tinkerboard.NewAdaptor()
    24  	adxl := i2c.NewADXL345Driver(a)
    25  
    26  	work := func() {
    27  		gobot.Every(1000*time.Millisecond, func() {
    28  			if x, y, z, err := adxl.XYZ(); err != nil {
    29  				fmt.Println(err)
    30  			} else {
    31  				fmt.Printf("x: %.7f | y: %.7f | z: %.7f \n", x, y, z)
    32  			}
    33  		})
    34  	}
    35  
    36  	robot := gobot.NewRobot("mpBot",
    37  		[]gobot.Connection{a},
    38  		[]gobot.Device{adxl},
    39  		work,
    40  	)
    41  
    42  	robot.Start()
    43  }