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

     1  // +build example
     2  //
     3  // Do not build by default.
     4  
     5  /*
     6   How to run
     7   Pass serial port to use as the first param:
     8  
     9  	go run examples/firmata_temp36.go /dev/ttyACM0
    10  */
    11  
    12  package main
    13  
    14  import (
    15  	"os"
    16  	"time"
    17  
    18  	"fmt"
    19  
    20  	"gobot.io/x/gobot"
    21  	"gobot.io/x/gobot/platforms/firmata"
    22  )
    23  
    24  func main() {
    25  	firmataAdaptor := firmata.NewAdaptor(os.Args[1])
    26  
    27  	work := func() {
    28  		gobot.Every(1*time.Second, func() {
    29  			val, err := firmataAdaptor.AnalogRead("0")
    30  			if err != nil {
    31  				fmt.Println(err)
    32  				return
    33  			}
    34  
    35  			voltage := (float64(val) * 5) / 1024 // if using 3.3V replace 5 with 3.3
    36  			tempC := (voltage - 0.5) * 100
    37  			tempF := (tempC * 9 / 5) + 32
    38  
    39  			fmt.Printf("%.2f°C\n", tempC)
    40  			fmt.Printf("%.2f°F\n", tempF)
    41  		})
    42  	}
    43  
    44  	robot := gobot.NewRobot("sensorBot",
    45  		[]gobot.Connection{firmataAdaptor},
    46  		[]gobot.Device{},
    47  		work,
    48  	)
    49  
    50  	robot.Start()
    51  }