AI Code Generation

So, while I was messing around with my electronic circuits and ran across this AI Tool generator website called Calculator Academy. The coolest thing is I tried it and it generated an awesome regulator voltage divider page in about a minute. Here is what I asked the AI to generate.

Regulator voltage divider with Vout, Vref, R1 and R2. Calculate for output voltage, Regulator Reference Voltage, R1, R2 where all but 1 variable is known. R1 is to ground and R2 is to Vout . Vref is between R1 and R2. Label fields specific to use.

```html
<html>
<head>
<title>Voltage Divider Calculator</title>
<script>
function calculate() {
    var Vout = parseFloat(document.getElementById("Vout").value);
    var Vref = parseFloat(document.getElementById("Vref").value);
    var R1 = parseFloat(document.getElementById("R1").value);
    var R2 = parseFloat(document.getElementById("R2").value);
    
    if (!isNaN(Vout) && !isNaN(Vref) && !isNaN(R1)) {
        R2 = R1 * (Vout - Vref) / Vref;
        document.getElementById("R2").value = R2.toFixed(2);
    } else if (!isNaN(Vout) && !isNaN(Vref) && !isNaN(R2)) {
        R1 = R2 * Vref / (Vout - Vref);
        document.getElementById("R1").value = R1.toFixed(2);
    } else if (!isNaN(Vout) && !isNaN(R1) && !isNaN(R2)) {
        Vref = Vout * R2 / (R1 + R2);
        document.getElementById("Vref").value = Vref.toFixed(2);
    } else if (!isNaN(Vref) && !isNaN(R1) && !isNaN(R2)) {
        Vout = Vref * (R1 + R2) / R2;
        document.getElementById("Vout").value = Vout.toFixed(2);
    } else {
        alert("Please fill in exactly three fields.");
    }
}

function reset() {
    document.getElementById("Vout").value = "";
    document.getElementById("Vref").value = "";
    document.getElementById("R1").value = "";
    document.getElementById("R2").value = "";
}
</script>
</head>
<body>
<h2>Voltage Divider Calculator</h2>
<label for="Vout">Output Voltage (Vout):</label>
<input type="number" id="Vout" step="any"><br>
<label for="Vref">Regulator Reference Voltage (Vref):</label>
<input type="number" id="Vref" step="any"><br>
<label for="R1">Resistor R1 (to ground):</label>
<input type="number" id="R1" step="any"><br>
<label for="R2">Resistor R2 (to Vout):</label>
<input type="number" id="R2" step="any"><br>
<button onclick="calculate()">Calculate</button>
<button onclick="reset()">Reset</button>
</body>
</html>
```
<div style="text-align:center; margin-top:20px;"><a href="https://calculator.academy" target="_blank" rel="noopener noreferrer">Powered By Calculator Academy</a></div>

So for the most part it is correct. Wow, this is very awesome for about a half hour of messing it really paid off I am going to have to remember this site for future endeavors! Below is the corrected code the issue with both Vout and VRef calculation was it had R2 where it should have had R1 so I modified the code and it works correctly.

<html>
<head>
<title>Voltage Divider Calculator</title>
<script>
function calculate() {
    var Vout = parseFloat(document.getElementById("Vout").value);
    var Vref = parseFloat(document.getElementById("Vref").value);
    var R1 = parseFloat(document.getElementById("R1").value);
    var R2 = parseFloat(document.getElementById("R2").value);
    
    if (!isNaN(Vout) && !isNaN(Vref) && !isNaN(R1)) {
        R2 = R1 * (Vout - Vref) / Vref;
        document.getElementById("R2").value = R2.toFixed(2);
    } else if (!isNaN(Vout) && !isNaN(Vref) && !isNaN(R2)) {
        R1 = R2 * Vref / (Vout - Vref);
        document.getElementById("R1").value = R1.toFixed(2);
    } else if (!isNaN(Vout) && !isNaN(R1) && !isNaN(R2)) {
        Vref = Vout * R1 / (R1 + R2);
        document.getElementById("Vref").value = Vref.toFixed(2);
    } else if (!isNaN(Vref) && !isNaN(R1) && !isNaN(R2)) {
        Vout = Vref * (R1 + R2) / R1;
        document.getElementById("Vout").value = Vout.toFixed(2);
    } else {
        alert("Please fill in exactly three fields.");
    }
}

function reset() {
    document.getElementById("Vout").value = "";
    document.getElementById("Vref").value = "";
    document.getElementById("R1").value = "";
    document.getElementById("R2").value = "";
}
</script>
</head>
<body>
<h2>Voltage Divider Calculator</h2>
<label for="Vout">Output Voltage (Vout):</label>
<input type="number" id="Vout" step="any"><br>
<label for="Vref">Regulator Reference Voltage (Vref):</label>
<input type="number" id="Vref" step="any"><br>
<label for="R1">Resistor R1 (to ground):</label>
<input type="number" id="R1" step="any"><br>
<label for="R2">Resistor R2 (to Vout):</label>
<input type="number" id="R2" step="any"><br>
<button onclick="calculate()">Calculate</button>
<button onclick="reset()">Reset</button>
</body>
</html>

<div style="text-align:center; margin-top:20px;"><a href="https://calculator.academy" target="_blank" rel="noopener noreferrer">Powered By Calculator Academy</a></div>

Corrected Code Above Note R2 needed to be replaced by R1 bold above.

Here is the code in the page.


Voltage Divider Calculator

Voltage Divider Calculator





One Last Note

Here is the version of AI prompt that generated the corrected code also shown below.

Regulator voltage divider with Vout, Vref, R1 and R2. Calculate for output voltage, Regulator Reference Voltage, R1, R2 where all but 1 variable is known. R1 is to ground and R2 is to Vout . Vref is between R1 and R2. Label fields specific to use. Vref = Vout * R1 / (R1 + R2); and Vout = Vref * (R1 + R2) / R1;

```html
<html>
<head>
<title>Voltage Divider Calculator</title>
<script>
function calculate() {
    var Vout = parseFloat(document.getElementById("Vout").value);
    var Vref = parseFloat(document.getElementById("Vref").value);
    var R1 = parseFloat(document.getElementById("R1").value);
    var R2 = parseFloat(document.getElementById("R2").value);
    
    if (!isNaN(Vout) && !isNaN(R1) && !isNaN(R2)) {
        Vref = Vout * R1 / (R1 + R2);
        document.getElementById("Vref").value = Vref.toFixed(2);
    } else if (!isNaN(Vref) && !isNaN(R1) && !isNaN(R2)) {
        Vout = Vref * (R1 + R2) / R1;
        document.getElementById("Vout").value = Vout.toFixed(2);
    } else if (!isNaN(Vout) && !isNaN(Vref) && !isNaN(R2)) {
        R1 = (Vref * R2) / (Vout - Vref);
        document.getElementById("R1").value = R1.toFixed(2);
    } else if (!isNaN(Vout) && !isNaN(Vref) && !isNaN(R1)) {
        R2 = (Vout * R1) / Vref - R1;
        document.getElementById("R2").value = R2.toFixed(2);
    } else {
        alert("Please enter values for at least three fields.");
    }
}

function reset() {
    document.getElementById("Vout").value = "";
    document.getElementById("Vref").value = "";
    document.getElementById("R1").value = "";
    document.getElementById("R2").value = "";
}
</script>
</head>
<body>
<h2>Voltage Divider Calculator</h2>
<label for="Vout">Output Voltage (Vout):</label>
<input type="number" id="Vout" step="any"><br>
<label for="Vref">Regulator Reference Voltage (Vref):</label>
<input type="number" id="Vref" step="any"><br>
<label for="R1">Resistor R1 (to ground):</label>
<input type="number" id="R1" step="any"><br>
<label for="R2">Resistor R2 (to Vout):</label>
<input type="number" id="R2" step="any"><br>
<button onclick="calculate()">Calculate</button>
<button onclick="reset()">Reset</button>
</body>
</html>
```
<div style="text-align:center; margin-top:20px;"><a href="https://calculator.academy" target="_blank" rel="noopener noreferrer">Powered By Calculator Academy</a></div>

Ok Last one I Promise

I used Grok to test this out too.

html code to calculate Regulator voltage divider with Vout, Vref, R1 and R2. Calculate for output voltage, Regulator Reference Voltage, R1, R2 where all but 1 variable is known. R1 is to ground and R2 is to Vout . Vref is between R1 and R2. Label fields specific to use.

Here is Grok's result

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Voltage Divider Calculator for Regulators</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            margin-top: 50px;
        }
        form {
            margin: 20px auto;
            width: 50%;
        }
        label, input {
            display: block;
            margin: 10px auto;
            width: 80%;
        }
        input[type="number"] {
            width: 70%;
            padding: 5px;
        }
        button {
            margin-top: 20px;
            padding: 10px 20px;
            font-size: 1em;
        }
        #result {
            margin: 20px auto;
            font-weight: bold;
            width: 50%;
            text-align: center;
        }
    </style>
</head>
<body>
    <h1>Voltage Divider Calculator for Regulators</h1>
    <form id="voltageDividerForm">
        <label for="vout">Output Voltage (Vout) in Volts:</label>
        <input type="number" step="0.01" id="vout">

        <label for="vref">Reference Voltage (Vref) in Volts:</label>
        <input type="number" step="0.01" id="vref">

        <label for="r1">Resistance R1 in Ohms:</label>
        <input type="number" id="r1">

        <label for="r2">Resistance R2 in Ohms:</label>
        <input type="number" id="r2">

        <button type="submit">Calculate</button>
    </form>
    <div id="result"></div>

    <script>
        document.getElementById('voltageDividerForm').addEventListener('submit', function(event) {
            event.preventDefault();
            let vout = parseFloat(document.getElementById('vout').value) || null;
            let vref = parseFloat(document.getElementById('vref').value) || null;
            let r1 = parseFloat(document.getElementById('r1').value) || null;
            let r2 = parseFloat(document.getElementById('r2').value) || null;

            let known = [vout, vref, r1, r2].filter(value => value !== null);
            let unknownIndex = [vout, vref, r1, r2].findIndex(value => value === null);

            if (known.length === 3) {
                let unknownLabel = ['Vout', 'Vref', 'R1', 'R2'][unknownIndex];
                let formula;

                switch(unknownIndex) {
                    case 0: // Vout
                        if (r1 && r2) formula = vref * (1 + r2/r1);
                        break;
                    case 1: // Vref
                        if (vout && r1 && r2) formula = vout / (1 + r2/r1);
                        break;
                    case 2: // R1
                        if (vout && vref && r2) formula = r2 / ((vout/vref) - 1);
                        break;
                    case 3: // R2
                        if (vout && vref && r1) formula = r1 * ((vout/vref) - 1);
                        break;
                }

                if (formula) {
                    document.getElementById('result').innerText = `${unknownLabel} = ${formula.toFixed(2)}`;
                } else {
                    document.getElementById('result').innerText = "Insufficient or incorrect data for calculation.";
                }
            } else {
                document.getElementById('result').innerText = "Please enter exactly three known values.";
            }
        });
    </script>
</body>
</html>

Here is the Grok Version Embedded

Voltage Divider Calculator for Regulators

Voltage Divider Calculator for Regulators

Also, I had to remove the styles from the Grok code as it was messing with the entire post. Other than that, the Grok version works flawlessly. Wow, thanks Grok! I will be looking to it more in the future for my calculations and may bring more calculators online because it is a big part of what I do these days.

Leave a Reply

Your email address will not be published. Required fields are marked *