diff --git a/js/adc/ai-sensor.js b/js/adc/ai-sensor.js deleted file mode 100644 index 74b305f..0000000 --- a/js/adc/ai-sensor.js +++ /dev/null @@ -1,72 +0,0 @@ -// Script created by ChatGPT 4 -// @CodeAllNight / MrDerekJamison -// Discord server - https://discord.com/invite/NsjCvqwPAd -// Github - https://github.com/jamisonderek/flipper-zero-tutorials - -// Flipper Zero JavaScript ADC (Analog-to-Digital Converter) demo. - -// Required hardware connections... -// Step 1. Connect a NTC Thermistor (R0:2K, B:3470K) to 3.3V and Pin A7. -// Step 2. Connect a 2.2K Resistor to Pin A7 and GND. - -// Requires MNTM-005 (or current Momentum dev build) - -let gpio = require("gpio"); -let Math = require("math"); - -// Constants for the thermistor calculation -let B = 3470; // B value of the thermistor -let R0 = 2000; // Resistance of the thermistor at T0 (in ohms) -let T0 = 298.15; // Reference temperature (25°C in Kelvin) -let Vref = 3.3; // Reference voltage in volts - -// Initialize pin A7 as analog -gpio.init("PA7", "analog", "no"); - -// Start analog reading with a reference voltage of 3.3V -gpio.startAnalog(2048); // vRef = 2.048V (scaled) - -function readTemperature() { - let millivolts = gpio.readAnalog("PA7"); - - // Voltage divider calculation - let R1 = 2200; // Known resistor value in ohms (2.2kΩ) - let Vout = millivolts / 1000; // Convert millivolts to volts - - if (Vout === 0) { - return 0; - } - - let Rt = R1 * (Vref / Vout - 1); // Corrected thermistor resistance calculation - - // Steinhart-Hart equation to calculate temperature in Kelvin - let logRt = Math.log(Rt / R0); - let invT = (1 / T0) + (1 / B) * logRt; - let tempK = 1 / invT; - let tempC = tempK - 273.15; // Convert Kelvin to Celsius - let tempF = tempC * 9 / 5 + 32; // Convert Celsius to Fahrenheit - - return tempF; -} - -function formatTemperature(temp) { - let roundedTemp = Math.floor(temp * 100 + 0.5) / 100; // Round to two decimal places - let intPart = Math.floor(roundedTemp); // Integer part - let fracPart = Math.floor((roundedTemp - intPart) * 100); // Fractional part - - let intPartStr = to_string(intPart); - let fracPartStr = fracPart < 10 ? '0' + to_string(fracPart) : to_string(fracPart); // Ensure two digits in the fractional part - - return intPartStr + '.' + fracPartStr; -} - -// Main loop to read and display the temperature every second -while (true) { - let tempF = readTemperature(); - if (tempF !== 0) { // Ignore invalid measurements - print("Temperature: " + formatTemperature(tempF) + " F"); // Use " F" instead of "°F" - } - delay(1000); // Wait for 1 second -} - -gpio.stopAnalog(); \ No newline at end of file diff --git a/js/adc/sensor.js b/js/adc/sensor.js index 0d09248..109c1db 100644 --- a/js/adc/sensor.js +++ b/js/adc/sensor.js @@ -9,15 +9,16 @@ // Step 1. Connect a NTC Thermistor (R0:2K, B:3470K) to 3.3V and Pin A7. // Step 2. Connect a 2.2K Resistor to Pin A7 and GND. -// Requires MNTM-005 (or current Momentum dev build) +// Requires MNTM-008 (or current Momentum dev build) +// NOTE: The "gpio" module requires that "event_loop" be loaded first. +let eventLoop = require("event_loop"); let gpio = require("gpio"); let Math = require("math"); -gpio.init("PA7", "analog", "no"); - +let tempSensor = gpio.get("PA7"); // 2048mV reference voltage -gpio.startAnalog(2048); +tempSensor.init({ direction: "in", inMode: "analog" }); function mvToSensorResistance(millivolts) { let Rfixed = 2200; // 2200 ohm fixed resistor @@ -30,38 +31,37 @@ function mvToSensorResistance(millivolts) { } function resistanceToCelsius(rsensor) { - // B value of the thermistor - let B = 3470; - // Reference temp (25°C in Kelvin) - let T0 = 273.15 + 25; - // Resistance of the thermistor at T0 (in ohms) - let R0 = 2000; - // B or β parameter equation - // from https://en.wikipedia.org/wiki/Thermistor - let invT = (1 / T0) + (1 / B) * Math.log(rsensor/R0); - let tempK = 1 / invT; - // Convert Kelvin to Celsius - let tempC = tempK - 273.15; - return tempC; + // B value of the thermistor + let B = 3470; + // Reference temp (25°C in Kelvin) + let T0 = 273.15 + 25; + // Resistance of the thermistor at T0 (in ohms) + let R0 = 2000; + // B or β parameter equation + // from https://en.wikipedia.org/wiki/Thermistor + let invT = (1 / T0) + (1 / B) * Math.log(rsensor / R0); + let tempK = 1 / invT; + // Convert Kelvin to Celsius + let tempC = tempK - 273.15; + return tempC; } // Function to print 1 digits after decimal written by ChatGPT 4o. function formatTemperature(temp) { - let roundedTemp = Math.floor(temp * 10 + 0.5) / 10; // Round to one decimal place - let intPart = Math.floor(roundedTemp); // Integer part - let fracPart = Math.floor((roundedTemp - intPart) * 10); // Fractional part - return to_string(intPart) + '.' + to_string(fracPart); + let roundedTemp = Math.floor(temp * 10 + 0.5) / 10; // Round to one decimal place + let intPart = Math.floor(roundedTemp); // Integer part + let fracPart = Math.floor((roundedTemp - intPart) * 10); // Fractional part + return intPart.toString() + '.' + fracPart.toString(); } -for (let i=0; i<50; i++) { - let mv = gpio.readAnalog("PA7"); +for (let i = 0; i < 50; i++) { + let mv = tempSensor.read_analog(); let sensor_resistance = mvToSensorResistance(mv); let tempC = resistanceToCelsius(sensor_resistance); - let msg = to_string(mv) + " mV:"; - msg = msg + to_string(sensor_resistance) + " Ohms:"; + let msg = mv.toString() + " mV:"; + msg = msg + sensor_resistance.toString() + " Ohms:"; msg = msg + formatTemperature(tempC) + " C"; print(msg); delay(1000); // 1 second } -gpio.stopAnalog(); \ No newline at end of file