Analog code

Status
Not open for further replies.
been looking for examples of analog input codes, nothing that seems to do what I want. Looking for analog code that can do a 'w' 'a' 's' 'd' code for mmo/minecraft/etc, just keyboard commands using analog input. and been at it for an hour hunting, nothing is showing. and to make another set of code to make the second joy be a right_mouse_click + each movement of the mouse accordingly. any ideas where to look?
 
Can you post a link to the tech specs on specific joystick part(s) you're using?

Much better advice is possible with such details....
 
http://www.ebay.com/itm/400614903745?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1439.l2649
this is the exact one I bought (technically have two, lol) and again, any help would be appreciated, thanks paul.

and here is the code I am using so far.

Code:
const int pinZero = 0;
const int pinOne = 1;
const int pinTwo = 2;
const int pinThree = 3;



int buttonState = 0;

void setup () {
  pinMode (pinZero, INPUT_PULLUP);
  pinMode (pinOne, INPUT_PULLUP);
  pinMode (pinTwo, INPUT_PULLUP);
  pinMode (pinThree, INPUT_PULLUP);
  
  Serial.begin(9600);
  Mouse.begin();
  Keyboard.begin();
}

void loop () {
  
  buttonState = digitalRead(pinZero);
  
  if (digitalRead(pinZero) == LOW) {
    Keyboard.write('1');
    delay(300);
  }
  else {
    digitalWrite(pinOne, HIGH);
  }
  buttonState = digitalRead(pinOne);
  
  if (digitalRead(pinOne) == LOW) {
    Keyboard.write('2');
    delay(300);
  }
  else {
    digitalWrite(pinOne, HIGH);
  }
}
Cross posted: http://forum.arduino.cc/index.php?topic=211673.msg1562105#msg1562105
 
Last edited:
If I understand correctly, what you really want is a sort of thresholding system. Think of those joysticks as essentially two potentiometers, one linked to up/down, and the other linked to left/right. When the joystick is centered (it's normal state, if it's spring-loaded), the value you'll read on analog pins for those potentiometers will both be AROUND 512. If you move the Y axis straight up, and push it to the very top, that axis' pin will read 1024 (or 0, if it's upside-down).

I'm not sure what your posted code is doing, as you probably won't get meaningful information from this joystick on digital pins. JRx and JRy should be attached to analog inputs, and read with analogRead().

For a ROUGH working version of this, you'd do something like this:

Code:
#define joy1X A1
#define joy1Y A2
#define joy1sw 3

void setup(){
   pinMode(joy1X, INPUT);
   pinMode(joy1Y, INPUT);
   pinMode(joy1sw, INPUT_PULLUP)
   Keyboard.begin();
}

uint16_t current_x = 0;
uint16_t current_y = 0;

void loop(){
   current_x = analogRead(joy1X);
   current_y = analogRead(joy1Y);
   
   if (current_X > 768 && ((current_y > 256) && (current_y < 768)){
      Keyboard.write('1');
   }
   //etc
   
}

I've only done one state of the joystick here, and I leave it up to you to tweak it (I've never used the Keyboard mode on my teensy so I'm not sure about any bugs with the Keyboard.write('1'), which looks a little funky to me). Basically, if the X axis is greater than 768 (about 3/4ths to the right), AND the Y axis is within the middle half or so of the joystick, it'll trigger that keyboard.write. Continually. If you need it to only trigger once, you'd probably want to set a flag when that if() statements completes to true, and unset that flag (use a boolean) when it's false. Then just check if the state of that boolean has changed, and trigger the button.
 
Here's another simple example that uses 1 joystick for mouse movement and another joystick for wasd.

I just tested it with a Teensy++ 2.0 in Sniper Elite V2 and worked ok., it should work for Teesny 3 as well just change the ledpin to 13. To use it you just need change joy1X, joy1Y, joy2X & joy2Y to match the analog input pins that your joysticks are connected to. Set your USB Type: to Keyboard + Mouse + Joystick and upload.

Code:
// joystick input pins
int joy1X = 0;
int joy1Y = 1;
int joy2X = 2;
int joy2Y = 3;
// joystick 1 x & y values
int joy1X_value;
int joy1Y_value;
// joystick 1 movement deltas
int deltaX;
int deltaY;
// joystick 2 x & y values
int joy2X_value;
int joy2Y_value;
// deadzone value for joystick 2
int deadzone = 50;
// for led
int ledPin = 6;
boolean ledOn = false;

void setup()
{
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin,LOW);
}

void loop()
{
  // joystick 1 - mouse
  
  joy1X_value = analogRead(joy1X);
  joy1Y_value = analogRead(joy1Y);
  // convert x & y into values from -20 - 20
  deltaX = (-511 + (joy1X_value)) /25;
  deltaY = (-511 + (joy1Y_value)) /25;
  // move the mouse by the delta values
  Mouse.move(deltaX,deltaY);
  
  // joystick 2 keyboard
  
  // convert to -511 - 512
  joy2X_value = -511 + analogRead(joy2X);
  joy2Y_value = -511 + analogRead(joy2Y);
  // if x movement greater than deadzone
  if (joy2X_value > deadzone)
  {
    // move right = key D down
    Keyboard.press('d');
  }
  // if x movement less than negative deadzone
  else if (joy2X_value < -deadzone)
  {
    // move left = key A down
    Keyboard.press('a');
  }
  else
  {
    // in the deadzone range so both keys up
    Keyboard.release('a');
    Keyboard.release('d');
  }
  // if y movement greater than deadzone
  if (joy2Y_value > deadzone)
  {
    // move down = key S down
    Keyboard.press('s');
  }
  // if x movement less than negative deadzone
  else if (joy2Y_value < -deadzone)
  {
    // move up = key W down
    Keyboard.press('w'); 
  }
  else
  {
    // in the deadzone range so both keys up
    Keyboard.release('s');
    Keyboard.release('w');
  }
  // short delay so code runs at 40 times per second
  delay(25);
  
  // toggle the led
  ledOn = !ledOn;
  digitalWrite(ledPin, ledOn);
}

Regards,

Les
 
Last edited:
Here's another simple example that uses 1 joystick for mouse movement and another joystick for wasd.

I just tested it with a Teensy++ 2.0 in Sniper Elite V2 and worked ok., it should work for Teesny 3 as well just change the ledpin to 13. To use it you just need change joy1X, joy1Y, joy2X & joy2Y to match the analog input pins that your joysticks are connected to. Set you USB Type: to Keyboard + Mouse + Joystick and upload.

Code:
// joystick input pins
int joy1X = 0;
int joy1Y = 1;
int joy2X = 2;
int joy2Y = 3;
// joystick 1 x & y values
int joy1X_value;
int joy1Y_value;
// joystick 1 movement deltas
int deltaX;
int deltaY;
// joystick 2 x & y values
int joy2X_value;
int joy2Y_value;
// deadzone value for joystick 2
int deadzone = 50;
// for led
int ledPin = 6;
boolean ledOn = false;

void setup()
{
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin,LOW);
}

void loop()
{
  // joystick 1 - mouse
  
  joy1X_value = analogRead(joy1X);
  joy1Y_value = analogRead(joy1Y);
  // convert x & y into values from -20 - 20
  deltaX = (-511 + (joy1X_value)) /25;
  deltaY = (-511 + (joy1Y_value)) /25;
  // move the mouse by the delta values
  Mouse.move(deltaX,deltaY);
  
  // joystick 2 keyboard
  
  // convert to -511 - 512
  joy2X_value = -511 + analogRead(joy2X);
  joy2Y_value = -511 + analogRead(joy2Y);
  // if x movement greater than deadzone
  if (joy2X_value > deadzone)
  {
    // move right = key D down
    Keyboard.press('d');
  }
  // if x movement less than negative deadzone
  else if (joy2X_value < -deadzone)
  {
    // move left = key A down
    Keyboard.press('a');
  }
  else
  {
    // in the deadzone range so both keys up
    Keyboard.release('a');
    Keyboard.release('d');
  }
  // if y movement greater than deadzone
  if (joy2Y_value > deadzone)
  {
    // move down = key S down
    Keyboard.press('s');
  }
  // if x movement less than negative deadzone
  else if (joy2Y_value < -deadzone)
  {
    // move up = key W down
    Keyboard.press('w'); 
  }
  else
  {
    // in the deadzone range so both keys up
    Keyboard.release('s');
    Keyboard.release('w');
  }
  // short delay so code runs at 40 times per second
  delay(25);
  
  // toggle the led
  ledOn = !ledOn;
  digitalWrite(ledPin, ledOn);
}

Regards,

Les


Les, you are a genius :D I will test this later once home from work, but that is alot of code, what about additonal code to add mouse_right_click to the movement of the mouse stick? Curious I am for this, or at least a button command to toggle right click (right click and mouse movement = pov movement). thanks again guys for the additional help
 
The code was just meant to get you started, and should be easy to add to. Use the examples for keyboard, mouse and joystick to help you.

So for right mouse button down add

Code:
Mouse.set_buttons(0, 0, 1);

and for right mouse button up add

Code:
Mouse.set_buttons(0, 0, 0);

Same goes for a button to key press, the simplest way is something like this...

First add the following to the setup function, changing 0 to match your digital pin.

Code:
pinMode(0,INPUT_PULLUP);

Then before the delay(25) add the following...

Code:
if (!digitalRead(0))
    Keyboard.press('e');
else
    Keyboard.release('e');

Because the digital pin is normally high (1), thanks to the internal pullup, it goes low (0) when the button is pressed. This is why we use the !(NOT) operator on the digitalRead, so we are basically saying, if the input pin is low press the key, else release it. Add as many of these as you need, adjusting the pin number and key to suit.

The whole of this code will need tweaking for your requirements, the mouse code above for example, allowed reasonable control of the cursor in windows, but was way to slow in game for me. It could probably do with a logarithmic scale, but maths is not my strong point.

Regards,

Les
 
Thank pointy, used a lot of the code, been awaiting a measurement tool from ebay, so that I can start measurements, designing of the case, and finalizing the code. Custom building the case(s) out of lexan. Hopefully this will go over well and will have pictures once done. EST completed ~end of march.
 
Thank pointy, used a lot of the code, been awaiting a measurement tool from ebay, so that I can start measurements, designing of the case, and finalizing the code. Custom building the case(s) out of lexan. Hopefully this will go over well and will have pictures once done. EST completed ~end of march.

Glad you found the code useful and it's good you are getting somewhere, I am looking forward to seeing the pictures of the final product.

I have been really struggling with my projects of late, and I am sure my CNC machine will be the death of me. Anyway I'm off for few days fishing, to take my mind off it.

Regards,

Les
 
Status
Not open for further replies.
Back
Top