From df22fa29ab8f7291553b993fe6b2a6b7210b8345 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Luk=C3=A1=C5=A1=20Ji=C5=99i=C5=A1t=C4=9B?= Date: Tue, 5 Nov 2024 15:22:14 +0100 Subject: [PATCH] Add Valve class, change Makefile and Servomatic.ino The main function needs to have init() before the declaration of first Valve, because the Valve constructor needs the arduino initialized. One cannot declare Valve before calling init(). Due to this and me liking variable declaration at the start of a function, I've written the actual contents of main to a new function. --- Makefile | 1 + Servomatic.ino | 21 +++++++++++++---- Valve.h | 31 ++++++++++++++++++++++++ Valve.ino | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 Valve.h create mode 100644 Valve.ino diff --git a/Makefile b/Makefile index 9adcd9e..3bf682c 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,7 @@ endif SRCS := Servomatic.ino \ PressureSensor.ino \ + Valve.ino \ NAME := Servomatic diff --git a/Servomatic.ino b/Servomatic.ino index 466b06a..f69b092 100644 --- a/Servomatic.ino +++ b/Servomatic.ino @@ -1,15 +1,26 @@ #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); } diff --git a/Valve.h b/Valve.h new file mode 100644 index 0000000..ab4a915 --- /dev/null +++ b/Valve.h @@ -0,0 +1,31 @@ +#ifndef VALVE_H +# define VALVE_H + +# include + +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 diff --git a/Valve.ino b/Valve.ino new file mode 100644 index 0000000..19542c1 --- /dev/null +++ b/Valve.ino @@ -0,0 +1,64 @@ +#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(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); +} -- 2.30.2