Hey!, I need a help for this sketch:
/*
--Driving Force Shifter--
This reads the input values from the 2 potentiometers and the 1 switch, to allow position data to be used as buttons.
X and Y-axis determines the position/"gear in use", and the switch is for enabling the Reverse gear position.
//--Sequential mode--//
Not yet implemented!
The shifter will also function as a sequential shifter. Press the shift shaft down while being
centered/in Neutral position, and you will activate either H-Shifter mode or Sequential mode.
Sequential mode goes along the neutral path left to right, Right is up, Left is down.
//--DF shifter pinout--//
// SubD9 | Arduino | Description | Color | J10 Pins | *
// | +5V | Vcc/5v | Yellow | |
// | A2 | Switch* | Orange | | Needs a pull-up Resistor to stabilize input
// | A1 | X-Axis | Red | |
// | A0 | Y-Axis | Brown | |
// | GND | Ground | Black | |
//
// SubD9 | Arduino | Description | Color | J11 Pins | *
// 1 | | ? | blue | 6 |
// 2 | | ? | grey | 7 |
// 3 | A2 | Switch | yellow | 5 |
// 4 | A1 | X-Axis | orange | 3 |
// 5 | | ? | white | 2 |
// 6 | GND | Ground | black | 8 |
// 7 | +5V | Vcc/5v | red | 1 |
// 8 | A0 | Y-Axis | teal | 4 |
// 9 | Null | Not used | | Null |
//--Clutch & Handbrake--//
// User dependent
// I personally used simple potentiometers from a Thrustmaster Ferrari Challenge pedals and Microsoft Wingman joystick.
// CREDITS
Arduino.cc: Tutorials and loads of info and more. http://www.arduino.cc/
MatthewH: Author of Joystick library. http://www.instructables.com/id/Arduino-LeonardoMicro-as-Game-ControllerJoystick/
jakkul: Initial X,Y,Z axis code author. http://jakkul.blogspot.se/search/label/arduino
--JawZ--: Sequential & H-Shifter code and altered X,Y,Z axis code. https://www.flickr.com/photos/142441312@N02/
*/
//--Libraries--//
#include "Joystick.h"
// Create Joystick
Joystick_ Joystick;
//--Initialization--//
int useserial = 1; // Enable/disable readout values of various analog and/or digital inputs. 0:Off, 1:On
int modeBtn = A2; // Button used to switch between gearbox modes
boolean currentState = LOW; // Storage for current button state
boolean lastState = LOW; // Storage for last button state
boolean seqState = LOW; // Storage for the current state of the Gearbox (off/on)
int shiftBtn = A2; // Button used to enable Reverse gear position
int shiftBtnD = 0; // Initialize Digital input for enabling Reverse gear
int gear = 0; // Gear nr in use
int gearBox = 0; // Gearbox in use
void setup() {
if (useserial == 1) {
Serial.begin(9600);
}
Joystick.begin();
pinMode(shiftBtn, INPUT);
digitalWrite(shiftBtn, HIGH);
pinMode(modeBtn, INPUT);
digitalWrite(modeBtn, HIGH);
}
void loop() {
/////////////////////////////////
/// Potentiometer Gearbox 6+R ///
/////////////////////////////////
// This uses two potentiometers and a switch, which the Driving Force Shifter contains.
currentState = digitalRead(modeBtn);
float shiftX = analogRead(A1); // X axis position data input
float shiftY = analogRead(A0); // Y axis position data input
shiftBtnD = digitalRead(shiftBtn); // Digital input for enabling Reverse gear
if (currentState == HIGH && lastState == LOW && shiftX > 455 && shiftX < 495 && shiftY > 460 && shiftY < 560) { // If button has just been pressed
delay(1); // Crude form of button debouncing
// Toggle the state of the gearbox mode
if (seqState == HIGH) {
gearBox = 1; // Toggle H-Shifter on
seqState = LOW;
}
else {
gearBox = 2; // Toggle Sequential on
seqState = HIGH;
}
}
// Split X and Y axis into 6 positions, then have a switch activate the 7th position. To make up a 6+R shifter
if (gearBox == 1) {
for (gear = 0; gear < 30; gear++) {
if (shiftY > 850) { // Gear upper position - 1,3,5
if (shiftX < 300) Joystick.pressButton(0); // Activate gear 1
else if (shiftX > 380 && shiftX < 530) Joystick.pressButton(2); // Activate gear 3
else if (shiftX > 650 && shiftX < 730) Joystick.pressButton(4); // Activate gear 5
}
else if (shiftY < 200) { // Gear lower position - 2,4,6,R
if (shiftX < 300) Joystick.pressButton(1); // Activate gear 2
else if (shiftX > 380 && shiftX < 530) Joystick.pressButton(3); // Activate gear 4
else if (shiftBtnD == HIGH && shiftX > 640 && shiftX < 740) Joystick.pressButton(5); // Activate gear 6
else if (shiftBtnD == LOW && shiftX > 640 && shiftX < 740) Joystick.pressButton(6); // Activate Reverse
}
else Joystick.releaseButton(gear); // Release any button that have been pressed
}
}
// Split X axis into 2 positions, to make up a sequential shifter
else if (gearBox == 2) {
for (gear = 0; gear < 30; gear++) {
if (shiftX < 400) {
Joystick.pressButton(8); // Shift DOWN
}
else if (shiftX > 545) {
Joystick.pressButton(9); // Shift UP
}
}
}
lastState = currentState;
///////////////////
/// Diagnostics ///
///////////////////
if (useserial == 1) {
// Print out Shifter input values
Serial.print ("ShiftX: "); // prints text
Serial.println (shiftX); // prints current byte value
Serial.print ("\t\tShiftY: ");
Serial.println (shiftY);
Serial.print ("\t\t\t\tShiftBtn: ");
Serial.println (shiftBtnD);
Serial.print ("\t\t\t\t\t\tSeqState: ");
Serial.println (seqState);
delay(30); // delay in between reads for stability
}
}
So the arduino are reading the 2 potentiometer and 1 button correctly but it wont register any button,i already changed the axis value to match my pot .The arduino i was using was leonardo
Thanks a lot!