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 25// Load Grove module 26var sensorModule = require('jsupm_ldt0028'); 27 28var NUMBER_OF_SECONDS = 10; 29var MILLISECONDS_PER_SECOND = 1000; 30var SAMPLES_PER_SECOND = 50; 31var THRESHOLD = 100; 32 33// Create the LDT0-028 Piezo Vibration Sensor object using AIO pin 0 34var sensor = new sensorModule.LDT0028(0); 35 36// Read the signal every 20 milliseconds for 10 seconds 37console.log("For the next " + NUMBER_OF_SECONDS + " seconds, " + 38 SAMPLES_PER_SECOND + " samples will be taken every second."); 39console.log(""); 40var buffer = []; 41for (var i=0; i < NUMBER_OF_SECONDS * SAMPLES_PER_SECOND; i++) { 42 buffer.push(sensor.getSample()); 43 delay(MILLISECONDS_PER_SECOND / SAMPLES_PER_SECOND ); 44} 45 46// Print the number of times the reading was greater than the threshold 47var count = 0; 48for (var i=0; i < NUMBER_OF_SECONDS * SAMPLES_PER_SECOND; i++) { 49 if (buffer[i] > THRESHOLD) { 50 count++; 51 } 52} 53console.log(sensor.name() + " exceeded the threshold value of " + 54 THRESHOLD + " a total of " + count + " times,"); 55console.log("out of a total of " + NUMBER_OF_SECONDS*SAMPLES_PER_SECOND + 56 " readings."); 57console.log(""); 58 59// Print a graphical representation of the average value sampled 60// each second for the past 10 seconds, using a scale factor of 15 61console.log("Now printing a graphical representation of the average reading "); 62console.log("each second for the last " + NUMBER_OF_SECONDS + " seconds."); 63var SCALE_FACTOR = 15; 64for (var i=0; i < NUMBER_OF_SECONDS; i++) { 65 var sum = 0; 66 for (var j=0; j < SAMPLES_PER_SECOND; j++) { 67 sum += buffer[i*SAMPLES_PER_SECOND+j]; 68 } 69 var average = sum / SAMPLES_PER_SECOND; 70 var stars_to_print = Math.round(average / SCALE_FACTOR); 71 var string = "(" + (" " + Math.round(average)).slice(-4) + ") | "; 72 for (var j=0; j < stars_to_print; j++) { 73 string += "*"; 74 } 75 console.log(string); 76} 77 78function delay( milliseconds ) { 79 var startTime = Date.now(); 80 while (Date.now() - startTime < milliseconds); 81} 82 83