Skip to content

Instantly share code, notes, and snippets.

@mattrossman
Created May 15, 2022 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattrossman/a524171adb9584c645f7b38562892c82 to your computer and use it in GitHub Desktop.
Save mattrossman/a524171adb9584c645f7b38562892c82 to your computer and use it in GitHub Desktop.
/**
MIT License
Copyright (c) 2022 Matt Rossman
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <Arduino_JSON.h>
char* endpoint = SECRET_ENDPOINT;
const int pinBtnLo = D0;
const int pinBtnHi = D1;
const int transitiontime = 1; // 1 = 100 ms
const int transitiontimeMs = transitiontime * 100;
void setup() {
pinMode(pinBtnLo, INPUT);
pinMode(pinBtnHi, INPUT);
Serial.begin(9600);
Serial.println("");
WiFi.begin(SECRET_SSID, SECRET_PASSWORD);
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Using enpoint: ");
Serial.println(endpoint);
}
int bri;
int lastBri;
float tensionRaw = 0;
float tensionSmooth = 0;
const float lambda = 0.999;
unsigned long ms; // time
unsigned long prevMs = 0; // previous time
int dt; // delta time
unsigned long lastChangeTime; // time of last state change
void loop() {
// CLOCK
ms = millis();
dt = ms - prevMs;
prevMs = ms;
// TENSION + SMOOTHING
tensionRaw = readTension();
tensionSmooth = DampF(tensionSmooth, tensionRaw, lambda, (float) dt / 1000);
bri = tensionSmooth * 254;
Serial.printf("t: %.2f, b: %d", tensionSmooth, bri);
if ((bri != lastBri) && (ms - lastChangeTime > transitiontimeMs)) {
setBrightness(bri);
Serial.print("*");
// Save changed values
lastChangeTime = ms;
lastBri = bri;
}
Serial.println();
delay(10);
}
/**
* @param {int} bri - [1, 254]
*/
void setBrightness(int bri) {
HTTPClient http;
http.begin(endpoint);
http.addHeader("Content-Type", "applicaton/json");
JSONVar body;
body["on"] = true;
body["bri"] = bri;
body["transitiontime"] = transitiontime;
int httpCode = http.PUT(JSON.stringify(body));
http.end(); //Close connection
}
int Rmin = 150000;
int Rmax = 300000;
float readTension() {
const float Vin = 3.3; // voltage input
const int R1 = 999999; // known resistor (ohms)
int analogValue = 0; // reading from analogRead()
float Vout; // voltage output
float R2; // unknown resistor (ohms)
float tension; // computed tension;
analogValue = analogRead(A0);
Vout = Vin * analogValue / 1023;
R2 = Vout * R1 / (Vin - Vout);
// Calibration buttons
if (digitalRead(pinBtnLo) == HIGH) {
Rmax = R2;
}
if (digitalRead(pinBtnHi) == HIGH) {
Rmin = R2;
}
tension = 1.0 - (R2 - Rmin) / (Rmax - Rmin);
tension = constrain(tension, 0, 1);
return tension;
}
// Adapted from
// https://github.com/mrdoob/three.js/blob/c7d06c02e302ab9c20fe8b33eade4b61c6712654/src/math/MathUtils.js
float LerpF(float x, float y, float t) {
return ( 1 - t ) * x + t * y;
}
float DampF(float a, float b, float lambda, float dt) {
return LerpF(a, b, 1 - exp(-lambda * dt));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment