-
Notifications
You must be signed in to change notification settings - Fork 0
/
PompaHydroponix.ino
103 lines (78 loc) · 2.36 KB
/
PompaHydroponix.ino
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
/*
NOTES: beware of intterupt --> se can make error for all the actuator to work
*/
//PIN FOR FLOWMETER
const int flowmeter1 = D21;
const int flowmeter2 = D19;
const int flowmeter3 = D18;
//PIN FOR MOTOR DRIVER
//pwm
const int pwm1 = D34; //ena1
const int pwm2 = D26; //ena2
const int pwm3 = D12; //ena3
//in
const int onHigh1 = ; //in 1
const int lowAlways1 = ; //in2
const int onHigh2 = ; //in3
const int lowAlways2 = ; //in4
const int onHigh3 = ; //in5
const int lowAlways3 = ; //in6
//VARIABLE
//flowmeter
double volume1;
double volume2;
double volume3;
double pulse1;
double pulse2;
double pulse3;
//for pid
double dt, last_time;
double integral, previous, output = 0;
double kp, ki, kd;
void setup() {
// Serial
Serial.begin(9600);
// Motor driver
pinMode(pwm1, OUTPUT);
pinMode(pwm2, OUTPUT);
pinMode(pwm3, OUTPUT);
pinMode(onHigh1, OUTPUT);
pinMode(lowAlways1, OUTPUT);
pinMode(onHigh2, OUTPUT);
pinMode(lowAlways2, OUTPUT);
pinMode(onHigh3, OUTPUT);
pinMode(lowAlways3, OUTPUT);
//Flowmeter
pinMode(flowmeter1, INPUT);
pinMode(flowmeter2, INPUT);
pinMode(flowmeter3, INPUT);
//rawan error tapi gatau jalan atau ngga, kalo ngga jalan pulseCounternya buat 3
attachInterrupt(digitalPinToInterrupt(flowmeter1), pulseCounter1, RISING);
attachInterrupt(digitalPinToInterrupt(flowmeter2), pulseCounter2, RISING);
attachInterrupt(digitalPinToInterrupt(flowmeter3), pulseCounter3, RISING);
}
void loop() {
//for counting the volume
//if the volume same as target, stop the water
// we will use 3 error to do, i think we should do it sekuensial first if there is no problem we can do simultaneously
}
//counting pulse from flowmeter
void pulseCounter1(){
pulse1++;
}
void pulseCounter2(){
pulse2++;
}
void pulseCounter3(){
pulse3++;
}
//for pid using error from flowmeter
double pid(double error){
double proportional = error; //proportional is just for error
integral += error * dt; //integral is summing the error
double derivative = (error - previous) / dt; //know that the error will be reduce overtime, but if the rate is big, the previous is have big error, so it would have BIG difference negative. but if the rate is low, the previous and error at the time will have low negative difference
previous = error;
double output = (kp * proportional) + (ki * integral) + (kd * derivative); //pid
return output;
}
}