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