Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
var original = {
resistorA: 470,
resistorB: 1,
capacitor: 0.01
};
var adapted = {
resistorA: 100,
resistorB: 0.27,
capacitor: 0.047
};
function getChargeTime(circuit) {
return 0.693 * (circuit.resistorA + circuit.resistorB) * circuit.capacitor;
}
function getDischargeTime(circuit) {
return 0.693 * circuit.resistorB * circuit.capacitor;
}
function getTotalPeriod(circuit) {
return getChargeTime(circuit) + getDischargeTime(circuit);
}
function getFrequency(circuit) {
var frequency = 1.44 / ((circuit.resistorA + circuit.resistorB) * circuit.capacitor);
// Making micro second readable
var hz = frequency * 1000;
if (hz > 1000) {
return (hz / 1000).toFixed(4) + 'khz';
} else {
return hz.toFixed(4) + ' hz';
}
}
function getDutyCycle(circuit) {
return circuit.resistorB / (circuit.resistorA + circuit.resistorB * 2)
}
function showStats(circuit, title) {
console.log('\n' + title + '\n');
console.log('Charge Time: ', getChargeTime(circuit).toFixed(4) + ' seconds');
console.log('Discharge Time: ', getDischargeTime(circuit).toFixed(4) + ' seconds');
console.log('Total Period: ', getTotalPeriod(circuit).toFixed(4));
console.log('Frequency: ', getFrequency(circuit));
console.log('Duty Cycle: ', getDutyCycle(circuit));
}
showStats(original, 'original');
showStats(adapted, 'adapted');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment