document.addEventListener('DOMContentLoaded', () => {
const supplyVoltageInput = document.getElementById('supply-voltage');
const fixedResistorInput = document.getElementById('fixed-resistor');
const variableResistorMinInput = document.getElementById('variable-resistor-min');
const variableResistorMaxInput = document.getElementById('variable-resistor-max');
const variableResistorSlider = document.getElementById('variable-resistor');
const variableResistorValueOutput = document.getElementById('variable-resistor-value');
const outputVoltageOutput = document.getElementById('output-voltage');
const adcReadingOutput = document.getElementById('adc-reading');
// Funkcja do aktualizacji suwaka zmiennego rezystora
function updateSlider() {
const min = parseFloat(variableResistorMinInput.value);
const max = parseFloat(variableResistorMaxInput.value);
variableResistorSlider.min = min;
variableResistorSlider.max = max;
variableResistorSlider.value = (min + max) / 2;
variableResistorValueOutput.value = variableResistorSlider.value;
}
// Funkcja do obliczania wyników
function calculate() {
const supplyVoltage = parseFloat(supplyVoltageInput.value);
const fixedResistor = parseFloat(fixedResistorInput.value);
const variableResistor = parseFloat(variableResistorSlider.value);
// Oblicz napięcie wyjściowe
const outputVoltage = supplyVoltage * (variableResistor / (fixedResistor + variableResistor));
outputVoltageOutput.value = outputVoltage.toFixed(2);
// Oblicz odczyt ADC (10-bitowy, czyli 1024 poziomy)
const adcReading = Math.round((outputVoltage / supplyVoltage) * 1023);
adcReadingOutput.value = adcReading;
}
// Aktualizacja suwaka i obliczeń przy zmianie wartości
variableResistorSlider.addEventListener('input', () => {
variableResistorValueOutput.value = variableResistorSlider.value;
calculate();
});
// Aktualizacja zakresu suwaka i wyników przy zmianie parametrów
[supplyVoltageInput, fixedResistorInput, variableResistorMinInput, variableResistorMaxInput].forEach(input => {
input.addEventListener('input', () => {
updateSlider();
calculate();
});
});
// Inicjalizacja
updateSlider();
calculate();
});