// 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