From 02e2fb71cda0746e6c19c45e81f9ff42cd92f754 Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Tue, 3 Dec 2024 16:23:40 +0100 Subject: [PATCH] Updated sensor. --- lib/sensor/sensor.cpp | 22 ++++++++++------------ lib/sensor/sensor.hpp | 6 ++++-- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/sensor/sensor.cpp b/lib/sensor/sensor.cpp index cc8af8b..936aaad 100644 --- a/lib/sensor/sensor.cpp +++ b/lib/sensor/sensor.cpp @@ -11,17 +11,15 @@ void Sensor::init(const uint8_t pin) digitalWrite(_pin, HIGH); } -float Sensor::get_value() +float Sensor::get_analog() { - /* - float sensor_volt; - float RS_gas; // Get value of RS in a GAS - float ratio; // Get ratio RS_GAS/RS_air - int sensorValue = analogRead(A0); - sensor_volt=(float)sensorValue/1024*5.0; - RS_gas = (5.0-sensor_volt)/sensor_volt; // omit *RL - //-Replace the name "R0" with the value of R0 in the demo of First Test - ratio = RS_gas/R0; // ratio = RS/R0 - */ - return ((analogRead(_pin))/1024*5.0); + return analogRead(_pin); +} + +// Normally should be 5V, but the ADC can only see up to 3.3 on 12 bits +float Sensor::get_RSR0() +{ + float sensor_volt=(float)(analogRead(_pin))/((1<<12)*3.3); + float RS_gas = (3.3-sensor_volt)/sensor_volt; + return RS_gas/R0; } \ No newline at end of file diff --git a/lib/sensor/sensor.hpp b/lib/sensor/sensor.hpp index 23d0426..d4d356a 100644 --- a/lib/sensor/sensor.hpp +++ b/lib/sensor/sensor.hpp @@ -3,13 +3,15 @@ #include -constexpr uint8_t SENSOR_PIN = 36; +constexpr uint8_t SENSOR_PIN = 34; +constexpr float R0 = 0.8; class Sensor { public: Sensor(); void init(const uint8_t pin); - float get_value(); + float get_analog(); + float get_RSR0(); private: uint8_t _pin; };