-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpassap_project_front_lock_arduino_Version_10.3_20.6.2020.ino
356 lines (253 loc) · 9.1 KB
/
passap_project_front_lock_arduino_Version_10.3_20.6.2020.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* Project "Passap-E6000-hacked-and-rebuilt", Version 4, 20.6.2020
Part: Front Lock Passap E6000
published under Hackaday
https://hackaday.io/project/163701-passap-e6000-rebuilt-and-replaced-console
created by Irene Wolf
*/
#include "CmdMessenger.h"
#include "Arduino.h"
#define Serial SerialUSB
byte patternArray[25]={0};
int series_length;
byte pat;
enum {
sPat, // incomming Data from Raspberry
sbPat, // get data from Raspberry and send data back
setEmptyPat,
sbEmptyPat,
setNPos,
sbNPos,
getNPos,
sendNPos,
setLeEnd,
sbLeEnd,
setRiEnd,
sbRiEnd,
getCursorPos,
sendCursorPos,
};
CmdMessenger c = CmdMessenger(Serial, ',',';','/');
///////////////////////////////////////////////////////////////////////////////
//Konstanten und Variablen Arduino - Passap E6000
///////////////////////////////////////////////////////////////////////////////
// GPIO Pins
#define PIN_CREF 9 // Sensor left
#define PIN_CSENSE 10 // Sensor right
#define PIN_NEEDLE_RTL 11 // Magnet: Pattern RTL (right to left)
#define PIN_NEEDLE_LTR 12 // Magnet: Pattern LTR (left to right)
// GPIO communication with Raspi, generates an interrupt on Raspi
#define PIN_RIGHT 2
#define PIN_LEFT 3
#define PIN_DIRECTIONCHANGE 4
// Variables direction VNB
volatile int csenseNow = 0;
volatile int crefNow = 0;
int currentCursorPosition = 100;
int patternPos = 0;
int needle =0;
int rightEnd = 70; // right end pattern
int leftEnd = 110; // left end pattern
// patternChange_R means, change of direction, right side
// patternChange_L means, change of direction, left side
boolean patternChange_R = true;
boolean patternChange_L = true;
volatile boolean interrupted = false;
volatile int state = 0;
///////////////////////////////////////////////////////////////////////////////
//Methods Arduino - Raspberry Pi
///////////////////////////////////////////////////////////////////////////////
// get new knit pattern row
void on_sPat(void){
series_length = c.readInt16Arg();
c.sendCmdStart(sbPat);
for (int i = 0; i < 23; i++){
pat=c.readBinArg<byte>();
patternArray[i] = pat;
c.sendCmdBinArg(patternArray[i]);
}
c.sendCmdEnd();
delay(5);
}
// get an empty knit pattern row, no needle will knit
void on_setEmptyPat(void){
series_length = c.readInt16Arg();
c.sendCmdStart(sbEmptyPat);
for (int i = 0; i < 23; i++){
pat=c.readBinArg<byte>();
patternArray[i] = pat;
c.sendCmdBinArg(patternArray[i]);
}
c.sendCmdEnd();
delay(5);
}
// set the 0 Position to calibrate
void on_set_null_pos(void){
int new_currentCursorPosition = c.readBinArg<int>();
c.sendCmdStart(sbNPos);
c.sendCmdBinArg((int)currentCursorPosition);
currentCursorPosition = 100;
c.sendCmdBinArg((int)currentCursorPosition);
c.sendCmdEnd();
delay(5);
}
// sets the desired left end needle position of the knitting
// not used at the moment
void on_set_leftEnd(){
leftEnd= c.readBinArg<int>();
c.sendBinCmd(sbLeEnd, leftEnd);
delay(5);
}
// sets the desired right end needle position of the knitting
// not used at the moment
void on_set_rightEnd(){
rightEnd = c.readBinArg<int>();
c.sendBinCmd(sbRiEnd, rightEnd);
delay(5);
}
// patternPos indicates the exact needle position, needle indicates state 0 or 1
void on_sendNPos(void){
c.sendBinCmd(sendNPos, patternPos);
//c.sendCmdEnd();
delay(5);
}
// currentCursorPosition
void on_sendCursorPos(void){
c.sendBinCmd(sendCursorPos, currentCursorPosition);
delay(5);
}
// The commands must be attached to the method
void attach_callbacks() {
c.attach(sPat, on_sPat);
c.attach(setEmptyPat, on_setEmptyPat);
c.attach(getNPos, on_sendNPos);
c.attach(setNPos, on_set_null_pos);
c.attach(setLeEnd, on_set_leftEnd);
c.attach(setRiEnd, on_set_rightEnd);
c.attach(getCursorPos, on_sendCursorPos);
}
///////////////////////////////////////////////////////////////////////////////
// Interrupt Routines
///////////////////////////////////////////////////////////////////////////////
// Pin CSENSE (light sensor) has changed state and triggered an interrupt
// The switch statement in the loop method requires unique cases. Therefore, I add 3 to the state of the
// light sensor CREF and multiply the result by 10. Thereafter, the state of
// the light sensor CSENSE is added.
// In this way, 4 different codes can be generated: 30, 31, 40, 41
void interrupt_CSENSE() {
interrupted = true; // flag
crefNow = digitalRead(PIN_CREF);
csenseNow = digitalRead(PIN_CSENSE);
state = ((crefNow + 3) * 10) + csenseNow;
}
// Pin CREF (light sensor) has changed state and triggered an interrupt
// The switch statement in the loop method requires unique cases. Therefore, I add 1 to the state of the
// light sensor CREF and multiply the result by 10. Thereafter, the state of
// the light sensor CSENSE is added.
// In this way, 4 different codes can be generated: 10, 11, 20, 21
void interrupt_CREF() {
interrupted = true; // flag
csenseNow = digitalRead(PIN_CSENSE);
crefNow = digitalRead(PIN_CREF);
state = ((crefNow + 1) * 10) + csenseNow;
// counting up, sets needle according to the pattern, , sends a signal to Raspberry Pi
if(state==10) {
if ((patternPos<=leftEnd) && (patternPos>=rightEnd)){
needle = bitRead(patternArray[patternPos/8], 7-(patternPos%8));
digitalWrite(PIN_NEEDLE_RTL, needle);
}
else if (patternPos == (leftEnd + 15)){
digitalWrite(PIN_NEEDLE_RTL, 1);
digitalWrite(PIN_LEFT, HIGH);
digitalWrite(PIN_LEFT, LOW);
}
else if (patternPos == (rightEnd - 20)){
digitalWrite(PIN_NEEDLE_RTL, 1);
digitalWrite(PIN_DIRECTIONCHANGE, HIGH);
digitalWrite(PIN_DIRECTIONCHANGE, LOW);
}
else {
digitalWrite(PIN_NEEDLE_RTL, 1);
}
currentCursorPosition += 1;
patternPos +=1;
}
// counting down, , sends a signal to Raspberry Pi
if(state==20){
currentCursorPosition -= 1;
patternPos -= 1;
if (patternPos == (rightEnd - 15)){
digitalWrite(PIN_RIGHT, HIGH);
digitalWrite(PIN_RIGHT, LOW);
}
}
}
///////////////////////////////////////////////////////////////////////////////
// Setup
///////////////////////////////////////////////////////////////////////////////
void setup(){
Serial.begin(115200);
while(!Serial);
attach_callbacks();
pinMode(PIN_LEFT, OUTPUT);
digitalWrite(PIN_LEFT, LOW);
pinMode(PIN_RIGHT, OUTPUT);
digitalWrite(PIN_RIGHT, LOW);
pinMode(PIN_DIRECTIONCHANGE, OUTPUT);
digitalWrite(PIN_DIRECTIONCHANGE, LOW);
// Setup Input-Pins
pinMode(PIN_CSENSE, INPUT_PULLUP);
pinMode(PIN_CREF, INPUT_PULLUP);
// Setup magnets
pinMode(PIN_NEEDLE_RTL, OUTPUT);
pinMode(PIN_NEEDLE_LTR, OUTPUT);
// Setup light sensors, pin change interrupt
attachInterrupt(digitalPinToInterrupt(PIN_CSENSE), interrupt_CSENSE, CHANGE);
attachInterrupt(digitalPinToInterrupt(PIN_CREF), interrupt_CREF, CHANGE);
// State of the light sensors
csenseNow = digitalRead(PIN_CSENSE);
crefNow = digitalRead(PIN_CREF);
// Set needle state
digitalWrite(PIN_NEEDLE_RTL, HIGH);
digitalWrite(PIN_NEEDLE_LTR, HIGH);
}
///////////////////////////////////////////////////////////////////////////////
//Loop
///////////////////////////////////////////////////////////////////////////////
void loop() {
if (interrupted){
interrupted = false;
switch(state){
// offset change, direction change, new form right to left
case 40: {
if (patternChange_R == true){
patternChange_L = true;
patternChange_R = false;
patternPos = currentCursorPosition - 126; // currentCursorPosition - 100 + 22;
}
}
break;
case 30: {
if ((patternPos<=leftEnd)&&(patternPos>=rightEnd)) {
needle = bitRead(patternArray[patternPos/8], 7-(patternPos%8));
digitalWrite(PIN_NEEDLE_LTR, needle);
}
else {
needle = 1;
digitalWrite(PIN_NEEDLE_LTR, needle);
}
}
break;
// offset change, direction change, new form left to right
case 41:{
if (patternChange_L == true){
patternChange_L = false;
patternChange_R = true;
patternPos = currentCursorPosition -116; // currentCursorPosition - 112 " 12"
}
}
break;
default: break;
}
}
c.feedinSerialData();
}