Hi !
I am working on the sim racing gearbox controlled by an Arduino Micro (not Pro, processor ATmega32U4).
It is mostly 6 buttons pushed by a mechanism.
Here is the thing : there is two additionnal switches for the sequential mode, linked to pin D16(MOSI) and D17(SS).
These two pins are configured as Input_Pullup, but D17 keeps floating.... Other guys from the closed beta encountered the same problem
Here is the diagram :
The code is below. It is mostly :
int downPin = 16; //for sequential
int upPin = 17;
void setup() {
Serial.begin(38400);
pinMode(downPin, INPUT_PULLUP);
pinMode(upPin, INPUT_PULLUP);
...
if (digitalRead(upPin) == LOW ) {
Serial.println("UP");
}
//Creative commons CC NC SA
// V1.0
// V1.01 The reverse gear was wrong
// V2.0 - The debug function has been rewritten in order to avoid any lag. You don't need to disable it anymore.
// - Added the Pattern Calibration function
// - Reverse gear has been fixed
// V2.1 - New code for "guide REV2"
#include "Joystick.h"
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_MULTI_AXIS, 17, 0,
true, true, false, false, false, false, false, false, true, true, false);
// Awesome Features for YOU !!! comment to disable
//#define USE_ACCEL
//#define USE_BRAKE
//#define USE_CLUTCH
//#define USE_ANALOG_HANDBRAKE
//#define USE_DIGITAL_HANDRAKE
#define DIGITALSEQUENTIAL // In this mode, upshift and downshift are buttons that need to be assigned. It should be compatible with any game
//#define SMARTSEQUENTIAL // In this mode, by example if you are in 3th, and upshift, the arduino will engage the 4th speed. It doesn't work on PC2
#define DEBUG
//#define BUTTONPLATEMOD
bool normalMount = false; //true : all the mechanism is at the front. false : all the mechanism is at the rear.
//------------------------------------------------------------------
int PositionPin[6] = {4, 5, 14, 15, A4, A5}; //set the wiring for the button plate mod.
int reverseSwitchPin = 6;
int gearForReverse = 12; //Set the gear you need to reach to be in reverse. For the moment, only 6th is mechanicaly possible
int HallSensorPinForGear[] = {7, 7, 8, 9, 10, 11, 12};
int downPin = 16; //for sequential
int upPin = 17;
int previousUpPinState = 0;
int previousDownPinState = 0;
bool seqMode = false;
bool sendDebug = false;
int unsigned long lastDebug = 0;
int DebugRefreshRate = 250;
//------------------------------------------------------------------
int gear = 0; // 0 default to neutral
unsigned long currentMillis = 0;
int lastShift = millis();
int shiftDelay = 125;
unsigned long previousMillis = 0;
int lastGear = 0;
int currentButtonState[17] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int lastButtonState[17] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int getCurrentGear() {
#if defined(DEBUG)
if (sendDebug) {
if (seqMode) {
#if defined(SMARTSEQUENTIAL)
Serial.print("SmartSequential");
#endif
#if defined(DIGITALSEQUENTIAL)
Serial.print("Sequential Mode, ");
#endif
}
else {
Serial.print("H Mode, ");
}
}
if (sendDebug) {
Serial.print("UpPin & downPin = ");
Serial.print(!digitalRead(upPin));
Serial.print(", ");
Serial.print(!digitalRead(downPin));
Serial.println(" (idle state is 0,0)");
}
#endif
if (previousUpPinState == 1 || previousDownPinState == 1) {
if (digitalRead(upPin) == LOW || digitalRead(downPin) == LOW ) {
return gear;
}
else {
previousUpPinState = 0;
previousDownPinState = 0;
}
}
for (int i = 1; i < 7 ; i++) {
if (digitalRead(HallSensorPinForGear[i]) == LOW) {
seqMode = false; //if one gear is engaged, you aren't in seqmode
}
}
if ((digitalRead(HallSensorPinForGear[1]) == HIGH && digitalRead(HallSensorPinForGear[2]) == HIGH
&& digitalRead(HallSensorPinForGear[3]) == HIGH && digitalRead(HallSensorPinForGear[4]) == HIGH
&& digitalRead(HallSensorPinForGear[5]) == HIGH && digitalRead(HallSensorPinForGear[6]) == HIGH) || (seqMode == true)) { // if no gear engaged, check if we are doing sequential
#if defined(SMARTSEQUENTIAL)
SmartSequential();
#endif
#if defined(DIGITALSEQUENTIAL)
DigitalSequential();
#endif
}
#if defined(DEBUG)
if (sendDebug) {
Serial.println("1 2 3 4 5 6");
for (int i = 1; i < 7 ; i++) {
Serial.print(!digitalRead(HallSensorPinForGear[i]));
Serial.print(" ");
}
Serial.println(" ");
}
#endif
if (seqMode == false) {
gear = 0;
} //0 is neutral
for (int i = 1; i < 7 ; i++) {
if (digitalRead(HallSensorPinForGear[i]) == LOW) {
gear = i ;
seqMode = false;
}
}
if (digitalRead(gearForReverse) == LOW && digitalRead(reverseSwitchPin) == LOW) {
gear = -1; //-1 is reverse
}
#if defined(DEBUG)
if (sendDebug) {
Serial.print("Gear is : ");
Serial.println(gear);
Serial.print("ReverseGear : ");
Serial.println(!digitalRead(reverseSwitchPin));
Serial.println("");
}
#endif
return gear;
}
void setup() {
Serial.begin(38400);
setPin(normalMount);
for (int i = 6; i < 13; i++) {
pinMode(i, INPUT_PULLUP);
}
pinMode(downPin, INPUT_PULLUP);
pinMode(upPin, INPUT_PULLUP);
#if defined(BUTTONPLATEMOD)
for (int i = 0; i < 6; i++) {
pinMode(PositionPin[i], INPUT_PULLUP);
}
#endif
int lastAcceleratorState = analogRead(AcceleratorPin);
int lastBrakeState = analogRead(BrakePin);
int lastClutchState = analogRead(ClutchPin);
int lastHandbrakeState = analogRead(HandbrakePin);
Joystick.begin();
Joystick.setAcceleratorRange(minAccelerator, maxAccelerator);
Joystick.setBrakeRange(minBrake, maxBrake);
Joystick.setXAxisRange(minClutch, maxClutch);
Joystick.setYAxisRange(minHandbrake, maxHandbrake);
delay(500); //security delay to flash the code
}
void loop() {
#if defined(DEBUG)
if (millis() - lastDebug > DebugRefreshRate) {
sendDebug = true;
lastDebug = millis();
}
#endif
getCurrentGear();
#if defined(BUTTONPLATEMOD)
buttonPlateMod();
#endif
setButtonState(gear);
sendDebug = false;
}
void setButtonState(int gear) {
if (gear == -1) {
currentButtonState[0] = 1; //gear = 0 is reverse , is associated with currentButtonState[0]
}
else {
currentButtonState[0] = 0;
}
for (int i = 1; i < 7; ++i) {
if (i == gear) {
currentButtonState[i] = 1; //0 to 6 is for gears, 7/8 is for seq,9 to 15 is for button plate 0 is reverse, 1 is 1st....6 is 6th
}
else {
currentButtonState[i] = 0;
}
}
for (int i = 0; i < 16; i++)
{
if (currentButtonState[i] != lastButtonState[i])
{ Joystick.setButton(i, currentButtonState[i]);
lastButtonState[i] = currentButtonState[i];
}
}
}
void DigitalSequential() {
currentButtonState[7] = 0;
currentButtonState[8] = 0;
currentMillis = millis();
#if defined(DEBUG)
if (sendDebug) {
Serial.println("DigitalSequential");
}
#endif
if (digitalRead(upPin) == LOW ) {
if ( currentMillis - lastShift < shiftDelay) {
return;
}
else {
lastShift = currentMillis;
}
#if defined(DEBUG)
if (sendDebug) {
Serial.println("UP");
}
#endif
currentButtonState[7] = 1;
previousUpPinState = 1;
seqMode = true;
return ;
}
if (digitalRead(downPin) == LOW) {
if ( currentMillis - lastShift < shiftDelay) {
return;
}
else {
lastShift = currentMillis;
}
#if defined(DEBUG)
if (sendDebug) {
Serial.println("DOWN");
}
#endif
previousDownPinState = 1;
seqMode = true;
currentButtonState[8] = 1;
return;
}
return;
}