-
Notifications
You must be signed in to change notification settings - Fork 0
/
CaseAnnealer.ino
260 lines (198 loc) · 6.66 KB
/
CaseAnnealer.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
254
255
256
257
258
259
260
#include <AFMotor.h>
#include <Servo.h>
#include <EEPROM.h>
// LCD I2C 1602 LCD Display Module 16X2
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// DC hobby servo
// Servo1 (signal) >> Pin10
// Servo2 (Signal) >> Pin9
// motor range 20-165
Servo servo1;
#include <ezButton.h>
ezButton btnRun(9); // servo2 not used
ezButton btnInc(3); // M2 not used
ezButton btnDec(11); // M1 not used
//Global var for anneal duration
float annealing_duration = 3.00; //time in sec, default 3 sec
const String sAnnealingReady = "Annealing ready";
const String sAnnealingRun = "Annealing run";
const String sChangeTiming = "Red btn to save";
bool isIdle = true;
bool isChangeTime = false;
bool isRequestCancel = false;
// Relay
int relayPin = 2; //Pin 2
//Specifics for my servo
int servo_min = 25;
int servo_max = 160;
int servo_mid = 93; // the servoe start pos
// Stepper motor on M3+M4 200 steps per revolution
// M1 -> Pin 11
// M2 -> Pin 3
AF_Stepper stepper(200, 2);
// LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27, if new version please use 0x3F instead.
// --------------------------------------------------------------------------------------------------------------
void showLCD(String msgLn1="", String msgLn2="", int lcd_delay=500 ) {
// Fill text with space to clear screen
for (int i = msgLn1.length(); i <=16; i++) {
msgLn1 = msgLn1 + " ";
}
for (int i = msgLn2.length(); i <=16; i++) {
msgLn2 = msgLn2 + " ";
}
lcd.setCursor(0, 0); // set the cursor to column 0, line 0
lcd.print(msgLn1); // Print a message to the LCD
lcd.setCursor(0, 1); // set the cursor to column 0, line 1
lcd.print(msgLn2); // Print a message to the LCD.
// Serial.println(msgLn1);
// Serial.println(msgLn2);
delay(lcd_delay);
}
void DoAnnealing () {
unsigned long StartTime;
unsigned long EndTime;
char str[5]; // Allocate a character array to store the resul
const String sStartAnn = "Annealing";
String sCancelRequested = "";
showLCD(sStartAnn);
Serial.println(sStartAnn);
StartTime = millis();
digitalWrite(relayPin,HIGH); // turn the relay ON
// [NO] is connected to feed
// [NC] is not connected to feed
while ((millis()-StartTime) < (annealing_duration*1000)) {
CheckRunPressDuringExecution();
if (isRequestCancel) {
sCancelRequested = " - CR";
}
dtostrf((millis()-StartTime)/1000.00, 4, 2, str);
showLCD(sStartAnn+sCancelRequested, String(str) + " sec", 0);
}
EndTime = millis();
digitalWrite(relayPin,LOW); // turn the relay OFF
// [NO] is not connected to feed
// [NC] is connected to feed
showLCD("Annealing finished", "Time: "+String((EndTime - StartTime)/1000)+" sec", 1000);
Serial.println("Annealing finished / Time: "+String((EndTime - StartTime)/1000)+" sec");
}
void CaseFeed() {
const String sCaseFeed = "Feed case";
showLCD(sCaseFeed);
Serial.println(sCaseFeed);
stepper.setSpeed(50);
// stepper.step(400, FORWARD, INTERLEAVE); //too fast
stepper.step(200, FORWARD, MICROSTEP);
// stepper.step(200, FORWARD, DOUBLE);
stepper.release(); // stop rotation and turn off holding torque.
}
void CaseRelease() {
const String sReleaseCase = "Release case";
showLCD(sReleaseCase);
Serial.println(sReleaseCase);
servo1.write(servo_min);
delay(500); //1000 ms = 1 sec
servo1.write(servo_mid);
}
String GetAnnealTime() {
return "Time: "+String(annealing_duration)+" sec";
}
void AnnealOneCycle() {
CaseFeed();
CheckRunPressDuringExecution();
DoAnnealing();
CheckRunPressDuringExecution();
CaseRelease();
unsigned long StartTime;
unsigned long TotalWaitTime = 0;
StartTime = millis();
while (TotalWaitTime < 2000) {
CheckRunPressDuringExecution();
TotalWaitTime = millis() - StartTime;
}
}
void CheckRunPressDuringExecution() {
btnRun.loop();
if (btnRun.isPressed()) {
const String sCancelReq = "Cancel requested";
isRequestCancel = true;
showLCD(sCancelReq, "");
Serial.println(sCancelReq);
// delay(1000);
}
}
void setup() {
//--------------------------------------------------------------------------------------------------------------------
// LCD
lcd.init(); //initialize the lcd
lcd.backlight(); //open the backlight
showLCD("LCD ready");
//--------------------------------------------------------------------------------------------------------------------
Serial.begin(9600); // set up Serial library at 9600 bps
//--------------------------------------------------------------------------------------------------------------------
// Relay setup
pinMode(relayPin, OUTPUT); // Define the port attribute as output
digitalWrite(relayPin, LOW); // switch off relay:
showLCD("Relay ready");
//--------------------------------------------------------------------------------------------------------------------
// turn on servo
servo1.attach(10);
if (servo1.read() == servo_mid) {
showLCD("Case door closed.",
"Pos: "+String(servo1.read())+", "+servo1.readMicroseconds()+" ms");
}
//--------------------------------------------------------------------------------------------------------------------
// turn on stepper motor #2
stepper.setSpeed(200);
showLCD("Feeder motor", "ready...");
// Get stored anneal time
EEPROM.get(0, annealing_duration);
showLCD(sAnnealingReady, GetAnnealTime());
}
void loop() {
btnInc.loop();
btnDec.loop();
btnRun.loop();
if (btnRun.isPressed() || isRequestCancel) {
if (isIdle){ //Idle & change -> Save value
if (isChangeTime) { //Save
EEPROM.put(0, annealing_duration);
isChangeTime = false;
showLCD("Timing saved", GetAnnealTime());
delay(1000);
showLCD(sAnnealingReady, GetAnnealTime());
} else { //Idle & no change -> RUN
isIdle = false;
showLCD(sAnnealingRun, ""); //will run after exit this IF
}
} else { // not idel (running) -> Cancel run
isIdle = true;
isRequestCancel = false;
showLCD("Canceling...", GetAnnealTime());
delay(1000);
showLCD(sAnnealingReady, GetAnnealTime());
}
}
//If running, keep running
if (!isIdle) {
AnnealOneCycle();
}
// Inc/Dec buttons can be used only in Idel
if (isIdle) {
if (btnInc.isPressed()){
annealing_duration = annealing_duration + 0.1;
isChangeTime = true;
showLCD(sChangeTiming, GetAnnealTime(), 0);
}
if (btnDec.isPressed()){
annealing_duration = annealing_duration -0.1;
if (annealing_duration < 0) {
annealing_duration = 0;
}
isChangeTime = true;
showLCD(sChangeTiming, GetAnnealTime(), 0);
}
}
delay(100);
}