Go Down

Topic: 2 axis potentiometer as a button (Read 637 times) previous topic - next topic

Figle

Hey!, I need a help for this sketch:
Code: [Select]
/*
  --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!

PaulS

You have way too much code when you are not even sure that all the hardware works. Dump ALL of the code that deals with the joystick, INCLUDING the library. Test JUST the switches. Print something that confirms that a switch is pressed or is not pressed. When that consistently prints the correct value, after the lag clears up, implement the state change detection code. Then, add the joystick stuff back in.

Post a wiring diagram - hand-drawn is fine.

The art of getting good answers lies in asking good questions.

Figle

You have way too much code when you are not even sure that all the hardware works. Dump ALL of the code that deals with the joystick, INCLUDING the library. Test JUST the switches. Print something that confirms that a switch is pressed or is not pressed. When that consistently prints the correct value, after the lag clears up, implement the state change detection code. Then, add the joystick stuff back in.

Post a wiring diagram - hand-drawn is fine.


Code: [Select]
ShiftX: 505.00
ShiftY: 469.00
ShiftBtn: 1
SeqState: 0
ShiftX: 505.00
ShiftY: 469.00
ShiftBtn: 1
SeqState: 0
ShiftX: 505.00
ShiftY: 469.00
ShiftBtn: 1
SeqState: 0
ShiftX: 505.00
ShiftY: 469.00
ShiftBtn: 1
SeqState: 0
ShiftX: 505.00
ShiftY: 469.00
ShiftBtn: 1
SeqState: 0
ShiftX: 505.00
ShiftY: 468.00
ShiftBtn: 1
SeqState: 0
ShiftX: 256.00
ShiftY: 575.00
ShiftBtn: 1
SeqState: 0
ShiftX: 314.00
ShiftY: 855.00
ShiftBtn: 1
SeqState: 0
ShiftX: 313.00
ShiftY: 853.00
ShiftBtn: 1
SeqState: 0
ShiftX: 235.00
ShiftY: 53.00
ShiftBtn: 1
SeqState: 0
ShiftX: 297.00
ShiftY: 280.00
ShiftBtn: 1
SeqState: 0
ShiftX: 523.00
ShiftY: 889.00
ShiftBtn: 1
SeqState: 0
ShiftX: 495.00
ShiftY: 393.00
ShiftBtn: 1
SeqState: 0
ShiftX: 505.00
ShiftY: 460.00
ShiftBtn: 1
SeqState: 0
ShiftX: 740.00
ShiftY: 798.00
ShiftBtn: 1
SeqState: 0
ShiftX: 728.00
ShiftY: 630.00
ShiftBtn: 1
SeqState: 0
ShiftX: 698.00
ShiftY: 59.00
ShiftBtn: 0
SeqState: 0
ShiftX: 506.00
ShiftY: 537.00
ShiftBtn: 0
SeqState: 0
ShiftX: 503.00
ShiftY: 476.00
ShiftBtn: 0
SeqState: 0
ShiftX: 506.00
ShiftY: 598.00
ShiftBtn: 0
SeqState: 0
ShiftX: 526.00
ShiftY: 457.00
ShiftBtn: 0
SeqState: 0
ShiftX: 690.00
ShiftY: 211.00
ShiftBtn: 0
SeqState: 0
ShiftX: 704.00
ShiftY: 74.00
ShiftBtn: 0
SeqState: 0
ShiftX: 496.00
ShiftY: 600.00
ShiftBtn: 0
SeqState: 0
ShiftX: 469.00
ShiftY: 324.00
ShiftBtn: 0
SeqState: 0
ShiftX: 492.00
ShiftY: 426.00
ShiftBtn: 0
SeqState: 0
ShiftX: 748.00
ShiftY: 456.00
ShiftBtn: 0
SeqState: 0
ShiftX: 721.00
ShiftY: 50.00
ShiftBtn: 0
SeqState: 0
ShiftX: 741.00
ShiftY: 80.00
ShiftBtn: 0
SeqState: 0
ShiftX: 687.00
ShiftY: 481.00
ShiftBtn: 0
SeqState: 0
ShiftX: 501.00
ShiftY: 448.00
ShiftBtn: 0
SeqState: 0
ShiftX: 501.00
ShiftY: 459.00
ShiftBtn: 0
SeqState: 0
ShiftX: 504.00
ShiftY: 896.00
ShiftBtn: 1
SeqState: 0
ShiftX: 505.00
ShiftY: 299.00
ShiftBtn: 1
SeqState: 0
ShiftX: 513.00
ShiftY: 311.00
ShiftBtn: 1
SeqState: 0
ShiftX: 750.00
ShiftY: 497.00
ShiftBtn: 1
SeqState: 0
ShiftX: 704.00
ShiftY: 841.00
ShiftBtn: 1
SeqState: 0
ShiftX: 687.00
ShiftY: 44.00
ShiftBtn: 0
SeqState: 0
ShiftX: 513.00
ShiftY: 499.00
ShiftBtn: 0
SeqState: 0
ShiftX: 508.00
ShiftY: 468.00
ShiftBtn: 0
SeqState: 0
ShiftX: 509.00
ShiftY: 468.00
ShiftBtn: 0
SeqState: 0
ShiftX: 509.00




i was using a logitech driving force shifter
The wiring diagram=
Code: [Select]
SubD9 | Arduino
         // 1     |           
         // 2     |                 
         // 3     | A2           
         // 4     | A1       
         // 5     |     
         // 6     | GND   | Ground   
         // 7     | +5V    | Vcc/5v   
         // 8     | A0     | Y-Axis     
         // 9     | Null   

Go Up