Cleric-K / vJoySerialFeeder

Feed Virtual Joystick driver with data from a serial port
GNU General Public License v3.0
252 stars 55 forks source link

Arduino #22

Closed darioragusa closed 5 years ago

darioragusa commented 5 years ago

Hello, I have a problem with the configuration. I connected to my Arduino a rotary encoder that send through serial port his position (-6 to 6). How I can use this value to move the X Axis?

darioragusa commented 5 years ago

I think I succeeded

hoantv commented 4 years ago

I faced this problem. Encoder range value is from -334 to 334 (When i used joystick library i work fine). unsigned long time = millis(); ibus.begin(); ibus.write(encoderPos); ibus.end(); // Joystick.setSteering(encoderPos); // Serial.println (encoderPos);

time = millis() - time; // time elapsed in reading the inputs if(time < UPDATE_INTERVAL) // sleep till it is time for the next update delay(UPDATE_INTERVAL - time); When i monitored serial port, it display @ [][][] @A[][][] @B[][][] ...(it change when i rotate encoder) I dont know how to send encoder position :(!!!

Cleric-K commented 4 years ago

Can you tell me what exactly rotary encoder are you using? How do you imagine the rotary motion should be translated into joystick motion? There's a fundamental difference: rotary encoder has infinite range of motion - you detect whether you are going one way or the other - while joystick has finite axes.

So there are different scenarios:

  1. each notch rotation of the encoder can send a button press - for example one button is pressed for each notch when rotating in one direction and another button is pressed in the other.
  2. you can transform rotary encoder into axis based on the speed of rotation - the faster you rotate in one direction, the more amount of joystick axis deflection. This is more complicated to implement.
  3. .. I'm sure there other ways also ...
hoantv commented 4 years ago

I use encoder 334 line. When i use the Joystick, X axis display exactly like encoder. Here is mycode, void setup() { pinMode (encoderPinA, INPUT); pinMode (encoderPinB, INPUT); attachInterrupt(digitalPinToInterrupt(0), updateEncoder, CHANGE); attachInterrupt(digitalPinToInterrupt(1), updateEncoder, CHANGE); // Joystick.setSteeringRange(maxCCW, maxCW); // Joystick.begin(); / Serial.begin(BAUD_RATE); } void loop() {

ibus.begin; ibus.write(encoderPos); ibus.end; }

void updateEncoder(){ pinAcurrent = digitalRead(encoderPinA); if ((pinALast == LOW) && (pinAcurrent == HIGH)) { if (digitalRead(encoderPinB) == LOW) { encoderPos++; } else { encoderPos--; }
} if (encoderPos > maxCW) { encoderPos = maxCW; } if (encoderPos < maxCCW) { encoderPos = maxCCW; } pinALast = pinAcurrent;
}

Cleric-K commented 4 years ago

Have you tried to dump the values from the encoder by using not ibus, but simple Serial.write ? Do the values make sense? What are the values of maxCCW and maxCW ? Also how do you declare the ibus object? What number of channels you give to the constructor? If your code is as in the example (the loop()), then you must create ibus as:

Ibus ibus = Ibus(1);

It is very important to have exactly as many ibus.write() calls between ibus.begin and ibus.end, as the number in the constructor above.

GangGames commented 1 year ago

someone help me to add the encoder to my code

include "ibus.h"

define UPDATE_INTERVAL 10 // milliseconds

byte analogPins[] = {A0, A1, A2};

byte digitalPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34};

byte digitalBitmappedPins[] = {};

define ANALOG_REFERENCE DEFAULT

define BAUD_RATE 115200

define ANALOG_INPUTS_COUNT sizeof(analogPins)

define DIGITAL_INPUTS_COUNT sizeof(digitalPins)

define DIGITAL_BITMAPPED_INPUTS_COUNT sizeof(digitalBitmappedPins)

define NUM_CHANNELS ( (ANALOG_INPUTS_COUNT) + (DIGITAL_INPUTS_COUNT) + (15 + (DIGITAL_BITMAPPED_INPUTS_COUNT))/16 )

IBus ibus(NUM_CHANNELS);

void setup() { for(int i=0; i < DIGITAL_INPUTS_COUNT; i++) { pinMode(digitalPins[i], INPUT_PULLUP); }

for(int i=0; i < DIGITAL_BITMAPPED_INPUTS_COUNT; i++) { pinMode(digitalBitmappedPins[i], INPUT_PULLUP); }

analogReference(ANALOG_REFERENCE); // use the defined ADC reference voltage source Serial.begin(BAUD_RATE); // setup serial }

void loop() { int i, bm_ch = 0; unsigned long time = millis();

ibus.begin();

// read analog pins - one per channel for(i=0; i < ANALOG_INPUTS_COUNT; i++) ibus.write(1000 + (uint32_t)analogRead(analogPins[i])*1000/1023); // map 0-1023 to 1000-2000

// read digital pins - one per channel for(i=0; i < DIGITAL_INPUTS_COUNT; i++) ibus.write(digitalRead(digitalPins[i]) == LOW ? 2000 : 1000);

// read digital bit-mapped pins - 16 pins go in one channel for(i=0; i < DIGITAL_BITMAPPED_INPUTS_COUNT; i++) { int bit = i%16; if(digitalRead(digitalBitmappedPins[i]) == LOW) bm_ch |= 1 << bit;

  if(bit == 15 || i == DIGITAL_BITMAPPED_INPUTS_COUNT-1) {
     // data for one channel ready
     ibus.write(bm_ch);
     bm_ch = 0;
  }

}

ibus.end();

time = millis() - time; // time elapsed in reading the inputs if(time < UPDATE_INTERVAL) // sleep till it is time for the next update delay(UPDATE_INTERVAL - time); }