Teensy 2.0 joystick button (newby)

Status
Not open for further replies.

Surferjoe86

New member
I'm a complete beginner making a replica spitfire flight stick and throttle quadrant.

I have the pots working.

But cant for the life of me get the joystick buttons working.


I have 7 joystick buttons in my code, they all light up in the game controller window.
The issue starts when I push a button 1 on the teensy, it blanks out joystick.button 2 and 4?
This is the only button seems to be working too. I have triple checked my wiring.

0-1-2-3-4-5-6 pins (on the ground side of the teensy 2.0) all go to micro switches. From the switches the wires then go to the centre ground on the Teensy (all pots use the same ground)

CODE:

void setup() {
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);

}

void loop() {
// read analog inputs and set X-Y position
Joystick.X(analogRead(3)); //good
Joystick.Y(analogRead(1)); // good
Joystick.Z(analogRead(2)); // good


Joystick.sliderLeft(analogRead(0)); // good

Joystick.sliderRight(5);




if (digitalRead(0)== HIGH)
{ Joystick.button(1, digitalRead(0))
; } else {
Joystick.button(1, digitalRead(1))
; }

if (digitalRead(1)== HIGH)
{ Joystick.button(2, digitalRead(0))
; } else {
Joystick.button(2, digitalRead(1))
; }

if (digitalRead(2)== HIGH)
{ Joystick.button(3, digitalRead(0))
; } else {
Joystick.button(3, digitalRead(1))
; }
if (digitalRead(3)== HIGH)
{ Joystick.button(4, digitalRead(0))
; } else {
Joystick.button(4, digitalRead(1))
; }
if (digitalRead(4)== HIGH)
{ Joystick.button(5, digitalRead(0))
; } else {
Joystick.button(5, digitalRead(1))
; }

if (digitalRead(5)== HIGH)
{ Joystick.button(6, digitalRead(0))
; } else {
Joystick.button(6, digitalRead(1))
; }

if (digitalRead(6)== HIGH)
{ Joystick.button(7, digitalRead(0))
; } else {
Joystick.button(7, digitalRead(1))
; }


}


Any guidance would be greatly appreciated.
Joe
 
pretty new here myself, but I believe the problem resides in all the IF statements. I think if you had case statements for all the buttons, it would work better.
What I mean is that it doesn't know to also read these other switches at the same time.
 
Thankyou for the reply, Been looking at that. Cant for the life of me figure out how to write the code?

copied this off a site

{
// read the sensor:

if (Serial.available() > 0) {
int inByte = Serial.read();

// do something different depending on the character received.
// The switch statement expects single number values for each case;
// in this exmaple, though, you're using single quotes to tell
// the controller to get the ASCII value for the character. For
// example 'a' = 97, 'b' = 98, and so forth:

switch (inByte) {
case 'a':
digitalWrite(2, HIGH);
break;
case 'b':
digitalWrite(3, HIGH);
break;

default:
//
}

which part picks up the button being pressed on my board?
 
Ok taking a closer look at your code.
Your button one is saying IF it’s high do something. Well it’s always held high so the code never moves on to the next step.
Try saying if the button goes low, which is really what your after.
Just try replacing all your Highs with Lows.

I could be totally wrong. I think we have the blind leading the blind here😄
 
Ok taking a closer look at your code.
Your button one is saying IF it’s high do something. Well it’s always held high so the code never moves on to the next step.
Try saying if the button goes low, which is really what your after.
Just try replacing all your Highs with Lows.

I could be totally wrong. I think we have the blind leading the blind here😄


Thanks for helping.

I tried this to no avail.

I have even removed all the signal wires to the teensy leaving one in. The same issue occurs even with the one signal wire.
 
Based on your original code from what I can tell, these should not be digitalRead. It should either be 1 for on or 0 for off. This code could be improved, but if you fix those digitalReads it should get it to work.
Code:
if (digitalRead(0)== HIGH)
{ Joystick.button(1, [COLOR="#FF0000"]digitalRead(0)[/COLOR])
; } else {
Joystick.button(1, [COLOR="#FF0000"]digitalRead(1)[/COLOR])
; }
 
Based on your original code from what I can tell, these should not be digitalRead. It should either be 1 for on or 0 for off. This code could be improved, but if you fix those digitalReads it should get it to work.
Code:
if (digitalRead(0)== HIGH)
{ Joystick.button(1, [COLOR="#FF0000"]digitalRead(0)[/COLOR])
; } else {
Joystick.button(1, [COLOR="#FF0000"]digitalRead(1)[/COLOR])
; }



Brilliant thankyou. It's working!
Really appreciate your help.
joe
 
Status
Not open for further replies.
Back
Top