From ded133e35f80cb3eb8c28ae414cc412f290d898e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Luk=C3=A1=C5=A1=20Ji=C5=99i=C5=A1t=C4=9B?= Date: Wed, 23 Oct 2024 10:43:22 +0200 Subject: [PATCH] Implement PressureSensor class and use it in main --- Makefile | 3 ++- PressureSensor.h | 24 ++++++++++++++++++++++++ PressureSensor.ino | 28 ++++++++++++++++++++++++++++ Servomatic.ino | 26 ++++++-------------------- 4 files changed, 60 insertions(+), 21 deletions(-) create mode 100644 PressureSensor.h create mode 100644 PressureSensor.ino diff --git a/Makefile b/Makefile index 31ea10c..a714226 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,8 @@ ifneq ("$(wildcard .verbose)","") MAINCMD += --verbose endif -SRCS := Servomatic.ino \ +SRCS := Servomatic.ino \ + PressureSensor.ino \ NAME := Servomatic diff --git a/PressureSensor.h b/PressureSensor.h new file mode 100644 index 0000000..1a849ae --- /dev/null +++ b/PressureSensor.h @@ -0,0 +1,24 @@ +#ifndef PRESSURESENSOR_H +# define PRESSURESENSOR_H + +class PressureSensor +{ + private: + typedef int pin; + + const pin m_analog_pin; + + static double signal_to_pressure(int ad_signal); + + public: + PressureSensor() = delete; + PressureSensor(pin analog_pin); + PressureSensor(const PressureSensor &other) = delete; + ~PressureSensor(); + + PressureSensor &operator=(const PressureSensor &other) = delete; + + double get_pressure(); +}; + +#endif // PRESSURESENSOR_H diff --git a/PressureSensor.ino b/PressureSensor.ino new file mode 100644 index 0000000..eae698e --- /dev/null +++ b/PressureSensor.ino @@ -0,0 +1,28 @@ +#include "PressureSensor.h" + +PressureSensor::PressureSensor(pin analog_pin) + : m_analog_pin{analog_pin} +{ +} + +PressureSensor::~PressureSensor() +{ +} + +// The Pressure sensor gives a current I/mA = 4 + 1.6 * p/bar with p absolute +// pressure from 0 bar to 10 bar (giving current between 4 mA and 20 mA). +// This current is passed through a 240 Ω resistor giving a voltage between +// 0.96 V and 4.8 V. Thus U/V = 0.96 + 0.384 * p/bar. +// Using the 10-bit AD convertor we get a signal from 0 to 2^10 - 1 on the range +// of 0 V to 5 V. +// Hence signal = (2^10 - 1) / (5 V) * U = 1023 / 5 * (0.96 + 0.384 * p/bar) +// And p/bar = 1 / 0.384 * (5 / (2^10 - 1) * signal - 0.96) +double PressureSensor::signal_to_pressure(int ad_signal) +{ + return (((static_cast(ad_signal) * 5) / (2 << 10 - 1) - 0.96) / 0.384); +} + +double PressureSensor::get_pressure() +{ + return (signal_to_pressure(analogRead(m_analog_pin))); +} diff --git a/Servomatic.ino b/Servomatic.ino index 684a35c..466b06a 100644 --- a/Servomatic.ino +++ b/Servomatic.ino @@ -1,29 +1,15 @@ -void wait_for_button(int trigger) -{ - int read_val; - - read_val = digitalRead(7); - while (read_val != trigger) - { - read_val = digitalRead(7); - delay(10); - } -} +#include "PressureSensor.h" int main() { + PressureSensor test{A5}; + init(); - pinMode(7, INPUT_PULLUP); - pinMode(4, OUTPUT); - digitalWrite(4, LOW); + Serial.begin(9600); while (1) { - wait_for_button(LOW); - digitalWrite(4, HIGH); - wait_for_button(HIGH); - wait_for_button(LOW); - digitalWrite(4, LOW); - wait_for_button(HIGH); + Serial.println(test.get_pressure()); + delay(500); } return (0); } -- 2.30.2