MAINCMD += --verbose
endif
-SRCS := Servomatic.ino \
+SRCS := Servomatic.ino \
+ PressureSensor.ino \
NAME := Servomatic
--- /dev/null
+#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
--- /dev/null
+#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)));
+}
-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);
}