Zurück   (C) Christof Ermer, Regensburg
Gästebuch (public. sonst besser Email)


Gratis Counter 26.08.2014

G:\WWW\Joystick
Analoger Joytick
http://www.arduino.cc/en/Tutorial/JoyStick








http://dduino.blogspot.de/2011/11/analog-joystick-with-arduino.html
Analog Joystick with Arduino!

I have a old Gravis Blackhawk joystick that i dug up to use with my Arduino. I opened up the back, and it doesn't have any electronics, just a circuit board splitting out the DB15 connector to all the buttons etc(no ICs etc). It has 3 buttons on the stick, a throttle pot and another button on the base, and of course the X + Y axis slider pots for the stick. All the buttons are each wired to a pin of the DB15 connector and the other side goes to GND. The X + Y axis and the slider are connected to +5v, GND and a pin for each on the DB15.

The Arduino programming was really simple. All i had to do was read 3 analog ports for the X,Y and throttle pots, and 4 Digital pins for the buttons. Then I put them all into a string like: "X,Y,SpeedPot,btn1,btn2,btn3,btn4" and printed to serial. Also, this Arduinio code uses millis() rather than delay to be more precise in delays.


/-----Axis-------------------------------------------------------//
#define Xaxis 3  //Joystick X axis Analog pin 3
#define Yaxis 4  //Joystick Y axis Analog pin 4
#define SpeedPot 5  //Pot in base for speed, Analog pin 5

int XaxisVar = 0;  //...analog pin 3---X axis
int YaxisVar = 0;  //...analog pin 4---Y axis
int SpeedVar = 0;  //...analog pin 5---Speed pot
//----------------------------------------------------------------//

//-----Buttons----------------------------------------------------//
#define TrigBtn 2  //trigger button, digital 2
#define MidBtn 6  //Button below trigger on joystick, digital 3
#define LowBtn 4  //Lowest button on the joystick, digital 4
#define BaseBtn 5  //button on base of joystick, digital 5

int TrigBtnState = 0;  //Stores the state of TrigBtn
int MidBtnState = 0;  //...MidBtnState
int LowBtnState = 0;  //...LowBtnState
int BaseBtnState = 0;  //...BaseBtnState
//----------------------------------------------------------------//
int HypDelayTime = 100;  //time you want the code to loop at. will be used to figure actual delay time(see end of code).

void setup()
{
  Serial.begin(9600);

  for (int pin=2; pin<=6; ++pin)  //sets all digital pin 2-5 as input and enables internal pullups.
  {                         
    pinMode(pin, INPUT);  //sets button pins to input
    digitalWrite(pin, HIGH);  // sets the pull up.
  }
}

void loop()
{
  int TimeInt = millis();  //record time at begining of main loop

  XaxisVar = analogRead(Xaxis);  //Read X-Axis postion
  YaxisVar = analogRead(Yaxis);  //Read Y-Axis postion
  SpeedVar = analogRead(SpeedPot);  //Read Speed Pot postion

  TrigBtnState = digitalRead(TrigBtn);  //read status of buttons on joystick
  MidBtnState = digitalRead(MidBtn);  //...
  LowBtnState = digitalRead(LowBtn);  //...
  BaseBtnState = digitalRead(BaseBtn);  //...

  //Just serial ouput formatting...
  String Comma = String(",");
  String Data = String(XaxisVar + Comma + YaxisVar + Comma + SpeedVar + TrigBtnState + Comma + MidBtnState + Comma + LowBtnState + Comma + BaseBtnState);
  Serial.println(Data);

  int TimeEnd = millis();  //record time at the end of main loop

  //the DelayTime is the time so that the code actually repeats at 1000ms.
  //"Time we actualy want to delay"="EndOfCodeTime"-"BeginingOfCodeTime"="time taken for code to execute"-"hypothetical delay"(what we wanted to delay(1000ms))
  int DelayTime = HypDelayTime - (TimeEnd-TimeInt);
  delay(DelayTime);
  //Serial.println(DelayTime); //prints delay time. is about ~980ms, so we can tell that all the signal reading and formating takes 20ms.
}