#include "PressureSensor.h"
+#include "Valve.h"
int main()
{
- PressureSensor test{A5};
-
init();
Serial.begin(9600);
+
+ main_loop();
+ return (0);
+}
+
+void main_loop()
+{
+ PressureSensor test{A5, 10};
+ Valve test_valve{6, 760, 2400};
+ int usec;
+
while (1)
{
- Serial.println(test.get_pressure());
- delay(500);
+ delay(5000);
+ test_valve.open(10);
+ delay(5000);
+ test_valve.close(100);
}
- return (0);
}
--- /dev/null
+#ifndef VALVE_H
+# define VALVE_H
+
+# include <Servo.h>
+
+class Valve
+{
+ private:
+ typedef int pin;
+
+ const Servo m_servo;
+ const int m_usec_opened;
+ const int m_usec_closed;
+ int m_usec_current;
+
+ void write_usec_timed(int usec_wanted, float seconds);
+ void update_pos();
+
+ public:
+ Valve() = delete;
+ Valve(const Valve &other) = delete;
+ Valve(pin pwm_pin, int usec_opened, int isec_closed);
+ ~Valve();
+
+ Valve &operator=(const Valve &other) = delete;
+
+ void open(float seconds = 5);
+ void close(float seconds = 5);
+};
+
+#endif // VALVE_H
--- /dev/null
+#include "Valve.h"
+
+Valve::Valve(pin pwm_pin, int usec_opened, int usec_closed)
+ : m_servo{}
+ , m_usec_opened{usec_opened}
+ , m_usec_closed{usec_closed}
+ , m_usec_current{usec_closed}
+{
+ m_servo.attach(pwm_pin);
+ update_pos();
+}
+
+Valve::~Valve()
+{
+}
+
+void Valve::write_usec_timed(int usec_wanted, float seconds)
+{
+ float msec_delay;
+ int step;
+
+ if (m_usec_current < usec_wanted)
+ step = 1;
+ else if (m_usec_current > usec_wanted)
+ step = -1;
+ else
+ return ;
+ msec_delay = step * seconds * (1.e3 / (usec_wanted - m_usec_current));
+ if (seconds <= 0 || msec_delay < 0.5)
+ {
+ m_usec_current = usec_wanted;
+ update_pos();
+ return ;
+ }
+ while (msec_delay < 10)
+ {
+ step *= 2;
+ msec_delay *= 2;
+ }
+ while ((step > 0 && usec_wanted > m_usec_current) || (step < 0 &&
+ usec_wanted < m_usec_current))
+ {
+ m_usec_current += step;
+ update_pos();
+ delay(static_cast<unsigned long>(msec_delay));
+ }
+}
+
+void Valve::open(float seconds)
+{
+ write_usec_timed(m_usec_opened, seconds);
+ //m_servo.writeMicroseconds(760);
+}
+
+void Valve::close(float seconds)
+{
+ write_usec_timed(m_usec_closed, seconds);
+ //m_servo.writeMicroseconds(2400);
+}
+
+void Valve::update_pos()
+{
+ m_servo.writeMicroseconds(m_usec_current);
+}