DEBUG Set to false to disable debug messages bool DEBUG true Fire butt

  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
// DEBUG. Set to false to disable debug messages
bool DEBUG = true;
// 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;
// 6 position switch
const int modeSwitchPin1 = 8;
const int modeSwitchPin2 = 9;
const int modeSwitchPin3 = 10;
const int modeSwitchPin4 = 11;
const int modeSwitchPin5 = 12;
const int modeSwitchPin6 = 13;
int modeSwitch = 0;
// Axis X encoder
const int encoderXpinA = 2;
const int encoderXpinB = 3;
volatile unsigned int tempX, counterX = 0;
int encoderXdirection = 0;
// Axis Y encoder
const int encoderYpinA = 18;
const int encoderYpinB = 19;
volatile unsigned int tempY, counterY = 0;
int encoderYdirection = 0;
void setup() {
Serial.begin (115200);
// initialize pins as pullup input
pinMode(fireButtonPin, INPUT_PULLUP);
pinMode(zoomSwitchPin, INPUT_PULLUP);
pinMode(fuseSwitchPin, 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);
}
void SendData(int axisX = 0, int axisY = 0) {
Serial.println (
(String)axisX
+ (String)axisY
+ modeSwitch
+ zoomSwitch
+ fuseSwitch
+ fireButton
);
}
// 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;
}
// 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 ) {
Serial.println ((String)"Encoder X: "+counterX);
if (counterX > tempX) {
encoderXdirection = 1;
}
else {
encoderXdirection = 2;
}
//tempX = counterX;
} else {
encoderXdirection = 0;
}
// Encoder Y routine
if ( counterY != tempY ) {
Serial.println ((String)"Encoder Y: "+counterY);
if (counterY > tempY) {
encoderYdirection = 1;
}
else {
encoderYdirection = 2;
}
//tempX = counterX;
} else {
encoderYdirection = 0;
}
// Send data to serial port
if ( counterX != tempX || counterY != tempY) {
SendData();
}
/* Serial.println (
(String)encoderYdirection
+ (String)encoderXdirection
+ modeSwitch
+ zoomSwitch
+ fuseSwitch
+ fireButton
);
*/
// Reset counters when sended
// Encoders counters set to zero
tempX = counterX;
tempY = counterY;
} // end of main loop