Register / Login | Desktop view |
/******************************************************************************************
*** 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();
}
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.
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.
auto joystickEncoder = new JoystickSwitchInput(analogDevice, analogPin, callback);
switches.setEncoder(joystickEncoder);
taskManager.scheduleOnce(250, joystickEncoder);
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
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
// 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);