Page 139 - Programming the Photon Getting Started With the Internet of Things
P. 139
void setup()
{
Spark.variable("cm", &cm, DOUBLE);
Spark.variable("inches", &inches, DOUBLE);
}
void loop()
{
cm = rangefinder.getDistanceCM();
inches = rangefinder.getDistanceInch();
delay(100);
}
In the first line we can import the HC_SR04 library so we can use all the functions to get
the distance from the sensor. The library can be found in the Particle build IDE by
searching for “HC SR04” and importing it into your program. For this experiment we are
going to provide two options to measure in both centimeters and inches, which we define
as double variables.
double cm = 0.0;
double inches = 0.0;
We also have created two spark variables for both centimeters and inches so we can
call the values from our webpage for whichever one we require. The loop function keeps
checking the distance every tenth of a second to update the value accurately. Our HTML
page will be similar to the previous experiment, with a few minor changes:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.j
s" type="text/javascript" charset="utf-8"></script>
<script src="raphael.2.1.0.min.js"></script>
<script src="justgage.1.0.1.min.js"></script>
<script>
var accessToken = "you access token here";
var deviceID = "you device id here"
var distance_url = "https://api.spark.io/v1/devices/" + deviceID + "/distan
ceCM";
function callbackDistance(data, status){
if (status == "success") {