Skip to content

Instantly share code, notes, and snippets.

@veiset
Created October 27, 2015 17:30
Show Gist options
  • Save veiset/a66da2ba407e89d745be to your computer and use it in GitHub Desktop.
Save veiset/a66da2ba407e89d745be to your computer and use it in GitHub Desktop.
import five from 'johnny-five';
const board = new five.Board();
const SAMPLE_SIZE = 50;
const SAMPLE_FREQ = 1;
const SAMPLE_TIMES = 10;
board.on("ready", function() {
const mic = new five.Sensor({
pin:"A0",
freq: SAMPLE_FREQ
});
let data = [];
let range = [];
mic.on("data", function() {
if (data.length === SAMPLE_SIZE) {
const min = data.reduce((min, current) => min>current ? current : min, 100000);
const max = data.reduce((max, current) => max<current ? current : max, 0);
const voltage = (max-min)*3.3/1024.0;
range.push(voltage);
data = [];
}
if (range.length === SAMPLE_TIMES) {
const total = range.reduce((total, current) => total+current, 0);
console.log(total/range.length);
range = [];
}
data.push(this.value);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment