gobot.io/x/gobot/v2@v2.1.0/examples/digispark_mpl115a2.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/digispark"
    16  )
    17  
    18  func main() {
    19  	board := digispark.NewAdaptor()
    20  	mpl115a2 := i2c.NewMPL115A2Driver(board)
    21  
    22  	work := func() {
    23  		gobot.Every(1*time.Second, func() {
    24  			press, _ := mpl115a2.Pressure()
    25  			fmt.Println("Pressure", press)
    26  
    27  			temp, _ := mpl115a2.Temperature()
    28  			fmt.Println("Temperature", temp)
    29  		})
    30  	}
    31  
    32  	robot := gobot.NewRobot("mpl115Bot",
    33  		[]gobot.Connection{board},
    34  		[]gobot.Device{mpl115a2},
    35  		work,
    36  	)
    37  
    38  	err := robot.Start()
    39  	if err != nil {
    40  		fmt.Println(err)
    41  	}
    42  }