Teensy Joystick HID and encoder assign to buttons

Hi, sorry here is the code I use now:

Code:
#include <Bounce.h>
#include <Encoder.h>

/* declare 'gamepad' around here please :) */


Bounce button2 = Bounce(1, 10);
Bounce button3 = Bounce(2, 10);


long oldPosition = 0;
Encoder myEnc(0, 1);

int8_t dir=0,odir=0;
uint8_t isChanged=0;

void setup() {
	pinMode(1, INPUT_PULLUP);
	pinMode(2, INPUT_PULLUP);
	//Serial.begin(Serial.baud());
}

void loop() {
	button2.update();
	button3.update();
	long newPosition = myEnc.read()/2;


           dir=newPosition-oldPosition;
	   oldPosition=newPosition;

       	if(dir>0) {
		Gamepad.button(2, 1);
		isChanged=1;
	} else if(dir<0) {
		Gamepad.button(3, 1);
		isChanged=1;
                  
	} else if(odir>0) {
		Gamepad.button(2, 0);
                isChanged=1;
        } else if(odir<0) {
		Gamepad.button(3, 0);
		isChanged=1;
	
        
       }
	odir=dir;
	
	if (button2.fallingEdge()) {
		Gamepad.button(2, 1);
		isChanged=1;
	}

	if (button3.fallingEdge()) {
		Gamepad.button(3, 1);
		isChanged=1;
	}

	if (button2.risingEdge()) {
		Gamepad.button(2, 0);
		isChanged=1;
	}

	if (button3.risingEdge()) {
		Gamepad.button(3, 0);
		isChanged=1;
	}

	if(isChanged) {
		Gamepad.send_now();
		isChanged=0;
	}
}
 
sorry, I actually should have guessed it was my last batch with that /2 change added.

Do I guess correctly that the way you are counting outputs is by what the game pad button does using something on a PC/Game/config ?

Just before I make much changes to the code, please tell me if you just want it to continuously press the button while you are rolling the shaft or if you want it to be like a tap on the button per detent (click) on the shaft practically no matter how quickly it is turned?
 
The "gamepad" you can also change into "Joystick.button" I just use a different option instead of the standard serial mouse/keyboard/joystick..

you guess correct, every click should do a count up or down.

should be tap on ( the button should go on/off ) per detent no matter how quick it is turned.

Hope this information helps again :)

Thank you so much

rgds
Frans
 
Try this;
Code:
#include <Bounce.h>
#include <Encoder.h>

/* declare 'gamepad' around here please :) */


Bounce button2 = Bounce(1, 10);
Bounce button3 = Bounce(2, 10);


long oldPosition = 0;
Encoder myEnc(0, 1);

int8_t dir=0,odir=0;
uint8_t isChanged=0;

elapsedMillis timing;

void setup() {
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  //Serial.begin(Serial.baud());
}

void loop() {
  button2.update();
  button3.update();
  
  if(timing>24) { // may need to make this bigger, depends polling interval on host
    long newPosition = myEnc.read()/2;
    if(odir) {
      if(odir>0) {
        Gamepad.button(2, 0);
        isChanged=1;
      } else if(odir<0) {
        Gamepad.button(3, 0);
        isChanged=1;
      }
      odir=0;
    } else {
      dir=newPosition-oldPosition;
      if(dir>0) {
        oldPosition++;
        Gamepad.button(2, 1);
        isChanged=1;
      } else if(dir<0) {
        oldPosition--;
        Gamepad.button(3, 1);
        isChanged=1;
      } 
      odir=dir;
    }
    timing=0;
  }
  if (button2.fallingEdge()) {
    Gamepad.button(2, 1);
    isChanged=1;
  }
  if (button3.fallingEdge()) {
    Gamepad.button(3, 1);
    isChanged=1;
  }
  if (button2.risingEdge()) {
    Gamepad.button(2, 0);
    isChanged=1;
  }
  if (button3.risingEdge()) {
    Gamepad.button(3, 0);
    isChanged=1;
  }
  if(isChanged) {
    Gamepad.send_now();
    isChanged=0;
  }
}
 
I'm new to this entirely, and am trying to achieve the same goal. Unfortunately I've read through this thread multiple times and still don't understand what "declare 'gamepad' around here please :)" means or how to do it. No clue.

Is the code pasted above complete? Or do I need to add to it to get it to compile without error?

I'm using a teensy 3.1. USB type set to: Serial + Keyboard + Mouse + Joystick. And I'm obviously getting the 'Gamepad' was not declared in this scope error. :)
 
Ok

I did figure out what was going on in my previous post. If I use the code posted by robsoles above (with all references to gamepad changed to joystick), It almost works properly.

As I rotate the encoder one detent it will press button 2 and hold it, rotate another detent and it will then press button 3 and hold it as well, turn another and button 2 gets released, turn again then button 3 is released, etc....

I am trying to achieve the same as Frans M, I believe. I would like to rotate the encoder clockwise and have it out put a button press (the same button press) and release for each detent. And rotating counter clockwise would do the same for a different button. Ideally I'd like to be able to edit the length of the button press as well.

Hope that makes sense.

This ability would be very valuable in simulators.

Thanks for any help.
 
I also tried this but with no luck. It does the same as Bodido's, which makes it unusable as a controller where the two buttons should serve opposite functions. Either that or I am doing something wrong
 
In order that the loop does not continue add it to joystick serialmode. This will help in particular movement according to the requirement.




Latest games here .
 
Last edited:
To those that posted since my last post in this thread my apologies it took a few of you to get me to review my crap.

I'm not sure I ever compiled my stuff there, before posting it, maybe I should apologise for that too.

My employer still hasn't shoved some rotary encoders into my hand tho they keep threatening to, maybe they should apologise about that.


Parsing the code fairly carefully today I realise that the button and encoder objects appear to be sharing pin 1 in the last version I posted as they probably did from the first version @Frans posted me. I assume you managed to fix it enough to make it work for you or you would have posted back(?) - confirmation or denial that it was any good would have been good feedback, I just can't help mentioning ;)

@all
Here is a version I have actually gone to the trouble of compiling but haven't really tried coz lacking encoder. I would very much appreciate feedback.
Code:
#include <Bounce.h>
#include <Encoder.h>

/* Just select a USB type which includes Joystick */

// Change this appropriately if you need to
#define ENCODER_VALUES_PER_CLICK 2

// Set this to a non zero and open the terminal to see valuable info.
#define AM_DEBUGGING_GOTTA_SEE_VALUES 0

// connect rotary encoder as sensibly as possible to following pins 
#define ENC_PIN_1 0
#define ENC_PIN_2 1

// connect buttons as sensibly as possible to pins;
#define BUT_PIN_1 2
#define BUT_PIN_2 3

Bounce button2 = Bounce(BUT_PIN_1, 10);
Bounce button3 = Bounce(BUT_PIN_2, 10);

long oldPosition = 0;
Encoder myEnc(ENC_PIN_1, ENC_PIN_2);

int8_t dir=0,odir=0;
uint8_t isChanged=0;

elapsedMillis timing;

void setup() {
  pinMode(BUT_PIN_1, INPUT); // prefer external pull-ups.
  pinMode(BUT_PIN_2, INPUT);
#if AM_DEBUGGING_GOTTA_SEE_VALUES
  Serial.begin(Serial.baud());
#endif
}

void loop() {
  button2.update();
  button3.update();
  
  if(timing>24) { // if not distinctive button press per click try larger number here.
    long newPosition = myEnc.read()/ENCODER_VALUES_PER_CLICK;
    if(odir) {
      if(odir>0) {
        Joystick.button(2, 0);
        isChanged=1;
#if AM_DEBUGGING_GOTTA_SEE_VALUES
        Serial.printf("Button 2 Up, newPosition=%L & oldPosition=%L\n",newPosition,oldPosition);
#endif
      } else if(odir<0) {
        Joystick.button(3, 0);
        isChanged=1;
#if AM_DEBUGGING_GOTTA_SEE_VALUES
        Serial.printf("Button 3 Up, newPosition=%L & oldPosition=%L\n",newPosition,oldPosition);
#endif
      }
      odir=0;
    } else {
      dir=newPosition-oldPosition;
      if(dir>0) {
        Joystick.button(2, 1);
        isChanged=1;
#if AM_DEBUGGING_GOTTA_SEE_VALUES
        Serial.printf("Button 2 Down, newPosition=%L & oldPosition=%L\n",newPosition,oldPosition);
#endif
        oldPosition++;
      } else if(dir<0) {
        Joystick.button(3, 1);
        isChanged=1;
#if AM_DEBUGGING_GOTTA_SEE_VALUES
        Serial.printf("Button 3 Down, newPosition=%L & oldPosition=%L\n",newPosition,oldPosition);
#endif
        oldPosition--;
      } 
      odir=dir;
    }
    timing=0;
  }
  if (button2.fallingEdge()) {
    Joystick.button(2, 1);
    isChanged=1;
#if AM_DEBUGGING_GOTTA_SEE_VALUES
    Serial.println("Button 2 Down, button2.fallingEdge");
#endif
  }
  if (button3.fallingEdge()) {
    Joystick.button(3, 1);
    isChanged=1;
#if AM_DEBUGGING_GOTTA_SEE_VALUES
    Serial.println("Button 3 Down, button3.fallingEdge");
#endif
  }
  if (button2.risingEdge()) {
    Joystick.button(2, 0);
    isChanged=1;
#if AM_DEBUGGING_GOTTA_SEE_VALUES
    Serial.println("Button 2 Up, button2.risingEdge");
#endif
  }
  if (button3.risingEdge()) {
    Joystick.button(3, 0);
    isChanged=1;
#if AM_DEBUGGING_GOTTA_SEE_VALUES
    Serial.println("Button 2 Down, button3.risingEdge");
#endif
  }
  if(isChanged) {
    Joystick.send_now();
    isChanged=0;
  }
}
 
I would very much appreciate feedback.

This is very very late feedback so please excuse me as I have only just found this thread.

I am also trying to get a joystick device that will allow me to send a joystick button with the turn of an encoder in each direction. I am not a programmer by any means although I have used many Teensy++2.0 development boards. I use GenericHID to program them (drag and drop) but the encoder feature does not allow for button presses only axis.

The picture shows my arduino setting, I also changed the settings slightly by "ENCODER_VALUES_PER_CLICK 2" otherwise used your sample code exactly.

In windows 10 joystick properties the lights for buttons 2 and 3 flicker on and off seemly random.

I also tried adding 1k resistors to each leg of the encoder, no difference.

Serial Monitor output:


Button 2 Down, button2.fallingEdge
Button 2 Down, newPosition=Button 2 Up, button2.risingEdge
Button 2 Up, newPosition=Button 2 Down, newPosition=Button 2 Up, newPosition=Button 2 Down, button2.fallingEdge
Button 2 Up, button2.risingEdge
Button 3 Down, newPosition=Button 3 Up, newPosition=Button 3 Down, newPosition=Button 3 Up, newPosition=Button 2 Down, button2.fallingEdge
Button 2 Up, button2.risingEdge
Button 2 Down, newPosition=Button 2 Up, newPosition=Button 2 Down, newPosition=Button 2 Up, newPosition=Button 2 Down, button2.fallingEdge
Button 3 Down, newPosition=Button 2 Up, button2.risingEdge
Button 3 Up, newPosition=Button 3 Down, newPosition=Button 3 Up, newPosition=Button 2 Down, button2.fallingEdge
Button 2 Down, newPosition=Button 2 Up, newPosition=Button 2 Up, button2.risingEdge
Button 2 Down, newPosition=Button 2 Up, newPosition=Button 2 Down, button2.fallingEdge
Button 3 Down, newPosition=Button 3 Up, newPosition=Button 3 Down, newPosition=Button 2 Up, button2.risingEdge
Button 3 Up, newPosition=Button 2 Down, button2.fallingEdge
Button 3 Down, newPosition=Button 3 Up, newPosition=Button 2 Up, button2.risingEdge
Button 3 Down, newPosition=Button 3 Up, newPosition=Button 2 Down, button2.fallingEdge
Button 2 Up, button2.risingEdge
Button 2 Down, newPosition=Button 2 Up, newPosition=Button 2 Down, newPosition=Button 2 Up, newPosition=Button 2 Down, button2.fallingEdge
Button 2 Up, button2.risingEdge
Button 2 Down, button2.fallingEdge
Button 2 Down, newPosition=Button 2 Up, newPosition=Button 2 Down, newPosition=Button 2 Up, button2.risingEdge
Button 2 Up, newPosition=Button 2 Down, button2.fallingEdge
Button 2 Up, button2.risingEdge
Button 2 Down, button2.fallingEdge
Button 2 Up, button2.risingEdge
Button 2 Down, button2.fallingEdge
Button 2 Up, button2.risingEdge
Button 2 Down, button2.fallingEdge
 

Attachments

  • teensydino example.jpg
    teensydino example.jpg
    98.4 KB · Views: 135
This is very very late feedback so please excuse me as I have only just found this thread.

This is really late feedback, but I had almost the same result with my experimentation with this code on a teensy 2.0++ board.

I am really new to all this and have been using borrowed code from help on these forums to get flight controller boxes for flight sims going.

I am currently experimenting with using multiple rotary encoders for a flight controller, with the intention of turning the encoder left and right to adjust trim on the plane up and down, and the button press as the reset. I just want each direction to detect as a button press.

Did you succeed in your attempts to make your project work?
 
I may as well follow the trend I probably set way back then.

This is very very late feedback so please excuse me as I have only just found this thread.

I am also trying to get a joystick device that will allow me to send a joystick button with the turn of an encoder in each direction. I am not a programmer by any means although I have used many Teensy++2.0 development boards. I use GenericHID to program them (drag and drop) but the encoder feature does not allow for button presses only axis.

The picture shows my arduino setting, I also changed the settings slightly by "ENCODER_VALUES_PER_CLICK 2" otherwise used your sample code exactly.

In windows 10 joystick properties the lights for buttons 2 and 3 flicker on and off seemly random.

I also tried adding 1k resistors to each leg of the encoder, no difference.

Serial Monitor output:


Button 2 Down, button2.fallingEdge
Button 2 Down, newPosition=Button 2 Up, button2.risingEdge
Button 2 Up, newPosition=Button 2 Down, newPosition=Button 2 Up, newPosition=Button 2 Down, button2.fallingEdge
Button 2 Up, button2.risingEdge
Button 3 Down, newPosition=Button 3 Up, newPosition=Button 3 Down, newPosition=Button 3 Up, newPosition=Button 2 Down, button2.fallingEdge
Button 2 Up, button2.risingEdge
Button 2 Down, newPosition=Button 2 Up, newPosition=Button 2 Down, newPosition=Button 2 Up, newPosition=Button 2 Down, button2.fallingEdge
Button 3 Down, newPosition=Button 2 Up, button2.risingEdge
Button 3 Up, newPosition=Button 3 Down, newPosition=Button 3 Up, newPosition=Button 2 Down, button2.fallingEdge
Button 2 Down, newPosition=Button 2 Up, newPosition=Button 2 Up, button2.risingEdge
Button 2 Down, newPosition=Button 2 Up, newPosition=Button 2 Down, button2.fallingEdge
Button 3 Down, newPosition=Button 3 Up, newPosition=Button 3 Down, newPosition=Button 2 Up, button2.risingEdge
Button 3 Up, newPosition=Button 2 Down, button2.fallingEdge
Button 3 Down, newPosition=Button 3 Up, newPosition=Button 2 Up, button2.risingEdge
Button 3 Down, newPosition=Button 3 Up, newPosition=Button 2 Down, button2.fallingEdge
Button 2 Up, button2.risingEdge
Button 2 Down, newPosition=Button 2 Up, newPosition=Button 2 Down, newPosition=Button 2 Up, newPosition=Button 2 Down, button2.fallingEdge
Button 2 Up, button2.risingEdge
Button 2 Down, button2.fallingEdge
Button 2 Down, newPosition=Button 2 Up, newPosition=Button 2 Down, newPosition=Button 2 Up, button2.risingEdge
Button 2 Up, newPosition=Button 2 Down, button2.fallingEdge
Button 2 Up, button2.risingEdge
Button 2 Down, button2.fallingEdge
Button 2 Up, button2.risingEdge
Button 2 Down, button2.fallingEdge
Button 2 Up, button2.risingEdge
Button 2 Down, button2.fallingEdge

Your serial monitor output suggests you rolled the encoder about four clicks each way up to a couple of times and then ended by rolling the encoder in a single direction several times in a row.

Did any of you guys solve this? Because I can fix the relatively simple mistakes in my last batch and make it give you a single button tap 'in the direction you are turning the encoder' 'per click' - the first thing I will do to it is strip it of all unnecessary elements, and make the fact that all we need do is 'follow' the value returned by 'myEnc.Read();' with a tracker and manipulate the joystick object with slightly more sensible timing than I did last time :p

{ P.s. I'm sorta back - did you miss me? :D }
 
Back
Top