Created
October 27, 2015 17:30
-
-
Save veiset/a66da2ba407e89d745be to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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