-
Notifications
You must be signed in to change notification settings - Fork 0
/
Valve.h
112 lines (90 loc) · 2.47 KB
/
Valve.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#ifndef VALVE_H
#define VALVE_H
#include "Parameters.h"
#include <Arduino.h>
#define VALVE_MOTOR_REVERSED false
// motor sample code
// https://www.velleman.eu/support/downloads/?code=VMA03
const uint8_t openingPin = A0,
motorSlowPin = 5,
motorBackwardPin = 6,
motorForwardPin = 7;
class Valve
{
public:
static void init() {
pinMode(motorBackwardPin, OUTPUT);
pinMode(motorForwardPin, OUTPUT);
pinMode(motorSlowPin, OUTPUT);
digitalWrite(motorBackwardPin, HIGH);
digitalWrite(motorForwardPin, HIGH);
digitalWrite(motorSlowPin, LOW);
moving = false;
};
static float getOpening() {
// returns the valve state in [0,1], where 0 is closed and 1 is open
int rawVal = analogRead(openingPin);
float opening = map(rawVal, 0, 767, 0, 1000) / 1000.;
return opening;
};
static void setOpening(float target) {
// sets the valve opening target
// depending on the current state, the motor will be engaged
target = min(target, 1.);
target = max(target, 0.);
float actual = getOpening();
float delta = target - actual;
float absDelta = abs(delta);
if (actual == target) {
stop();
return;
}
// for complete opening / closing keep moving
// a mechanical solution prevents the motor from taking damage
if (target == 0.) {
move(false);
return;
}
if (target == 1.) {
move(true);
return;
}
if (moving) {
if (absDelta < targetTolerance) {
stop();
}
else {
move(delta > 0);
}
}
else {
if (absDelta > openingTolerance) {
move(delta > 0);
}
}
};
static void open() {
move(true);
};
static void close() {
move(false);
};
static void move(bool forward) {
forward ^= VALVE_MOTOR_REVERSED;
moving = true;
digitalWrite(motorBackwardPin, !forward);
digitalWrite(motorForwardPin, forward);
};
static void stop() {
moving = false;
digitalWrite(motorBackwardPin, HIGH);
digitalWrite(motorForwardPin, HIGH);
};
static void setSlow(bool slow) {
digitalWrite(motorSlowPin, slow);
};
private:
static bool moving;
};
bool Valve::moving = false;
#endif