No Description

ldt0028.js 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Author: Sarah Knepper <sarah.knepper@intel.com>
  3. * Copyright (c) 2014 Intel Corporation.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. // Load Grove module
  25. var sensorModule = require('jsupm_ldt0028');
  26. var NUMBER_OF_SECONDS = 10;
  27. var MILLISECONDS_PER_SECOND = 1000;
  28. var SAMPLES_PER_SECOND = 50;
  29. var THRESHOLD = 100;
  30. // Create the LDT0-028 Piezo Vibration Sensor object using AIO pin 0
  31. var sensor = new sensorModule.LDT0028(0);
  32. // Read the signal every 20 milliseconds for 10 seconds
  33. console.log("For the next " + NUMBER_OF_SECONDS + " seconds, " +
  34. SAMPLES_PER_SECOND + " samples will be taken every second.");
  35. console.log("");
  36. var buffer = [];
  37. for (var i=0; i < NUMBER_OF_SECONDS * SAMPLES_PER_SECOND; i++) {
  38. buffer.push(sensor.getSample());
  39. delay(MILLISECONDS_PER_SECOND / SAMPLES_PER_SECOND );
  40. }
  41. // Print the number of times the reading was greater than the threshold
  42. var count = 0;
  43. for (var i=0; i < NUMBER_OF_SECONDS * SAMPLES_PER_SECOND; i++) {
  44. if (buffer[i] > THRESHOLD) {
  45. count++;
  46. }
  47. }
  48. console.log(sensor.name() + " exceeded the threshold value of " +
  49. THRESHOLD + " a total of " + count + " times,");
  50. console.log("out of a total of " + NUMBER_OF_SECONDS*SAMPLES_PER_SECOND +
  51. " readings.");
  52. console.log("");
  53. // Print a graphical representation of the average value sampled
  54. // each second for the past 10 seconds, using a scale factor of 15
  55. console.log("Now printing a graphical representation of the average reading ");
  56. console.log("each second for the last " + NUMBER_OF_SECONDS + " seconds.");
  57. var SCALE_FACTOR = 15;
  58. for (var i=0; i < NUMBER_OF_SECONDS; i++) {
  59. var sum = 0;
  60. for (var j=0; j < SAMPLES_PER_SECOND; j++) {
  61. sum += buffer[i*SAMPLES_PER_SECOND+j];
  62. }
  63. var average = sum / SAMPLES_PER_SECOND;
  64. var stars_to_print = Math.round(average / SCALE_FACTOR);
  65. var string = "(" + (" " + Math.round(average)).slice(-4) + ") | ";
  66. for (var j=0; j < stars_to_print; j++) {
  67. string += "*";
  68. }
  69. console.log(string);
  70. }
  71. function delay( milliseconds ) {
  72. var startTime = Date.now();
  73. while (Date.now() - startTime < milliseconds);
  74. }