forked from allpowerlabs/KS_PowerPallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathControlInputs.pde
95 lines (93 loc) · 2.59 KB
/
ControlInputs.pde
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
void DoControlInputs() {
int control_input = analogRead(ANA_ENGINE_SWITCH);
if (grid_tie == 0){
if (abs(control_input-10)<20) { //"engine off"
if (control_state != CONTROL_OFF) {
control_state_entered = millis();
}
control_state = CONTROL_OFF;
}
if (abs(control_input-1023)<20) { //"engine off"
if (control_state != CONTROL_OFF) {
control_state_entered = millis();
}
control_state = CONTROL_OFF;
}
if (abs(control_input-683)<20) { //"engine on" and starter button pressed
if (control_state != CONTROL_START) {
control_state_entered = millis();
}
control_state = CONTROL_START;
}
if (abs(control_input-515)<20) { //"engine on"
if (control_state != CONTROL_ON) {
control_state_entered = millis();
}
control_state = CONTROL_ON;
}
} else { //Controlled by Deapsea
if (control_input > 515){
if (control_state == CONTROL_OFF){
control_state = CONTROL_START;
control_state_entered = millis();
Logln_p("# Deap Sea controller set to: Start");
}
if (control_state == CONTROL_START && (millis() - control_state_entered >= 500)){
control_state = CONTROL_ON;
Logln_p("# Deap Sea controller set to: On");
}
} else {
if (control_state != CONTROL_OFF) {
control_state_entered = millis();
control_state = CONTROL_OFF;
Logln_p("# Deap Sea controller set to: Off");
}
}
}
}
void smoothAnalog(int channel){ //channel is the analog channel, filterval is the number of past channelvalues to average over.
float ana_signal = analogRead(channel);
channel = getAnaArray(channel);
float smoothed_value = smoothed[channel];
float filterval = smoothed_filters[channel];
if (filterval > 0){
if (ana_signal > smoothed_value){
smoothed_value = smoothed_value + (ana_signal - smoothed_value)/filterval;
} else {
smoothed_value = smoothed_value - (smoothed_value - ana_signal)/filterval;
}
} else {
smoothed_value = ana_signal;
}
smoothed[channel] = int(smoothed_value);
}
int getAnaArray(int channel){
int ana_channel;
switch (channel){
case ANA0:
ana_channel = 0;
break;
case ANA1:
ana_channel = 1;
break;
case ANA2:
ana_channel = 2;
break;
case ANA3:
ana_channel = 3;
break;
case ANA4:
ana_channel = 4;
break;
case ANA5:
ana_channel = 5;
break;
case ANA6:
ana_channel = 6;
break;
case ANA7:
ana_channel = 7;
break;
}
return ana_channel;
}