From 39e6065046be09d42d03e4e0c265e2b3f896d4a1 Mon Sep 17 00:00:00 2001 From: Sarah Knepper Date: Thu, 4 Dec 2014 10:58:38 +0000 Subject: [PATCH] grovetemp.cxx: Improve C++ example Signed-off-by: Sarah Knepper Signed-off-by: Brendan Le Foll --- examples/grovetemp.cxx | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/examples/grovetemp.cxx b/examples/grovetemp.cxx index 7ec07ed0..73de9c7f 100644 --- a/examples/grovetemp.cxx +++ b/examples/grovetemp.cxx @@ -1,5 +1,6 @@ /* * Author: Brendan Le Foll + * Contributions: Sarah Knepper * Copyright (c) 2014 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining @@ -24,18 +25,30 @@ #include #include +#include #include "grove.h" int main(int argc, char **argv) { //! [Interesting] - upm::GroveTemp* s = new upm::GroveTemp(0); - std::cout << s->name() << std::endl; + + // Create the temperature sensor object using AIO pin 0 + upm::GroveTemp* temp = new upm::GroveTemp(0); + std::cout << temp->name() << std::endl; + + // Read the temperature ten times, printing both the Celsius and + // equivalent Fahrenheit temperature, waiting one second between readings for (int i=0; i < 10; i++) { - std::cout << s->value() << std::endl; + int celsius = temp->value(); + int fahrenheit = (int) (celsius * 9.0/5.0 + 32.0); + printf("%d degrees Celsius, or %d degrees Fahrenheit\n", + celsius, fahrenheit); sleep(1); } + + // Delete the temperature sensor object + delete temp; //! [Interesting] return 0;