I'm developing a sidestick for flight simulation and I'm using two encoders, one on digital ports 2 and 3 and another on ports 4 and 5, but I haven't had success with the programming yet! I would like you to give me a light on how to read these values in these ports, would you like to get the X, Y axes? Note: I'm using not only the joystick.h library but also the Encoder library.
Code:
Encoder myEnc01(3, 2);
Encoder myEnc02(4, 5);
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
JOYSTICK_TYPE_MULTI_AXIS, 32, 0,
true, true, false, false, false, false,
true, true, false, false, false);
void setup() {
//Serial.begin(115200);
Joystick.begin();
pinMode(13,OUTPUT);
analogWrite(13,255);
}`Texto pré-formatado`
void loop() {
long newPosition01 = myEnc01.read();
long newPosition02 = myEnc02.read();
//axes
Joystick.setXAxis(map(myEnc01.read()),0, 1024, -127, 128)); => unsuccessfully
Joystick.setYAxis(map(digitalRead(newPosition02),0, 1024, -127, 128)); => unsuccessfully
Serial.println(myEnc01.read());
exit status 1
too few arguments to function 'long int map(long int, long int, long int, long int, long int)'
That line looks wrong to me. Maybe it should be:
Joystick.setXAxis(map(myEnc01.read(),0, 1024, -127, 128));
Removed an extra ')', after read call.
Hello, yes the code compiled now but it doesn't move the X, Y axes, maybe my logic is not correct! I'm new to Arduino!
#include "Joystick.h"
#include <Encoder.h>
Encoder myEnc01(3, 2);
Encoder myEnc02(4, 5);
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
JOYSTICK_TYPE_MULTI_AXIS, 32, 0,
true, true, false, false, false, false,
true, true, false, false, false);
void setup() {
Joystick.begin();
}
void loop() {
long newPosition01 = myEnc01.read();
long newPosition02 = myEnc02.read();
//EIXOS
Joystick.setXAxis(map(myEnc01.read(),0, 1024, -127, 128));
Joystick.setXAxis(map(myEnc02.read(),0, 1024, -127, 128));
digite ou cole o código aqui
}
Hi, @edmarpmc
Welcome to the forum.
Can you please post your complete code and not just the start it?
What model Arduino are you using?
Can you please post link to data/specs of your encoders?
Can you please post an image(s) of your project?
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, component names and pin labels.
Can you place Serial.print statements in your code to use as debug through the IDE monitor?
Thanks.. Tom..
You have 2 calls to Joystick.setXAxis()
. I am guessing that one of them should be Joystick.setYAxis()
Good catch..
Tom...
#include "Joystick.h"
#include <Encoder.h>
int ganho = 0;
int Position01 = 0;
long oldPosition01 = -999;
int ganho02 = 0;
int Position02 = 0;
long oldPosition02 = -999;
int pulsosVolta= 50;
Encoder myEnc01(3, 2);
Encoder myEnc02(6, 5);
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
JOYSTICK_TYPE_MULTI_AXIS, 32, 0,
true, true, false, false, false, false,
true, true, false, false, false);
void setup() {
Joystick.setXAxisRange (0, (pulsosVolta*2.8));
Joystick.begin();
pinMode(13,OUTPUT);
analogWrite(13,255);
}
void loop() {
long newPosition01 = myEnc01.read();
long newPosition02 = myEnc02.read();
if (digitalRead(4)== LOW){
ganho = (map(newPosition01,(pulsosVolta*(-1.4)),(pulsosVolta*1.4),(pulsosVolta*1.4),(pulsosVolta*(-1.4))));
}
//AXLE X
if (newPosition01 != oldPosition01){
oldPosition01 = newPosition01;
}
Position01 = (newPosition01 + ganho);
if (Position01 >= pulsosVolta*1.4){
Position01 = pulsosVolta*1.4;
}
if (Position01 <= pulsosVolta*(-1.4)){
Position01 = pulsosVolta*(-1.4);
}
//AXLE Y
if (newPosition02 != oldPosition02){
oldPosition02 = newPosition02;
}
Position02 = (newPosition02 + ganho);
if (Position02 >= pulsosVolta*1.4){
Position02 = pulsosVolta*1.4;
}
if (Position02 <= pulsosVolta*(-1.4)){
Position02 = pulsosVolta*(-1.4);
}
Joystick.setXAxis (map(Position01,pulsosVolta*(-1.4),pulsosVolta*1.4,0,pulsosVolta*2.8));
Joystick.setYAxis (map(Position02,pulsosVolta*(-1.4),pulsosVolta*1.4,0,pulsosVolta*2.8));
}
Hello goodnight
I have this new code, when I move the X axis it moves, but when I move the Y axis it doesn't move and the X axis stops as well! I don't have much experience with arduino, I must be wrong somewhere. I also send you the schematic in .jpg, if you can help me, thank you in advance!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.