Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var clapDetector = require('clap-detector'),
_ = require('lodash');
clapDetector.start({
DETECTION_PERCENTAGE_START : '5%',
DETECTION_PERCENTAGE_END: '5%',
CLAP_AMPLITUDE_THRESHOLD: 0.5,
CLAP_ENERGY_THRESHOLD: 0.5,
CLAP_MAX_DURATION: 100
});
var spinTimes = [],
second = 0,
calories = 0,
distance = 0;
clapDetector.onClap(function() {
spinTimes.push(Date.now());
});
setInterval( everySecond, 1000 );
function calculateRPMs() {
var len = spinTimes.length,
lastRPM = 0;
if (len < 6) {
lastRPM = _.round( 60 / ( ( spinTimes[len - 1] - spinTimes[0] ) / ( len - 1 ) / 1000 ), 1 );
} else {
lastRPM = _.round( 60 / ( ( spinTimes[len - 1] - spinTimes[len - 6 ] ) / 5 / 1000 ), 1 );
}
return lastRPM;
}
function everySecond() {
var cadence = calculateRPMs(),
out = '';
if ( spinTimes.length ) {
second++;
out = second + ' seconds: ';
var watts = _.round( ( 0.172 * Math.pow( cadence, 2 ) ) - ( 12.16 * cadence ) + 271.3, 3 );
if ( watts > 0 ) {
calories += watts * 3.9 / 60 / 60;
var speed = cadence / ( 10 / 3 );
distance += speed / 60 / 60;
out = out + Math.floor( calories ) + ' cals ';
out = out + _.round( distance, 3 ) + ' miles ';
out = out + cadence + ' RPMs ';
out = out + _.round(speed, 2) + ' mph ';
out = out + watts + ' watts';
console.log(out);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.