Bad read linear pot

Hi folks:

Thanks in advance for read my post.

Well the problem is: i did an interface to connect yoke and pedal from Gameport, the project was substracted from: GitHub - shadwork/Arduino-PC-Gameport-HID: Turn Arduino Leonardo into converter PC Gameport DB15 -> USB HID

Well... i've got some trouble:

  1. The Xaxis and Yaxis doesn't move to the right and up. (I guess there is some problem reading the linear pot).

  2. I want to add 2 more switches, how?...

Thanks for your time.

Your code?

How to use this forum

Yes, i forgot it... sorry guys :smiley:

// Settings
#define ENABLE_DEBUG;

// Pins
const int button0 = 7;
const int button1 = 6;
const int button2 = 5;
const int button3 = 4;
const int button4 = 3; //Added by me
const int button5 = 2; //Added by me

const int axis0 = A0;
const int axis1 = A1;
const int axis2 = A2;
const int axis3 = A3;

// Analog to digital
const int AXIS_MIN = 550;
const int AXIS_MAX = 1023;
const int AXIS_NUL = 720;

// Joestick
JoyState_t joyStick;

// Ugly vars
int buttonState0= 0;  
int buttonState1= 0;
int buttonState2= 0;
int buttonState3= 0;
int buttonState4= 0;
int buttonState5= 0;
int buttonState6= 0;
int buttonState7= 0;
int buttonState8= 0;
int buttonState9= 0;

// Setup
void setup() {
  Serial.begin(9600);
  // pull up
  pinMode(button0, INPUT_PULLUP);     
  pinMode(button1, INPUT_PULLUP); 
  pinMode(button2, INPUT_PULLUP); 
  pinMode(button3, INPUT_PULLUP);
  pinMode(button4, INPUT_PULLUP);    //Added by me
  pinMode(button5, INPUT_PULLUP);   //Added by me
}

void loop(){
  // read data
  buttonState0 = !digitalRead(button0);
  buttonState1 = !digitalRead(button1);
  buttonState2 = !digitalRead(button2);
  buttonState3 = !digitalRead(button3);  
  buttonState4 = !digitalRead(button4); //Added by me
  buttonState5 = !digitalRead(button5); //Added by me

  buttonState6 = analogRead(axis0);
  buttonState7 = analogRead(axis1);
  buttonState8 = analogRead(axis2);
  buttonState9 = analogRead(axis3);  


#ifdef ENABLE_DEBUG
  Serial.print(buttonState0);
  Serial.print(buttonState1);
  Serial.print(buttonState2);
  Serial.print(buttonState3); 
  Serial.print(buttonState4); //Added by me 
  Serial.print(buttonState5); //Added by me
  Serial.print(" ");
  Serial.print(buttonState6);     
  Serial.print(" ");
  Serial.print(buttonState7); 
  Serial.print(" ");
  Serial.print(buttonState8); 
  Serial.print(" ");
  Serial.print(buttonState9);     
  Serial.println("");
#endif 

 
  float tempState = 127;
  if(buttonState6>AXIS_NUL){
    tempState = 127+(AXIS_NUL - buttonState6)/2; 
  }
  else{
    tempState =127+ (AXIS_NUL - buttonState6)/1;     
  }
  tempState = tempState<0?0:tempState;
  tempState = tempState>200?200:tempState;  
  joyStick.xAxis = tempState;

  tempState = 127;
  if(buttonState7>AXIS_NUL){
    tempState = 127+(AXIS_NUL - buttonState7)/2; 
  }
  else{
    tempState =127+ (AXIS_NUL - buttonState7)/1;     
  }
  tempState = tempState<0?0:tempState;
  tempState = tempState>255?255:tempState;  
  joyStick.yAxis = tempState;  

  tempState = buttonState8 - AXIS_MIN;
  //tempState = tempState<0?0:tempState;
  //tempState = (256 *tempState)/ (float)(AXIS_MAX-AXIS_MIN);
  tempState = tempState>255?255:tempState;  
  joyStick.rudder = tempState; 

  tempState = buttonState9 - AXIS_MIN;
  tempState = tempState<0?0:tempState;
  tempState = (256 *tempState)/ (float)(AXIS_MAX-AXIS_MIN);
  tempState = abs(tempState - 256);
  tempState = tempState<0?0:tempState;
  tempState = tempState>255?255:tempState;  
  joyStick.throttle = tempState;

  // all another axis set to zero position
  joyStick.zAxis = 127;
  joyStick.xRotAxis = 127;
  joyStick.yRotAxis = 127;
  joyStick.zRotAxis = 127;
  //joyStick.throttle = 127;
  //joyStick.rudder = 127;


  // hat connected to 4 key
  if(buttonState0 && buttonState1 && buttonState2 && buttonState3){
    joyStick.hatSw1 = 0; 
  }
  else if(buttonState0 && buttonState1 && !buttonState2 && buttonState3){
    joyStick.hatSw1 = 4;
  }
  else if(buttonState0 && buttonState1 && !buttonState2 && !buttonState3){
    joyStick.hatSw1 = 6;
  }
  else if(buttonState0 && buttonState1 && buttonState2 && !buttonState3){
    joyStick.hatSw1 = 2;
  }
  else{
    joyStick.hatSw1 = 8;
    // buttons connects
    joyStick.buttons = 0;
    // digital buttons to mask
    if(buttonState0){
      joyStick.buttons = joyStick.buttons | 1;  
    }

    if(buttonState1){
      joyStick.buttons = joyStick.buttons | 2;  
    }

    if(buttonState2){
      joyStick.buttons = joyStick.buttons | 4;  
    }

    if(buttonState3){
      joyStick.buttons = joyStick.buttons | 8;  
    } 

  }  

  // and hat2 to center
  joyStick.hatSw2 = 8;    

  Joystick.setState(&joyStick);

  delay(1);
}

I'm using The file uploaded...

HID.cpp (17.3 KB)

USBAPI.h (7.26 KB)

Hi people:

I',ve solved a lot of trouble with this project but this is driving me crazy...

An image worth more than 1000 words... (Image attached)

The happy face is the area where the axis move the problem is is not been possible make it work for the other area...

Rudder just came till' there and stop it and when i move the linear pot it cames till the end and reset the value (Something strange).

Here is the code:

// Settings
#define ENABLE_DEBUG;

// Pins
const int button0 = 7;
const int button1 = 6;
const int button2 = 5;
const int button3 = 4;
const int button4 = 3; //Added by me
const int button5 = 2; //Added by me

const int axis0 = A0;
const int axis1 = A1;
const int axis2 = A2;
const int axis3 = A3;

// Analog to digital
const int AXIS_MIN = 550;
const int AXIS_MAX = 1023;
const int AXIS_NUL = 720;

// Joestick
JoyState_t joyStick;

// Ugly vars
int buttonState0= 0;  
int buttonState1= 0;
int buttonState2= 0;
int buttonState3= 0;
int buttonState4= 0;
int buttonState5= 0;
int buttonState6= 0;
int buttonState7= 0;
int buttonState8= 0;
int buttonState9= 0;

// Setup
void setup() {
  Serial.begin(9600);
  // pull up
  pinMode(button0, INPUT_PULLUP);     
  pinMode(button1, INPUT_PULLUP); 
  pinMode(button2, INPUT_PULLUP); 
  pinMode(button3, INPUT_PULLUP);
  pinMode(button4, INPUT_PULLUP);    //Added by me
  pinMode(button5, INPUT_PULLUP);   //Added by me
}

void loop(){
  // read data
  buttonState0 = !digitalRead(button0);
  buttonState1 = !digitalRead(button1);
  buttonState2 = !digitalRead(button2);
  buttonState3 = !digitalRead(button3);  
  buttonState4 = !digitalRead(button4); //Added by me
  buttonState5 = !digitalRead(button5); //Added by me

  buttonState6 = analogRead(axis0);
  buttonState7 = analogRead(axis1);
  buttonState8 = analogRead(axis2);
  buttonState9 = analogRead(axis3);  


#ifdef ENABLE_DEBUG
  Serial.print(buttonState0);
  Serial.print(buttonState1);
  Serial.print(buttonState2);
  Serial.print(buttonState3); 
  Serial.print(buttonState4); //Added by me 
  Serial.print(buttonState5); //Added by me
  Serial.print(" ");
  Serial.print(buttonState6);     
  Serial.print(" ");
  Serial.print(buttonState7); 
  Serial.print(" ");
  Serial.print(buttonState8); 
  Serial.print(" ");
  Serial.print(buttonState9);     
  Serial.println(" ");
#endif 

 
  float tempState = 127;
  if(buttonState6>AXIS_NUL){
    tempState = 127+(AXIS_NUL - buttonState6)/2; 
  }
  else{
    tempState =127+ (AXIS_NUL - buttonState6)/1;     
  }
  tempState = tempState<0?0:tempState;
  tempState = tempState>255?255:tempState;  
  joyStick.xAxis = tempState;

  tempState = 127;
  if(buttonState7>AXIS_NUL){
    tempState = 127+(AXIS_NUL - buttonState7)/2; 
  }
  else{
    tempState =127+ (AXIS_NUL - buttonState7)/1;     
  }
  tempState = tempState<0?0:tempState;
  tempState = tempState>255?255:tempState;  
  joyStick.yAxis = tempState;  

  tempState = buttonState8 - AXIS_MIN;
  //tempState = tempState<0?0:tempState;
  //tempState = (256 *tempState)/ (float)(AXIS_MAX-AXIS_MIN);
  tempState = tempState>255?255:tempState;  
  joyStick.rudder = tempState; 

  tempState = buttonState9 - AXIS_MIN;
  tempState = tempState<0?0:tempState;
  tempState = (256 *tempState)/ (float)(AXIS_MAX-AXIS_MIN);
  tempState = abs(tempState - 256);
  tempState = tempState<0?0:tempState;
  tempState = tempState>255?255:tempState;  
  joyStick.throttle = tempState;

  // all another axis set to zero position
  joyStick.zAxis = 127;
  joyStick.xRotAxis = 127;
  joyStick.yRotAxis = 127;
  joyStick.zRotAxis = 127;
  //joyStick.throttle = 127;
  //joyStick.rudder = 127;


  // hat connected to 4 key
  if(buttonState0 && buttonState1 && buttonState2 && buttonState3){
    joyStick.hatSw1 = 0; 
  }
  else if(buttonState0 && buttonState1 && !buttonState2 && buttonState3){
    joyStick.hatSw1 = 4;
  }
  else if(buttonState0 && buttonState1 && !buttonState2 && !buttonState3){
    joyStick.hatSw1 = 6;
  }
  else if(buttonState0 && buttonState1 && buttonState2 && !buttonState3){
    joyStick.hatSw1 = 2;
  }
  else{
    joyStick.hatSw1 = 8;
    // buttons connects
    joyStick.buttons = 0;
    // digital buttons to mask
    if(buttonState0){
      joyStick.buttons = joyStick.buttons | 1;  
    }

    if(buttonState1){
      joyStick.buttons = joyStick.buttons | 2;  
    }

    if(buttonState2){
      joyStick.buttons = joyStick.buttons | 4;  
    }

    if(buttonState3){
      joyStick.buttons = joyStick.buttons | 8;  
    } 

  }  

  // and hat2 to center
  joyStick.hatSw2 = 8;    

  Joystick.setState(&joyStick);

  delay(1);
}

Thanks a lot

Untitled.jpg

const int button0 = 7;
const int button1 = 6;
const int button2 = 5;
const int button3 = 4;
const int button4 = 3; //Added by me
const int button5 = 2; //Added by me

Are those comments REALLY useful?

Have you ever heard of arrays?

  buttonState6 = analogRead(axis0);
  buttonState7 = analogRead(axis1);
  buttonState8 = analogRead(axis2);
  buttonState9 = analogRead(axis3);

Nonsense. Digital pins have state (pressed or not pressed). Analog pins have values.

  if(buttonState6>AXIS_NUL){
    tempState = 127+(AXIS_NUL - buttonState6)/2; 
  }
  else{
    tempState =127+ (AXIS_NUL - buttonState6)/1;     
  }

Divided by 1?

  tempState = tempState<0?0:tempState;
  tempState = tempState>255?255:tempState;

constrain() is too subtle for you?

Print the values being assigned to the joystick object. Somewhere, you are assigning an incorrect value.

Well guys:

The deal is: Changing the value:

 joyStick.xAxis = tempState;

Instead of "tempState" i put 1023 and the cross remain in the middle.

I put 511 and the cross reamain in the middle.

Same for the Yaxis.

Any suggest?...

Here i put the code:

// Settings
#define ENABLE_DEBUG;

// Pins
const int button0 = 7;
const int button1 = 6;
const int button2 = 5;
const int button3 = 4;
const int button4 = 3; //Added by me
const int button5 = 2; //Added by me

const int axis0 = A0;
const int axis1 = A1;
const int axis2 = A2;
const int axis3 = A3;

// Analog to digital
const int AXIS_MIN = 511;
const int AXIS_MAX = 1023;
const int AXIS_NUL = 127;

// Joestick
JoyState_t joyStick;

// Ugly vars
int buttonState0= 0;  
int buttonState1= 0;
int buttonState2= 0;
int buttonState3= 0;
int buttonState4= 0;
int buttonState5= 0;
int buttonState6= 0;
int buttonState7= 0;
int buttonState8= 0;
int buttonState9= 0;

// Setup
void setup() {
  Serial.begin(9600);
  // pull up
  pinMode(button0, INPUT_PULLUP);     
  pinMode(button1, INPUT_PULLUP); 
  pinMode(button2, INPUT_PULLUP); 
  pinMode(button3, INPUT_PULLUP);
  pinMode(button4, INPUT_PULLUP);    //Added by me
  pinMode(button5, INPUT_PULLUP);   //Added by me
}

void loop(){
  // read data
  buttonState0 = !digitalRead(button0);
  buttonState1 = !digitalRead(button1);
  buttonState2 = !digitalRead(button2);
  buttonState3 = !digitalRead(button3);  
  buttonState4 = !digitalRead(button4); //Added by me
  buttonState5 = !digitalRead(button5); //Added by me

  buttonState6 = analogRead(axis0);
  buttonState7 = analogRead(axis1);
  buttonState8 = analogRead(axis2);
  buttonState9 = analogRead(axis3);  


#ifdef ENABLE_DEBUG
  Serial.print(buttonState0);
  Serial.print(buttonState1);
  Serial.print(buttonState2);
  Serial.print(buttonState3); 
  Serial.print(buttonState4); //Added by me 
  Serial.print(buttonState5); //Added by me
  Serial.print(" ");
  Serial.print(buttonState6);     
  Serial.print(" ");
  Serial.print(buttonState7); 
  Serial.print(" ");
  Serial.print(buttonState8); 
  Serial.print(" ");
  Serial.print(buttonState9);     
  Serial.println(" ");
#endif 

 
  float tempState = 127;
  if(buttonState6>AXIS_NUL){
    tempState = 127+(AXIS_NUL - buttonState6)/2; 
  }
  else{
    tempState =127+ (AXIS_NUL - buttonState6)/1;     
  }
  tempState = tempState<0?0:tempState;
  tempState = tempState>255?255:tempState;  
  joyStick.xAxis = tempState;

  tempState = 127;
  if(buttonState7>AXIS_NUL){
    tempState = 127+(AXIS_NUL - buttonState7)/2; 
  }
  else{
    tempState =127+ (AXIS_NUL - buttonState7)/1;     
  }
  tempState = tempState<0?0:tempState;
  tempState = tempState>255?255:tempState;  
  joyStick.yAxis = tempState;  

  tempState = buttonState8 - AXIS_MIN;
  //tempState = tempState<0?0:tempState;
  //tempState = (256 *tempState)/ (float)(AXIS_MAX-AXIS_MIN);
  tempState = tempState>255?255:tempState;  
  joyStick.rudder = tempState; 

  tempState = buttonState9 - AXIS_MIN;
  tempState = tempState<0?0:tempState;
  tempState = (256 *tempState)/ (float)(AXIS_MAX-AXIS_MIN);
  tempState = abs(tempState - 256);
  tempState = tempState<0?0:tempState;
  tempState = tempState>255?255:tempState;  
  joyStick.throttle = tempState;

  // all another axis set to zero position
  joyStick.zAxis = 127;
  joyStick.xRotAxis = 127;
  joyStick.yRotAxis = 127;
  joyStick.zRotAxis = 127;
  //joyStick.throttle = 127;
  //joyStick.rudder = 127;


  // hat connected to 4 key
  if(buttonState0 && buttonState1 && buttonState2 && buttonState3){
    joyStick.hatSw1 = 0; 
  }
  else if(buttonState0 && buttonState1 && !buttonState2 && buttonState3){
    joyStick.hatSw1 = 4;
  }
  else if(buttonState0 && buttonState1 && !buttonState2 && !buttonState3){
    joyStick.hatSw1 = 6;
  }
  else if(buttonState0 && buttonState1 && buttonState2 && !buttonState3){
    joyStick.hatSw1 = 2;
  }
  else{
    joyStick.hatSw1 = 8;
    // buttons connects
    joyStick.buttons = 0;
    // digital buttons to mask
    if(buttonState0){
      joyStick.buttons = joyStick.buttons | 1;  
    }

    if(buttonState1){
      joyStick.buttons = joyStick.buttons | 2;  
    }

    if(buttonState2){
      joyStick.buttons = joyStick.buttons | 4;  
    }

    if(buttonState3){
      joyStick.buttons = joyStick.buttons | 8;  
    } 

  }  

  // and hat2 to center
  joyStick.hatSw2 = 8;    

  Joystick.setState(&joyStick);

  delay(1);
}

Thanks in advance

PaulS:
Are those comments REALLY useful?

That is something I do myself all the time - mark the changes I make in someone else's code. It makes it easy to revert it back to the original when I need to, or simply to find the lines I have changed.

Having said that I can't make any sense of the OP's code. It would be easier to follow if it used arrays. And it looks like a complex way to respond to a joystick - but that may just be because it is hard to see the wood for the trees.

Some comments that explain what the different sections of code are supposed to do would be a big help.

...R

@cptburgos, do not cross-post. Threads merged.

@cptburgos, do not cross-post. Yet another thread merged.

Divided by 1?

At least it wasn't divided by zero!

Robin2:
That is something I do myself all the time - mark the changes I make in someone else's code. It makes it easy to revert it back to the original when I need to, or simply to find the lines I have changed.

Let me introduce you to Git.

Never mind adding comments to lines. How do you comment code you have removed?

Use Git. Track your changes.

[quote author=Nick Gammon date=1430730537 link=msg=2216822]Use Git. Track your changes.
[/quote]
Jeez - I avoid that like the plague.

Just like Facebook and Twitter :slight_smile:

...R

Sorry, IMHO Git is even worse than Github. I live in the stone age and I'm happy there.

I have looked at Git 3 or 4 times and I have NO IDEA what it is supposed to do or how it could be any use to me. It is one of those things that just does not click with my brain.

In any case I was not suggesting marking the changes for version control purposes - but merely so that I can see what I changed.

...R

There is no way you have to get onto GitHub or any other hub. Git is nothing like Facebook.

You can run it purely locally. GitHub is simply a convenience for sharing code (like I do).

It is very handy to know, after you made a whole lot of changes, maybe over a few days, what you changed. Putting in comments like "I changed this line" hardly cuts it.

Using the command line, change to the directory where your project is (the sketch folder).

Initialize git in that folder:

git init

Add your current files:

git add *.ino
git add *.cpp
git add *.h

(As appropriate)

Commit this initial add:

git commit -m "Initial commit"

Now, work away to your heart's desire. To compare changes to when you last commited:

git diff

Say you now have a working version, that you don't want to lose before you add "that new great feature". Add and commit all your changes:

git commit -am "Improvements for xxx purpose"

Basically keep doing the above. If you add files to the project don't forget to add them to git. You can be reminded of what has been changed (or not added) by doing:

git status

When things go to hell, can you issue a
git f**ked
command?

Put it another way: git is like a backup system. Basically a commit makes a copy of your files as they were at a point in time. So if you do something foolish, you get them back from git.

This doesn't protect you from accidentally deleting the entire directory, which is why "pushing" a repository somewhere else, regularly, is helpful. GitHub is one such place. Or you can just use another folder on another disk or shared drive.

I did not mean it was technically like Facebook.

I just meant that I avoid it for the same luddite reasons that I avoid Facebook.

...R