-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJogBox.ino
More file actions
289 lines (248 loc) · 5.32 KB
/
JogBox.ino
File metadata and controls
289 lines (248 loc) · 5.32 KB
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
//Duet 2 Remote Pendant.
//Connects to PanelDue port.
//Uses Arduino Pro Mini 3V3 module (must be 3V3 version)
//Rotary encoder on pins 2 and 3.
//LEDS via 470 Ohm resistors to Ground on pins 6,8,10 for Indicating X Y Z axis
//Push Buttons to Ground with 4K7 Pull up resistors on pins 5,7,9 for selecting X Y Z Axis
//Push Buttons to Ground with 4K7 Pull up resistors on Pins 11,12,13,A0,A1,A2,A3,A4 for Custom commands
//Connection to PanelDue port using serial port on pins 0 and 1 plus ground and 5V.
//
//Colin Durbridge G4EML February 2020
#include <Encoder.h>
float stepsize;
bool absmode;
bool highspeed;
bool butok;
long lastmove;
long lastbut;
long lastresp;
long oldPosition;
int i;
int j;
int sent[8];
int axis=0;
int key[8];
char csm;
char str[20];
int linenumber;
bool respavail;
#define largestep 0.5
#define smallstep 0.05
char * command[]={"M98 P\"/macros/JogBox/1.g\"","M98 P\"/macros/JogBox/2.g\"","M98 P\"/macros/JogBox/3.g\"","M98 P\"/macros/JogBox/4.g\"",
"M98 P\"/macros/JogBox/5.g\"","M98 P\"/macros/JogBox/6.g\"","M98 P\"/macros/JogBox/7.g\"","M98 P\"/macros/JogBox/8.g\""};
#define RA 2
#define RB 3
#define LEDX 10
#define LEDY 8
#define LEDZ 6
#define SWX 9
#define SWY 7
#define SWZ 5
#define KEY1 12
#define KEY2 13
#define KEY3 15
#define KEY4 17
#define KEY5 11
#define KEY6 14
#define KEY7 16
#define KEY8 18
Encoder myEnc(RA, RB);
void setup()
{
Serial.begin(57600);
Serial.setTimeout(1000);
pinMode(LEDX,OUTPUT);
pinMode(LEDY,OUTPUT);
pinMode(LEDZ,OUTPUT);
pinMode(SWX,INPUT_PULLUP);
pinMode(SWY,INPUT_PULLUP);
pinMode(SWZ,INPUT_PULLUP);
key[0]=KEY1;
key[1]=KEY2;
key[2]=KEY3;
key[3]=KEY4;
key[4]=KEY5;
key[5]=KEY6;
key[6]=KEY7;
key[7]=KEY8;
for(i=0;i<8;i++)
{
pinMode(key[i],INPUT_PULLUP);
}
absmode=true;
lastmove=0;
highspeed=false;
butok=true;
lastbut=millis();
lastresp=millis();
axis=0;
digitalWrite(LEDX,LOW);
digitalWrite(LEDY,LOW);
digitalWrite(LEDZ,LOW);
delay(500);
csm=0;
linenumber=1;
respavail=false;
oldPosition=-999;
for(i=0;i<5;i++) // send a few linefeeds to establish serial link.
{
Serial.println("");
}
}
void chsprintlinenumber()
{
csm=0;
sprintf(str,"N%d ",linenumber++);
chsprint(str);
}
void chsprint(char* st)
{
for(j=0;st[j]!=0;j++)
{
csm=csm ^ st[j];
}
Serial.print(st);
}
void chsprintln(char* st)
{
for(j=0;st[j]!=0;j++)
{
csm=csm ^ st[j];
}
Serial.print(st);
Serial.print("*");
Serial.println(csm & 0xff);
csm=0;
respavail=true;
}
void flushresp(void)
{
chsprintlinenumber();
chsprintln("M408 S0 R1"); //request Json Status message including most recent errors. (We do nothing with it. This is just to stop the reprap output buffers filling up)
respavail=false;
lastresp=millis();
}
void loop()
{
long newPosition = myEnc.read()/2;
if (newPosition != oldPosition)
{
if(axis>0)
{
myEnc.write(0);
oldPosition = 0;
if(absmode)
{
chsprintlinenumber();
chsprintln("M120");
chsprintlinenumber();
chsprintln("G91");
absmode=false;
}
if(highspeed)
{
stepsize=largestep;
}
else
{
stepsize=smallstep;
}
chsprintlinenumber();
chsprint("G0 ");
if(axis==1) chsprint("X");
if(axis==2) chsprint("Y");
if(axis==3) chsprint("Z");
float mv=newPosition*stepsize;
dtostrf(mv,4,2,str);
chsprintln(str);
lastmove=millis();
}
else
{
myEnc.write(0);
oldPosition = 0;
}
}
if(((millis()-lastmove) > 1000) && (absmode==false))
{
chsprintlinenumber();
chsprintln("G90");
absmode=true;
chsprintlinenumber();
chsprintln("M121");
}
if(((millis()-lastresp)>1000) && respavail)
{
flushresp();
}
if(digitalRead(SWX)==HIGH && digitalRead(SWY)==HIGH && digitalRead(SWZ)==HIGH)
{
highspeed=false;
if(millis()>lastbut+200) butok=true;
}
if(digitalRead(SWX)==LOW && digitalRead(SWY)==HIGH && digitalRead(SWZ)==LOW)
{
axis=0;
highspeed=false;
lastbut=millis();
butok=false;
digitalWrite(LEDX,LOW);
digitalWrite(LEDY,LOW);
digitalWrite(LEDZ,LOW);
}
if(digitalRead(SWX)==LOW && digitalRead(SWY)==HIGH && digitalRead(SWZ)==HIGH)
{
highspeed=true;
if(butok)
{
axis=1;
lastbut=millis();
butok=false;
digitalWrite(LEDX,HIGH);
digitalWrite(LEDY,LOW);
digitalWrite(LEDZ,LOW);
}
}
if(digitalRead(SWX)==HIGH && digitalRead(SWY)==LOW && digitalRead(SWZ)==HIGH)
{
highspeed=true;
if(butok)
{
axis=2;
lastbut=millis();
butok=false;
digitalWrite(LEDX,LOW);
digitalWrite(LEDY,HIGH);
digitalWrite(LEDZ,LOW);
}
}
if(digitalRead(SWX)==HIGH && digitalRead(SWY)==HIGH && digitalRead(SWZ)==LOW)
{
highspeed=true;
if(butok)
{
axis=3;
lastbut=millis();
butok=false;
digitalWrite(LEDX,LOW);
digitalWrite(LEDY,LOW);
digitalWrite(LEDZ,HIGH);
}
}
for(i=0;i<8;i++)
{
if(digitalRead(key[i])==LOW)
{
if(sent[i]==0)
{
chsprintlinenumber();
chsprintln(command[i]);
}
sent[i]=2000;
}
else
{
if(sent[i]>0) sent[i]--;
}
}
}