From 61201913b170a87142fd43a8a797ce29c59bde18 Mon Sep 17 00:00:00 2001 From: jeromefroe Date: Fri, 1 Apr 2016 17:24:43 -0400 Subject: [PATCH] Added example of how to use temp36 temperature sensor with firmata --- examples/firmata_temp36.go | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 examples/firmata_temp36.go diff --git a/examples/firmata_temp36.go b/examples/firmata_temp36.go new file mode 100644 index 00000000..41c4db62 --- /dev/null +++ b/examples/firmata_temp36.go @@ -0,0 +1,43 @@ +package main + +import ( + "time" + + "fmt" + + "github.com/hybridgroup/gobot" + "github.com/hybridgroup/gobot/platforms/firmata" +) + +func main() { + gbot := gobot.NewGobot() + + firmataAdaptor := firmata.NewFirmataAdaptor("firmata", "/dev/ttyACM0") + + work := func() { + gobot.Every(1*time.Second, func() { + val, err := firmataAdaptor.AnalogRead("0") + if err != nil { + fmt.Println(err) + return + } + + voltage := (float64(val) * 5) / 1024 // if using 3.3V replace 5 with 3.3 + tempC := (voltage - 0.5) * 100 + tempF := (tempC * 9 / 5) + 32 + + fmt.Printf("%.2f°C\n", tempC) + fmt.Printf("%.2f°F\n", tempF) + }) + } + + robot := gobot.NewRobot("sensorBot", + []gobot.Connection{firmataAdaptor}, + []gobot.Device{}, + work, + ) + + gbot.AddRobot(robot) + + gbot.Start() +}