Closed darioragusa closed 5 years ago
I think I succeeded
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 :(!!!
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:
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;
}
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.
someone help me to add the encoder to my code
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[] = {};
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); }
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?