diff options
| author | Christian Kolset <ckolset@colostate.edu> | 2026-08-06 11:20:32 -0600 |
|---|---|---|
| committer | Christian Kolset <ckolset@colostate.edu> | 2026-08-06 11:23:16 -0600 |
| commit | 66d69b65379826f9282515d4a227178c4d7e0597 (patch) | |
| tree | 116e55dc678d98cc833fb3e1def99abab9c82807 /api_layers | |
| parent | 36ea532b4f7f335bece90f2c56a129e20ebc94e6 (diff) | |
Firmware update.
- Renamed and moved arduino firmware into it's own sketch for arduino
compiling interface.
- Added esp32 firmware for stringpotentiometer.
Diffstat (limited to 'api_layers')
| -rw-r--r-- | api_layers/firmware/arduinoUno.ino (renamed from api_layers/firmware/LabUI_firmware.ino) | 0 | ||||
| -rw-r--r-- | api_layers/firmware/esp32_stringPot/esp32_stringPot.ino | 66 |
2 files changed, 66 insertions, 0 deletions
diff --git a/api_layers/firmware/LabUI_firmware.ino b/api_layers/firmware/arduinoUno.ino index 8f1b252..8f1b252 100644 --- a/api_layers/firmware/LabUI_firmware.ino +++ b/api_layers/firmware/arduinoUno.ino diff --git a/api_layers/firmware/esp32_stringPot/esp32_stringPot.ino b/api_layers/firmware/esp32_stringPot/esp32_stringPot.ino new file mode 100644 index 0000000..a2efc64 --- /dev/null +++ b/api_layers/firmware/esp32_stringPot/esp32_stringPot.ino @@ -0,0 +1,66 @@ +/* + LabUI ESP32 Firmware v1.0 + ───────────────────────────────────────────────────────────────────────── + Author: Adam Shatila + Co-Author: Christian Kolset + + Upload this sketch to your ESP32 with a TE SP1 spring potensiometer. + + Required library: esp32 by Espressif Systems + + ───────────────────────────────────────────────────────────────────────── + */ + +const int sensorPin = A0; +const unsigned long sampleInterval = 2000; +unsigned long lastSampleTime = 0; +const double lowPassFilter_alpha = 0.00675; +const unsigned long calibration_factor = 1.07517; +const long calibration_offset = -267.25; +float filtered_displacement = 0.0; + +void setup() { + + Serial.begin(115200); + + while (!Serial) { + delay(10); + } + + analogReadResolution(12); +} + +void loop() { + unsigned long currentTime = micros(); + + if (currentTime - lastSampleTime >= sampleInterval) { + lastSampleTime = currentTime; + + uint32_t millivolts = analogReadMilliVolts(sensorPin); + float displacement = convert_mV_to_mm(millivolts); + filtered_displacement = lowPassFilter(displacement, filtered_displacement, lowPassFilter_alpha); + float volts = filtered_displacement; + + // Output the voltage to the serial monitor with 3 decimal places + //Serial.print(displacement, 3); + //Serial.print(", "); + Serial.println(filtered_displacement, 3); + //Serial.println(millivolts); + } +} + +float convert_mV_to_mm(float mVolts){ + // Reads mV from sensor and output in mm from lower limit + float voltage = mVolts / 1000.0; + float raw_val = voltage * (635.0 / 2.863); + //float inverted_val = 650.0 - raw_val; + //float volts = inverted_val * (635.0 / 637.0); + float volts = raw_val * (635.0 / 637.0); + + return calibration_factor * volts + calibration_offset; +} + +float lowPassFilter(float newSample, float prevOutput, float alpha) { + // Simple exponential low-pass filter (EMA) formula + return alpha * newSample + (1.0 - alpha) * prevOutput; +} |
