From c90295a57f23c0aa778219a3674cbba7fa76cc10 Mon Sep 17 00:00:00 2001 From: Luis Rodil-Fernandez Date: Tue, 15 Feb 2022 00:57:27 +0100 Subject: [PATCH] implemented a calibration process for the atomizer library --- src/atomizer.h | 53 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/src/atomizer.h b/src/atomizer.h index 3404755..5dd73cc 100644 --- a/src/atomizer.h +++ b/src/atomizer.h @@ -1,28 +1,67 @@ #include +#include +#include /** - * Simple controller library for an ultrasonic atomizer - * used in atomizers, scent dispensers, etc. + * Simple controller library for a cheap + * ultrasonic atomizer used in humidifiers, + * scent dispensers, etc. * * studio derfunke (c) 2022 */ class Atomizer { int pinSwitch; int pinSensor; - u_int lastToggle; // since last toggle action + u_int lastToggle; // timestamp of last toggle action + + MinMaxScaler minmax; public: - Atomizer(int pin) : pinSwitch(pin) { + Atomizer(int pin1, int pin2) + : pinSwitch(pin1), + pinSensor(pin2) { + + pinMode(PIN_ATOMIZER, OUTPUT); + pinMode(PIN_ATOMIZER_LDR, INPUT); } - bool debug() { + void debug() { int ldr = analogRead(pinSensor); - Serial.print(ldr); + LOG(ldr); LOG_NEW_LINE + } + + void calibrate() { + int reading; + float s; + int i, j; + + LOG("Calibrating atomizer actuation..."); LOG_NEW_LINE + + for(i = 0; i < 5; i++) { + LOG("Calibration loop "); LOG(i); LOG_NEW_LINE + + // do some readings on current state + for(int i = 0; i < 10; i++) { + reading = analogRead(pinSensor); + minmax.put( reading ); + } + + toggle(); // change state + + // do some more readings + for(int i = 0; i < 10; i++) { + reading = analogRead(pinSensor); + minmax.put( reading ); + } + + toggle(); // change state again + } // how many calibration steps } bool is_on() { int ldr = analogRead(pinSensor); - return ldr > 300; + float s = minmax.put( ldr ); // run our value through a minmax scaler + return (s > 0.85); } u_int elapsed() {