-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpcr_arduino.ino
153 lines (134 loc) · 2.61 KB
/
pcr_arduino.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <PID_v1.h>
#include <Messenger.h>
Messenger message;
double Setpoint, Input, Output;
int KP;
int KI;
PID myPid(&Input, &Output, &Setpoint, 1,1,0,DIRECT);
int set_tuningsKP()
{
int check_tunings = 0;
while (check_tunings == 0 )
{
if(Serial.available())
{
message.process(Serial.read());
}
KP = message.readInt();
if(KP>0)
{
Serial.println(KP);
myPid.SetTunings(KP,KI,0);
}
if(KP != 0)
{
check_tunings = 1;
}
else
check_tunings = 0;
}
}
int set_tuningsKI()
{
int check_tunings = 0;
while (check_tunings == 0 )
{
if(Serial.available())
{
message.process(Serial.read());
}
KI = message.readInt();
if (KI> 0)
Serial.println(KI);
if(KI != 0)
{
myPid.SetTunings(KP,KI,0);
check_tunings = 1;
}
else
check_tunings = 0;
}
}
//do set temp należałoby dodać jeszcze sprawdzanie czy liczba jest z zakresu 0-1023.
int set_temp()
{
int check_tunings = 0;
int T;
while (check_tunings == 0 )
{
digitalWrite(8,HIGH);
if(Serial.available())
{
message.process(Serial.read());
}
T = message.readInt();
if(T>0)
Serial.println(T);
if(T != 0)
{
Setpoint = T;
check_tunings = 1;
}
else
check_tunings = 0;
}
}
void send_temp()
{
int analog_read0,analog_read1,analog_read2,analog_read3;
analog_read0 = analogRead(A0);
analog_read1 = analogRead(A1);
analog_read2 = analogRead(A2);
analog_read3 = analogRead(A3);
String temp0 ,temp1,temp2,temp3;
String str0 = "<temp0>";
String str1 = "<temp1>";
String str2 = "<temp2>";
String str3 = "<temp3>";
temp0 = str0 + analog_read0 + str0;
temp1 = str1 + analog_read1 + str1;
temp2 = str2 + analog_read2 + str2;
temp3 = str3 + analog_read3 + str3;
Serial.println("<Sens>Sensors</Sens>");
Serial.println(temp0);
Serial.println(temp1);
Serial.println(temp2);
Serial.println(temp3);
Serial.flush();
}
void MessageCompleted()
{
if (message.checkString("b"))
{
send_temp();
}
if(message.checkString("a"))
{
set_tuningsKP();
}
if(message.checkString("c"))
{
set_tuningsKI();
}
if(message.checkString("t"))
{
set_temp();
}
}
void setup()
{
Serial.begin(115200);
message.attach(MessageCompleted);
myPid.SetMode(AUTOMATIC);
pinMode(8,OUTPUT);
}
void loop()
{
while ( Serial.available( ) ) message.process(Serial.read( ) );
Input = analogRead(0);
//Serial.println(Output);
//Serial.println(Input);
myPid.Compute();
analogWrite(3,Output);
digitalWrite(8,LOW);
}