-
Notifications
You must be signed in to change notification settings - Fork 3
/
serial.h
55 lines (50 loc) · 1.18 KB
/
serial.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
#pragma once
#include "Arduino.h"
void serialRun(){
static unsigned long tic = millis();
unsigned long tac = millis();
if(tac-tic<100) return;
Serial.print(reference); Serial.print(" ");
Serial.print(input+reference); Serial.print(" ");
Serial.print(output);
Serial.println();
tic = tac;
}
void serialEvent() {
static String s = "";
static char cmd = '\0';
static float v = 0;
while (Serial.available()) {
char c = (char)Serial.read();
double RR = 8; // respiratory rate
double PEEP = 2;
double PIP = 12;
double PPLAT = 10;
if(c=='P' || c=='I' || c=='D' || c == 'E' || c == 'W' || c == 'X' || c == 'Y' || c == 'Z'){
cmd = c;
}else if(c!='\n'){
s+=c;
}else{
v = s.toFloat();
if(cmd=='P'){
pidSetP(v);
}else if(cmd=='I'){
pidSetI(v);
}else if(cmd=='D'){
pidSetD(v);
}else if(cmd=='E'){
errorReset();
}else if(cmd=='W'){
referenceSetRR(v);
}else if(cmd=='X'){
referenceSetPEEP(v);
}else if(cmd=='Y'){
referenceSetPIP(v);
}else if(cmd=='Z'){
referenceSetPPLAT(v);
}
s = "";
c = '\0';
}
}
}