Register / Login  |  Desktop view  |  Jump to bottom of page

IoAbstraction & TaskManagerIO » How to setup two AnalogJoystickEncoders?

Author: ByteC
19/01/2020 23:37:51
Hi there,

I'm fairly new to this library, browsing through your examples.
I'm using an Adafruit Thumbstick (https://www.adafruit.com/product/512) which supports two axes. I've wired that part to A8 and A9 on an Arduino Due.

My nonworking sketch so far: (btw, there some more parts assigned to that due).
I assume that I have to add another encoder somwhow, instead of another ArduinoAnalogDevice abstraction... hmmm smilie

Cheers,

Peter

/******************************************************************************************
 *** TRN V1.0 - Arduino Due                                                             *** 
 ******************************************************************************************
 * Kommunikation mit Mega via Serial3, 115K                                               *
 * Kommunikation mit PC via Serial, 115K                                                  * 
 * ****************************************************************************************
 * Shields:                                                                               *
 *   GShield V5b                                                                          *
 *                                                                                        *
 * Hardware:                                                                              *  
 *   1X Analog Joystick, 2x Analog, 1x Digital Switch [A08, A09][DI29]                    *
 *   3X Taster, [DI30, DI31, Di32]                                                        *
 *   1X Keyboard 3x4, [DI22-DI28]                                                         *
 *   4X Endschalter, 2 davon benutzt, [DI34-DI37]                                         *
 *   1X Z-Achse, StepPin 4, DirPin 7, in Verwendung                                       *   
 *   1X Y-Achse, StepPin 3, DirPin 6, nicht in Verwendung                                 *
 *   1X X-Achse, StepPin 2, DirPin 5, nicht in Verwendung                                 *
 *   Stepper DisablePin = 8                                                               *
 *****************************************************************************************/
#include <IoAbstraction.h>
#include <KeyboardManager.h>
#include <JoystickSwitchInput.h>

#include <AccelStepper.h>
#include <MultiStepper.h>

///////////////////////////////////////////////////////////
// Definitionen Joystick                                 //
///////////////////////////////////////////////////////////
#define JOYSTICK_X A8
#define JOYSTICK_Y A9

ArduinoAnalogDevice analogDevice1;
ArduinoAnalogDevice analogDevice2;
IoAbstractionRef arduinoPins = ioUsingArduino();

// JoyX Event 
void onEncoderJoyXChange(int newXValue) {
    Serial.print("New joystick X value: ");
    Serial.print(newXValue);
    Serial.print(", analog in ");
    Serial.println(analogDevice1.getCurrentValue(JOYSTICK_X));
}
// JoyY Event 
void onEncoderJoyYChange(int newYValue) {
    Serial.print("New joystick Y value: ");
    Serial.print(newYValue);
    Serial.print(", analog in ");
    Serial.println(analogDevice2.getCurrentValue(JOYSTICK_Y));
}

///////////////////////////////////////////////////////////
// Definitionen Keyboard                                 //
///////////////////////////////////////////////////////////
MAKE_KEYBOARD_LAYOUT_3X4(keyLayout)
MatrixKeyboardManager keyboard;
IoAbstractionRef arduinoIo = ioUsingArduino();
class MyKeyboardListener : public KeyboardListener {
public:
    void keyPressed(char key, bool held) override {
        Serial.print("Key ");
        Serial.print(key);
        Serial.print(" is pressed, held = ");
        Serial.println(held);
    }

    void keyReleased(char key) override {
        Serial.print("Released ");
        Serial.println(key);
    }
} keyListener;

///////////////////////////////////////////////////////////
// Setup - RunOnce                                       //
///////////////////////////////////////////////////////////

void setup() {
  while(!Serial);
  Serial.begin(115200);

  // Keyboard
  keyLayout.setRowPin(0, 25);
  keyLayout.setRowPin(1, 26);
  keyLayout.setRowPin(2, 27);
  keyLayout.setRowPin(3, 28);
  keyLayout.setColPin(0, 22);
  keyLayout.setColPin(1, 23);
  keyLayout.setColPin(2, 24);
  keyboard.initialise(arduinoIo, &keyLayout, &keyListener);
  keyboard.setRepeatKeyMillis(850, 350);
  
  // Joystick X/Y
  switches.initialise(arduinoPins, true);  
  setupAnalogJoystickEncoder(&analogDevice1  ,JOYSTICK_X, onEncoderJoyXChange);
  setupAnalogJoystickEncoder(&analogDevice2, JOYSTICK_Y, onEncoderJoyYChange);  
  switches.changeEncoderPrecision(500, 250);    

  Serial.println("Setup done.");   
}

void loop() {
  taskManager.runLoop();
}


Author: davetcc
20/01/2020 10:14:29
Hi there, couple of things to start with.

First you are right that you only need one analog abstraction. It just maps the underlying Arduino functions.

Secondly, there’s a guide to using multiple encoders here that may be useful:

https://www.thecoderscorner.com/products/arduino-libraries/io-abstraction/switches-rotary-encoder-documentation/

See the section on using multiple encoders titled ‘Advanced usage of rotary encoders’.

Let me know if you still need a bit more detail and I’ll write it up later today in the documentation.

Author: ByteC
23/01/2020 07:17:14
 
davetcc wrote:Hi there, couple of things to start with.

First you are right that you only need one analog abstraction. It just maps the underlying Arduino functions.

Secondly, there’s a guide to using multiple encoders here that may be useful:

https://www.thecoderscorner.com/products/arduino-libraries/io-abstraction/switches-rotary-encoder-documentation/

See the section on using multiple encoders titled ‘Advanced usage of rotary encoders’.

Let me know if you still need a bit more detail and I’ll write it up later today in the documentation.


Many thanks Dave, will browse through this weekend!

Author: ByteC
28/01/2020 21:55:31
 
davetcc wrote:Hi there, couple of things to start with.

First you are right that you only need one analog abstraction. It just maps the underlying Arduino functions.

Secondly, there’s a guide to using multiple encoders here that may be useful:

https://www.thecoderscorner.com/products/arduino-libraries/io-abstraction/switches-rotary-encoder-documentation/

See the section on using multiple encoders titled ‘Advanced usage of rotary encoders’.

Let me know if you still need a bit more detail and I’ll write it up later today in the documentation.


Hmm, still stuck with adding a 2nd joystick encoder... i guess that's possibly a problem with my overall knowledge of c++

Normally I would add 2 object references pointing at encoder 0 and 1 (encoder 0..3 should be possible afaik)
As it seems, I have to use interrupts when using 2 encoders. This might be problematic with analog joystick pins, so could you please
add one or two example lines for joystick encoders, how to initialize such objects, that would be great!

All the best,

Peter

Author: davetcc
03/02/2020 10:37:38
If we look at the below function copied out of JoystickSwitchInput.h

auto joystickEncoder = new JoystickSwitchInput(analogDevice, analogPin, callback);
switches.setEncoder(joystickEncoder);
taskManager.scheduleOnce(250, joystickEncoder);


Then we can see that each encoder does not need an interrupt; instead, they need to be scheduled by task manager.

Can you try something along the lines of:

auto joystickEncoder = new JoystickSwitchInput(analogDevice, analogPin, callback);
switches.setEncoder(0, joystickEncoder);
taskManager.scheduleOnce(250, joystickEncoder);

auto joystickEncoder2 = new JoystickSwitchInput(analogDevice, analogPin2, callback2);
switches.setEncoder(1, joystickEncoder2);
taskManager.scheduleOnce(250, joystickEncoder2);

// your other initialisation


Author: ByteC
08/02/2020 18:08:53
 
davetcc wrote:If we look at the below function copied out of JoystickSwitchInput.h

auto joystickEncoder = new JoystickSwitchInput(analogDevice, analogPin, callback);
switches.setEncoder(joystickEncoder);
taskManager.scheduleOnce(250, joystickEncoder);


Then we can see that each encoder does not need an interrupt; instead, they need to be scheduled by task manager.

Can you try something along the lines of:

auto joystickEncoder = new JoystickSwitchInput(analogDevice, analogPin, callback);
switches.setEncoder(0, joystickEncoder);
taskManager.scheduleOnce(250, joystickEncoder);

auto joystickEncoder2 = new JoystickSwitchInput(analogDevice, analogPin2, callback2);
switches.setEncoder(1, joystickEncoder2);
taskManager.scheduleOnce(250, joystickEncoder2);

// your other initialisation



Thank you Dave, that was really helpful!

// Joystick X/Y
  switches.initialise(arduinoPins, true);  

  auto joystickEncoder0 = new JoystickSwitchInput(&analogDevice, JOYSTICK_X, onEncoderJoyXChange);
  switches.setEncoder(0, joystickEncoder0);
  taskManager.scheduleOnce(250, joystickEncoder0);

  auto joystickEncoder1 = new JoystickSwitchInput(&analogDevice, JOYSTICK_Y, onEncoderJoyYChange);
  switches.setEncoder(1, joystickEncoder1);
  taskManager.scheduleOnce(250, joystickEncoder1);

  switches.changeEncoderPrecision(0, 256, 0);      
  switches.changeEncoderPrecision(1, 256, 0);

Still struggling with c++, but this is just a syntax and definition issue, no showstopper.
I really have to go through all of your routines to gain more knowledge.

Pretty elegant code and possibilities, thank you for sharing your library with us smilie

Cheers,

Peter

Author: patebeng
17/09/2021 01:10:24
removed.

Author: patebeng
17/09/2021 01:13:49
removed




Register / Login  |  Desktop view  |  Jump to top of page