// ENCODER WIRING
// Red : VCC = 5V
// Black: 0V = GND
// White: OUTA = Pin 2
// Green: OUTB = Pin 3
//////////////////////
//SHIFT WIRING
//Black: GND
//Red: Pin 8
//White: Pin 9
//////////////////////
//PEDALBOARD WIRING
//Yellow (throttle): GND
//Orange (throttle): 5V
//Black (throttle): pin A0
//Green (brake): GND
//White (brake): 5v
//Brown (brake): pin A1
//////////////////////
//BUTTON WIRING
//
#include <Joystick.h>
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_MULTI_AXIS,
4, 0, // Button Count, Hat Switch Count
true, true, true, // X and Y, but no Z Axis
false, false, false, // No Rx, Ry, or Rz
false, false, // No rudder or throttle
true, true, false); // No accelerator, brake, or steering
const int outA = 2;
const int outB = 3;
volatile long encoder = 0; // declare volatile since modified by interrupt routines
long encoder_save = 0;
void setup() {
Joystick.begin();
Joystick.setXAxisRange(-1600, 1600);
Joystick.setYAxisRange(0, 1023);
Joystick.setZAxisRange(0, 1023);
pinMode(8, INPUT_PULLUP); //Shift pin
pinMode(9, INPUT_PULLUP); //Shift pin
pinMode(outA, INPUT_PULLUP); //Encoder pin
pinMode(outB, INPUT_PULLUP); //Encoder pin
// attach interrupts to pins
// call digitalPinToInterrupt(pin) to be compatible with different Arduino boards
attachInterrupt(digitalPinToInterrupt(outA), outAChange, CHANGE);
attachInterrupt(digitalPinToInterrupt(outB), outBChange, CHANGE);
}
void loop() {
if (encoder_save != encoder) { // only print if new value
encoder_save = encoder;
Joystick.setXAxis(encoder);
}
delay(10);
int BrakePot = analogRead(A0);
int ThrottlePot = analogRead(A1);
Joystick.setYAxis(ThrottlePot);
Joystick.setZAxis(BrakePot);
//Shift Up
if(digitalRead(8) == LOW){
Joystick.setButton(0, 1);
} else {
Joystick.setButton(0, 0);
}
//Shift Down
if(digitalRead(9) == LOW){
Joystick.setButton(1, 1);
} else {
Joystick.setButton(1, 0);
}
delay(10); // delay in between reads for stability
}
// interrupt routines
void outAChange() {
// when outA changes, outA==outB means negative direction
encoder += digitalRead(outA) == digitalRead(outB) ? -1 : 1;
}
void outBChange() {
// when outB changes, outA==outB means positive direction
encoder += digitalRead(outA) == digitalRead(outB) ? 1 : -1;
}
Comments