-
Notifications
You must be signed in to change notification settings - Fork 2
/
surfcube.ino
executable file
·254 lines (226 loc) · 6.7 KB
/
surfcube.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
#include <Servo.h>
#include <EEPROM.h>
#define clamp(x, low, high) max(low, min(x, high));
#define SERVO_MAX_ANGLE 115
#define CUBE_LEGTH 127
#define CUBE_WIDTH 80
#define CUBE_BASE_AREA (CUBE_LEGTH * CUBE_WIDTH)
#define STEPS_PER_ML 62
#define STEPPER_MAX_POSITION (STEPS_PER_ML * 300)
#define EEPROM_STEPPER_ADDR 0
#define UP 1
#define DOWN 0
#define RED_PIN 3
#define YELLOW_PIN 5
#define GREEN_PIN 6
#define RED_MASK 1
#define YELLOW_MASK ( 1 << 1 )
#define GREEN_MASK ( 1 << 2 )
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *myStepper = AFMS.getStepper(200, 2);
Servo servo;
unsigned long now = 0;
unsigned long serialNextCheck = 0;
int stepperCurrentPosition;
int stepperTargetPosition;
int servoUserTargetAngle = SERVO_MAX_ANGLE;
int servoTargetAngle = SERVO_MAX_ANGLE;
int servoCurrentAngle = SERVO_MAX_ANGLE;
unsigned long servoNextCheck = 0;
unsigned long servoFrequency = 7000;
unsigned long servoDelay = 2;
unsigned long keepAliveNextCheck = 0;
/*
0 -> OFF
1 -> red
2 -> yellow
3 -> red + yellow
4 -> green
5 -> red + green
6 -> yellow + green
7 -> red + yellow + green
*/
int LEDBreathingState = 0;
void setup() {
Serial.begin(9600);
AFMS.begin();
servo.attach(10);
myStepper->setSpeed(255);
servo.write(SERVO_MAX_ANGLE);
EEPROM.get(EEPROM_STEPPER_ADDR, stepperCurrentPosition);
stepperTargetPosition = stepperCurrentPosition;
Serial.print("read stepperCurrentPosition from EEPROM: ");
Serial.println(stepperCurrentPosition);
pinMode(RED_PIN, OUTPUT);
pinMode(YELLOW_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
}
/*
mm -> number of milimeters above the minimum.
*/
void tide(float mm) {
float mm3, ml;
Serial.print("CUBE_BASE_AREA = ");
Serial.println(CUBE_BASE_AREA);
Serial.print("mm = ");
Serial.println(mm);
mm3 = mm * CUBE_BASE_AREA;
Serial.print("mm3 = ");
Serial.println(mm3);
ml = mm3 / 1000;
Serial.print("ml = ");
Serial.println(ml);
stepperTargetPosition = ml * STEPS_PER_ML;
stepperTargetPosition = clamp(stepperTargetPosition, 0, STEPPER_MAX_POSITION);
Serial.print("Set stepperTargetPosition to ");
Serial.println(stepperTargetPosition);
}
/*
Temperature in Celsius
< 15 green
15 - 20 green + yellow
20 - 25 yellow
25 - 30 yellow + red
30 > red
*/
void setTemperature(int temperature) {
analogWrite(RED_PIN, 0);
analogWrite(YELLOW_PIN, 0);
analogWrite(GREEN_PIN, 0);
LEDBreathingState = 0;
if(temperature <= 15) {
LEDBreathingState = 4;
Serial.println("Setting led to green");
} else if(15 < temperature && temperature <= 20) {
LEDBreathingState = 6;
Serial.println("Setting led to green + yellow");
} else if(20 < temperature && temperature <= 25) {
LEDBreathingState = 2;
Serial.println("Setting led to yellow");
} else if(25 < temperature && temperature <= 30) {
LEDBreathingState = 3;
Serial.println("Setting led to yellow + red");
} else if(30 < temperature) {
LEDBreathingState = 1;
Serial.println("Setting led to red");
}
}
void checkSerial() {
String command;
int val;
if (Serial.available() > 0) {
Serial.println("Data received");
command = Serial.readStringUntil('\n');
while(Serial.available() == 0) {}
val = Serial.parseInt();
// Remove new line character after reading the integer.
Serial.readStringUntil('\n');
Serial.print("command = [");
Serial.print(command);
Serial.print("]; val = [");
Serial.print(val);
Serial.println("]");
if (command == "forward") {
stepperTargetPosition += val;
stepperTargetPosition = clamp(stepperTargetPosition, 0, STEPPER_MAX_POSITION);
} else if (command == "backward") {
stepperTargetPosition -= val;
stepperTargetPosition = clamp(stepperTargetPosition, 0, STEPPER_MAX_POSITION);
} else if (command == "servo") {
servoUserTargetAngle = val ? (val % (SERVO_MAX_ANGLE + 1)) : val;
} else if (command == "frequency") {
servoFrequency = val;
} else if (command == "delay") {
servoDelay = val;
} else if (command == "tide") {
tide((float)val);
} else if (command == "breathingstate") {
analogWrite(RED_PIN, 0);
analogWrite(YELLOW_PIN, 0);
analogWrite(GREEN_PIN, 0);
LEDBreathingState = val;
} else if (command == "red") {
LEDBreathingState &= ~RED_MASK;
analogWrite(RED_PIN, val);
} else if (command == "yellow") {
LEDBreathingState &= ~YELLOW_MASK;
analogWrite(YELLOW_PIN, val);
} else if (command == "green") {
LEDBreathingState &= ~GREEN_MASK;
analogWrite(GREEN_PIN, val);
} else if (command == "ping") {
Serial.println("pong");
} else if (command == "setstepperposition") {
stepperCurrentPosition = clamp(val, 0, STEPPER_MAX_POSITION);
stepperTargetPosition = clamp(val, 0, STEPPER_MAX_POSITION);
EEPROM.put(EEPROM_STEPPER_ADDR, stepperCurrentPosition);
} else if (command == "temperature") {
setTemperature(val);
} else {
Serial.println("Command not understood!");
}
}
serialNextCheck = now + 1;
}
void checkServo() {
if(servoCurrentAngle != servoTargetAngle) {
if(servoCurrentAngle < servoTargetAngle) {
++servoCurrentAngle;
}
if(servoCurrentAngle > servoTargetAngle) {
--servoCurrentAngle;
}
servo.write(servoCurrentAngle);
servoNextCheck = now + servoDelay;
}
if(servoCurrentAngle == servoTargetAngle) {
if(servoTargetAngle == SERVO_MAX_ANGLE) {
servoNextCheck = now + servoFrequency;
}
servoTargetAngle = servoTargetAngle == SERVO_MAX_ANGLE ? servoUserTargetAngle : SERVO_MAX_ANGLE;
}
}
void checkStepper() {
int stepperDirection;
if(stepperTargetPosition > stepperCurrentPosition) {
stepperDirection = FORWARD;
++stepperCurrentPosition;
} else {
stepperDirection = BACKWARD;
--stepperCurrentPosition;
}
myStepper->step(1, stepperDirection, SINGLE);
if(stepperCurrentPosition == stepperTargetPosition) {
EEPROM.put(EEPROM_STEPPER_ADDR, stepperCurrentPosition);
}
}
int counter = 0;
void updateLED() {
float val = (exp(sin(now/2000.0*PI)) - 0.36787944)*108.0;
if(LEDBreathingState & RED_MASK) {
analogWrite(RED_PIN, val);
}
if(LEDBreathingState & YELLOW_MASK) {
analogWrite(YELLOW_PIN, val);
}
if(LEDBreathingState & GREEN_MASK) {
analogWrite(GREEN_PIN, val);
}
}
void loop() {
now = millis();
if(now >= serialNextCheck) {
checkSerial();
}
if(stepperTargetPosition != stepperCurrentPosition) {
checkStepper();
} else if(now >= servoNextCheck) { // Only check servo if stepper has finished.
checkServo();
}
if(LEDBreathingState) {
updateLED();
}
}