diff --git a/lib/sensor/sensor.cpp b/lib/sensor/sensor.cpp new file mode 100644 index 0000000..cc8af8b --- /dev/null +++ b/lib/sensor/sensor.cpp @@ -0,0 +1,27 @@ +#include "sensor.hpp" +#include + +Sensor::Sensor() +{} + +void Sensor::init(const uint8_t pin) +{ + _pin = pin; + pinMode(_pin, INPUT); + digitalWrite(_pin, HIGH); +} + +float Sensor::get_value() +{ + /* + 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); +} \ No newline at end of file diff --git a/lib/sensor/sensor.hpp b/lib/sensor/sensor.hpp new file mode 100644 index 0000000..23d0426 --- /dev/null +++ b/lib/sensor/sensor.hpp @@ -0,0 +1,17 @@ +#ifndef HEADER_SENSOR +#define HEADER_SENSOR + +#include + +constexpr uint8_t SENSOR_PIN = 36; + +class Sensor { +public: + Sensor(); + void init(const uint8_t pin); + float get_value(); +private: + uint8_t _pin; +}; + +#endif // HEADER_SENSOR \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 930f696..1d9ccfc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,8 @@ #include #include +#include "sensor.hpp" + #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 @@ -13,6 +15,8 @@ Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // RN2483Serial using RX2 and TX2 HardwareSerial RN2483Serial(2); +Sensor gaz_sensor; + void printSSD1306(String text) { display.clearDisplay(); display.setCursor(0, 0); @@ -58,14 +62,19 @@ void test_RN2483A() { void setup() { // Initialize RN2483A Serial communication RN2483Serial.begin(57600); + Serial.begin(115200); // Initialize SSD1306 Init_SSD1306(); + Serial.println("Init_SSD Called."); + gaz_sensor.init(SENSOR_PIN); } void loop() { test_RN2483A(); + Serial.print("Value from sensor: "); + Serial.println(gaz_sensor.get_value()); delay(1000); }