Implement PressureSensor class and use it in main
authorLukáš Jiřiště <jiriste@icpf.cas.cz>
Wed, 23 Oct 2024 08:43:22 +0000 (10:43 +0200)
committerLukáš Jiřiště <jiriste@icpf.cas.cz>
Wed, 23 Oct 2024 08:43:22 +0000 (10:43 +0200)
Makefile
PressureSensor.h [new file with mode: 0644]
PressureSensor.ino [new file with mode: 0644]
Servomatic.ino

index 31ea10cf31625e34a96fdbd5ea89317659bf884f..a71422612399e69c76311903e644327e51f6b1c7 100644 (file)
--- 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 (file)
index 0000000..1a849ae
--- /dev/null
@@ -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 (file)
index 0000000..eae698e
--- /dev/null
@@ -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<double>(ad_signal) * 5) / (2 << 10 - 1) - 0.96) / 0.384);
+}
+
+double PressureSensor::get_pressure()
+{
+       return (signal_to_pressure(analogRead(m_analog_pin)));
+}
index 684a35c131c669e3b82ad92f0ad1445e9b7caa20..466b06aae47c00abd252ec0f5b2c30817d549bf7 100644 (file)
@@ -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);
 }