include SoftwareSerial char firmware_version DEBUG Set to false to dis

  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
#include <SoftwareSerial.h>
char firmware_version[] = "0.7";
// DEBUG. Set to false to disable debug messages
bool DEBUG = false;
// Serial interfaces for output
bool USB_SERIAL_ENABLED = true; // arduino USB port
bool RS232_SERIAL_ENABLED = false; // DB9 RS-232 port
// Serial ports baud rate. Decrise if not stable link
int USB_SERIAL_SPEED = 9600;
int RS232_SERIAL_SPEED = 9600;
// Fire button
const int fireButtonPin = 7;
int fireButton = 0;
// 2 position zoom switch
const int zoomSwitchPin = 6;
int zoomSwitch = 0;
// 2 position fuse
const int fuseSwitchPin = 4;
int fuseSwitch = 0;
// 2 position speedSwitch
const int speedSwitchPin = 29;
int speedSwitch = 0;
// TV power on switch
const int tvOnPin = 27;
// RS-232 pins
SoftwareSerial mySerial(11, 10); //232_TX,232_RX
// We need to send switches status while encoders not moved
// for this we need non-blocking delay
unsigned long previousMillis = 0; // will store last time data sended
const long interval = 50; // interval at which data sends to serial port (milliseconds)
// 6 position switch
const int modeSwitchPin1 = 8;
const int modeSwitchPin2 = 9;
const int modeSwitchPin3 = 23;
const int modeSwitchPin4 = 25;
const int modeSwitchPin5 = 12;
const int modeSwitchPin6 = 13;
int modeSwitch = 0;
// Axis X encoder
const int encoderXpinA = 2; // white encoder cable
const int encoderXpinB = 3;
volatile unsigned long tempX, counterX = 0;
unsigned int encoderXdirection = 0;
// Axis Y encoder
const int encoderYpinA = 18;
const int encoderYpinB = 19;
volatile unsigned long tempY, counterY = 0;
unsigned int encoderYdirection = 0;
void setup() {
// set the data rate for the USB serial
Serial.begin (USB_SERIAL_SPEED);
// set the data rate for the SoftwareSerial RS232
mySerial.begin(RS232_SERIAL_SPEED);
// Open serial communications and wait for port to open
delay(100);
Serial.begin(USB_SERIAL_SPEED);
while (!Serial) {
; // wait for serial port to connect
}
//Print firmware version, baud rate and status for serial ports
Serial.println("Firmware version: " + (String)firmware_version); // to usb port
if (USB_SERIAL_ENABLED){
Serial.println("USB serial baud rate: " + (String)USB_SERIAL_SPEED);
} else {
Serial.println("USB serial disabled");
}
mySerial.println("Firmware version: " + (String)firmware_version); // to rs232 port
if (RS232_SERIAL_ENABLED){
mySerial.println("RS-232 serial baud rate: " + (String)RS232_SERIAL_SPEED);
} else {
mySerial.println("RS-232 serial disabled");
}
// initialize pins as pullup input
pinMode(fireButtonPin, INPUT_PULLUP);
pinMode(zoomSwitchPin, INPUT_PULLUP);
pinMode(fuseSwitchPin, INPUT_PULLUP);
pinMode(speedSwitchPin, INPUT_PULLUP);
pinMode(modeSwitchPin1, INPUT_PULLUP);
pinMode(modeSwitchPin2, INPUT_PULLUP);
pinMode(modeSwitchPin3, INPUT_PULLUP);
pinMode(modeSwitchPin4, INPUT_PULLUP);
pinMode(modeSwitchPin5, INPUT_PULLUP);
pinMode(modeSwitchPin6, INPUT_PULLUP);
// Axis X Encoder
pinMode(encoderXpinA, INPUT_PULLUP);
pinMode(encoderXpinB, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(encoderXpinA), encXinteruptA, RISING);
attachInterrupt(digitalPinToInterrupt(encoderXpinB), encXinteruptB, RISING);
// Axis Y Encoder
pinMode(encoderYpinA, INPUT_PULLUP);
pinMode(encoderYpinB, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(encoderYpinA), encYinteruptA, RISING);
attachInterrupt(digitalPinToInterrupt(encoderYpinB), encYinteruptB, RISING);
// Turn TV on
pinMode(tvOnPin, OUTPUT);
digitalWrite(tvOnPin, HIGH);
delay(1000);
digitalWrite(tvOnPin, LOW);
}
void SendData(int axisX = 0, int axisY = 0) {
// Built-in Arduino USB serial
if (USB_SERIAL_ENABLED) {
Serial.println (
(String)axisX
+ (String)axisY
+ modeSwitch
+ zoomSwitch
+ fuseSwitch
+ fireButton
+ speedSwitch
);
}
// RS-232 serial
if (RS232_SERIAL_ENABLED) {
mySerial.println(
(String)axisX
+ (String)axisY
+ modeSwitch
+ zoomSwitch
+ fuseSwitch
+ fireButton
+ speedSwitch
);
}
}
// Encoder X interrupts
void encXinteruptA() {
if (digitalRead(encoderXpinB) == LOW) {
counterX++;
} else {
counterX--;
}
}
void encXinteruptB() {
if (digitalRead(encoderXpinA) == LOW) {
counterX--;
} else {
counterX++;
}
}
// Encoder Y interrupts
void encYinteruptA() {
if (digitalRead(encoderYpinB) == LOW) {
counterY++;
} else {
counterY--;
}
}
void encYinteruptB() {
if (digitalRead(encoderYpinA) == LOW) {
counterY--;
} else {
counterY++;
}
}
void loop() {
// Fire button. LOW if pressed, HIGH if not pressed
if (digitalRead(fireButtonPin) == LOW) {
fireButton = 1;
} else {
fireButton = 0;
}
// Zoom switch. LOW if X1 state, HIGH if X10 state
if (digitalRead(zoomSwitchPin) == LOW) {
zoomSwitch = 1;
} else {
zoomSwitch = 0;
}
// Fuse switch. LOW if deactivated state, HIGH if active
if (digitalRead(fuseSwitchPin) == LOW) {
fuseSwitch = 1;
} else {
fuseSwitch = 0;
}
// Speed switch. LOW if deactivated state, HIGH if active
if (digitalRead(speedSwitchPin) == LOW) {
speedSwitch = 1;
} else {
speedSwitch = 0;
}
// 6 position switch
if (digitalRead(modeSwitchPin1) == LOW) {
modeSwitch = 1;
}
if (digitalRead(modeSwitchPin2) == LOW) {
modeSwitch = 2;
}
if (digitalRead(modeSwitchPin3) == LOW) {
modeSwitch = 3;
}
if (digitalRead(modeSwitchPin4) == LOW) {
modeSwitch = 4;
}
if (digitalRead(modeSwitchPin5) == LOW) {
modeSwitch = 5;
}
if (digitalRead(modeSwitchPin6) == LOW) {
modeSwitch = 6;
}
// Encoder X routine
if ( counterX != tempX ) {
if (DEBUG){Serial.println ((String)"Encoder X: "+counterX);}
if (counterX > tempX) {
encoderXdirection = 1;
}
else {
encoderXdirection = 2;
}
} else {
encoderXdirection = 0;
}
// Encoder Y routine
if ( counterY != tempY ) {
if (DEBUG){Serial.println ((String)"Encoder Y: "+counterY);}
if (counterY > tempY) {
encoderYdirection = 1;
}
else {
encoderYdirection = 2;
}
} else {
encoderYdirection = 0;
}
//
// Sending data to serial port
//
/*
// if encoders moved
if ( counterX != tempX || counterY != tempY) {
//for (int i=min(tempX, counterX); i<max(tempX, counterX); i++) {
//SendData(encoderXdirection, 0);
//}
//for (int i=min(tempY, counterY); i<max(tempY, counterY); i++) {
//SendData(0, encoderYdirection);
SendData(encoderXdirection,encoderYdirection);
}
// if encoders still
else {
SendData();
}
*/
// check if it's time to send data.
// If the difference between the current time and last time when we send data
unsigned long currentMillis = millis();
// send data if its right time
if (currentMillis - previousMillis >= interval) {
// save the last time we send data
previousMillis = currentMillis;
SendData();
}
// Reset counters when sended
// Encoders counters set to zero
tempX = counterX;
tempY = counterY;
} // end of main loop