Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Everything about electronics
Everything about electronics
Calculate the duty cycle of a signal using total pulse width and either ON time or OFF time.
s
ms
µs
ns
s
ms
µs
ns
s
ms
µs
ns
function convertToSeconds(value, unit) {
switch(unit) {
case “s”: return value;
case “ms”: return value / 1000;
case “us”: return value / 1e6;
case “ns”: return value / 1e9;
default: return 0;
}
}
function calculateDutyCycle() {
const total = parseFloat(document.getElementById(“totalWidth”).value);
const totalUnit = document.getElementById(“totalUnit”).value;
const on = parseFloat(document.getElementById(“onTime”).value);
const onUnit = document.getElementById(“onUnit”).value;
const off = parseFloat(document.getElementById(“offTime”).value);
const offUnit = document.getElementById(“offUnit”).value;
const totalSec = convertToSeconds(total, totalUnit);
let onSec = isNaN(on) ? null : convertToSeconds(on, onUnit);
const offSec = isNaN(off) ? null : convertToSeconds(off, offUnit);
const resultDiv = document.getElementById(“dutyResult”);
if (!total || isNaN(totalSec) || totalSec totalSec || onSec < 0) {
resultDiv.innerHTML = "⚠️ Invalid ON time.";
return;
}
const dutyCycle = (onSec / totalSec) * 100;
resultDiv.innerHTML = `⚙️ Duty Cycle: ${dutyCycle.toFixed(2)}%`;
}
function clearDutyCycle() {
document.getElementById(“totalWidth”).value = “”;
document.getElementById(“onTime”).value = “”;
document.getElementById(“offTime”).value = “”;
document.getElementById(“dutyResult”).innerHTML = “”;
}
(adsbygoogle = window.adsbygoogle || []).push({});