upm/examples/javascript/ultrasonic.js
Mihai Tudor Panu 89d5de43e0 license: update to SPDX style license text throughout
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
2020-03-05 15:13:36 -08:00

33 lines
822 B
JavaScript

/*
* Author: Jun Kato <i@junkato.jp>
* Copyright (c) 2015 Jun Kato.
*
* Thanks to Seeed Studio for a working arduino sketch
*
* This program and the accompanying materials are made available under the
* terms of the The MIT License which is available at
* https://opensource.org/licenses/MIT.
*
* SPDX-License-Identifier: MIT
*/
var ultrasonic = require("jsupm_ultrasonic");
var sensor = new ultrasonic.UltraSonic(2);
var myInterval = setInterval(function()
{
var travelTime = sensor.getDistance();
if (travelTime > 0) {
var distance = (travelTime / 29 / 2).toFixed(3);
console.log("distance: " + distance + " [cm]");
}
}, 200);
// When exiting: clear interval and print message
process.on('SIGINT', function()
{
clearInterval(myInterval);
console.log("Exiting...");
process.exit(0);
});