Hello, Like all joystick threads I have decide to build a joystick to fit my needs. One feature I want it to have is to toggle between different axis with a push of a button. I want to be able to control the in game camera then press a button and the same joystick to run features in the game. Problem is I have no idea how to go about coding it. I have used the joystick library to make a working joystick. But that's where I get lost. First I though I just define 2 joysticks and use "if" and "else" statements to achieve my goal. As I was thinking about how I would write the code I felt silly setting pins for inputs for the second joystick. Then it hit me I really don't think it will work that way with out using relays and is that really the best way to go. I am a bit of a newbie with coding but love the challenge. Could i please get some advice or help? I would greatly appreciate it.
Split the project into smaller pieces. Then go on finding out how to solve each piece. Reading joysticks is one, reading buttons is another.
Then coding to make those parts make this or that can begin.
Look in the IDE examples folder for test code.
Well I have been looking at examples. But I Fail in finding a way to make one physical joysticks work as 2 different ones. Was hoping some one could show me some ideas, or help me understand what I need to do better then what I am doing.
A good place to start as usual, is to provide details of what you have there.
Code , drawing and what you are using for power etc.
Cannot see there being a problem reading the pot value for one part of your code and then reading once again for a different outcome when you push a button.
Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'
Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination
You don't need to define 2 joysticks or have the joystick connected to 2 sets of pins or anything like it
Suppose you had a boolean variable named controlCamera and set it to true. Then, in your code, if the boolean is true you execute whatever code you need to control the camera using the joystick.
Then, when the button becomes pressed you set the boolean to false in your code. If the boolean is false you execute whatever code you need to control the game using the joystick.
When the button becomes pressed again you set the boolean to true and you are back to controlling the camera and so on every time the button becomes pressed
Sorry about not my failed attempt at loading my code. I will make it my focus to do better in the future. I planning on starting a new code using booleans when I get some free time. I am sure I will have more questions. Thank you for your help so far.
So I got a chance to play around a little. I added some booleans. Figured i would add a indicator led to help know what mode I am in Just not sure how to do the part in void Loop still playing around with it but though i try posting up my changes and see what help I can get.
#include <Joystick.h>
//Define and Allocate Input Pins to memorable names
#define joyX A0
#define joyY A1
#define joyRx A2
#define joyButton1 9
#define joyButton2 8
#define joyButton3 7
//Initializing Axis as Integers, at a 0 default value
int xAxis_ = 0;
int yAxis_ = 0;
int rxAxis_ = 0;
int togglePin = 12;
int modeledPin = 13;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean ledOn = false;
//Setting up Buttons
//Updating a static variable gives greater stability than reading directly from the digital pin.
//Giving Default Values to the Buttons for later use
int lastButton1State = 0;
int lastButton2State = 0;
int lastButton3State = 0;
Joystick_ Joystick = {
Joystick_(0x04, JOYSTICK_TYPE_JOYSTICK, 8, 1, true, true, true, true, false, false, false, false, false, false, false),
};
//Set Auto Send State
//Enables Auto Sending, allowing the controller to send information to the HID system, rather than waiting to be asked.
const bool initAutoSendState = true;
void setup() {
//Initialize Buttons
//Buttons set up between Digital Pin and Ground, following pin allocations from earlier on
pinMode(joyButton1, INPUT_PULLUP);
pinMode(joyButton2, INPUT_PULLUP);
pinMode(joyButton3, INPUT_PULLUP);
//Start Joystick - Needed to start the Joystick function libary
Joystick.begin();
pinMode (togglePin, INPUT);
pinMode (modeledPin, OUTPUT);
}
boolean debounce(boolean last)
{
boolean current = digitalRead(togglePin);
if (last != current)
{
delay(5);
current = digitalRead(togglePin);
}
}
void loop() {
//Axis Reading during Runtime
//Setting Read functions for each axis and parsing correctly. The X axis will be used as an example for explanation
//Reading the X Axis analog pin to the xAxis_ variable for processing
xAxis_ = analogRead(joyX);
//Mapping the X Axis data from a 0-1023 to 0-255 range for a smoother action
xAxis_ = map(xAxis_, 0, 1023, 0, 255);
//Set the Joystick X Axis value as the new, smoother, value
Joystick.setXAxis(xAxis_);
yAxis_ = analogRead(joyY);
yAxis_ = map(yAxis_, 0, 1023, 0, 255);
Joystick.setYAxis(yAxis_);
rxAxis_ = analogRead(joyRx);
rxAxis_ = map(rxAxis_, 0, 1023, 0, 255);
Joystick.setRxAxis(rxAxis_);
//Button Reading during Runtime
//Setting Read functions for each button, using a state value for memory. Button 1 will be used as an example for explanation
//Reading the current Button digital pin to the Current Button State for processing
int currentButton1State = !digitalRead(joyButton1);
//If loop - Check that the button has actually changed.
if (currentButton1State != lastButton1State) {
//If the button has changed, set the specified HID button to the Current Button State
Joystick.setButton(0, currentButton1State);
//Update the Stored Button State
lastButton1State = currentButton1State;
}
int currentButton2State = !digitalRead(joyButton2);
if (currentButton2State != lastButton2State) {
Joystick.setButton(1, currentButton2State);
lastButton2State = currentButton2State;
}
int currentButton3State = !digitalRead(joyButton3);
if (currentButton3State != lastButton3State) {
Joystick.setButton(2, currentButton3State);
lastButton3State = currentButton3State;
}
//Pole Delay/Debounce
//To reduce unessecary processing, the frequency of the reading loop is delayed. The value(in ms) can be changed to match requirement
delay(10);
}
You seem to be making things more complicated than they need to be. Consider this program outline
start of loop()
if the button has become pressed
increment the state change counter
end if
if the state change counter is divisible by 2 (use the % operator for this)
call functions to control camera
else
call functions to control the game
end if
end of loop()
You can borrow the state change detection code from the cunningly name StateChangeDetection example in the IDE
I don't doubt I am making it more complicated then it needs to be. It not something I'm doing on purpose, But lack of understanding. There no hiding the fact that what I want to do is perhaps more advanced then my skill set. But my determination to learn drives me to keep trying. I have thought about paying someone to write it for me. But what would I learn out of that. Not to mention the satisfaction I would lose by giving up. I just hope I don't upset the ones trying to help me with my ignorance to code. I want to thank you, and I hope you understand I appreciate your help.
That is an excellent attitude. Have a go at writing the code using the suggested structure and come back with any problems
So How does this look so far? I am guessing I will need to add some functions defining the control camera and control game. Would I be right about that? If I am right about that then I guess just write the joystick read code under each function? Or would i be complicating it again?
/*
Farmsim Joystick State Change Detection
Build 1.0
*/
// Included Librarys
#include <Joystick.h>
//Defining and Allocate Input pins
#define joyX A0
#define joyY A1
#define joyZ A2
//Initializing Axis as Integers, at a o default value
int xAxis_ = 0;
int yAxis_ = 0;
int zAxis_ = 0;
// Constant Integers
const int buttonPin = 12;
const int ledPin = 13;
//Variables Integers
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
//Defining the Joystick
Joystick_ Joystick={
Joystick_(0x03, JOYSTICK_TYPE_JOYSTICK, 1, 0, true, true, true, false, false, false, false, false, false, false, false),};
void setup() {
//Initialize pin states
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
// Starting joystick
Joystick.begin();
}
void loop() {
}
It looks OK but I am not familiar with the Joystick library
The next step is to put the button state change detection code into loop and print the state change counter variable
You will, of course, need to write or copy code to do what you want the joystick to control but I thought that you had that. I would write each of them as a separate function. You will need to write them such that they do not block the free running of loop() which is where you will be detecting the button press to change function
So I added loop. I have added the joystick read code so that may help with helping me set it up right. I feel I keep getting hung up on how the pc is to know to send as a different joystick. Maybe I am over thinking it. I though using the ledPin with if statement to tell what one to send. I hope I am understand it right and not overthinking.
/*
Farmsim Joystick State Change Detection
Build 1.0
*/
// Included Librarys
#include <Joystick.h>
//Defining and Allocate Input pins
#define joyX A0
#define joyY A1
#define joyZ A2
//Initializing Axis as Integers, at a o default value
int xAxis_ = 0;
int yAxis_ = 0;
int zAxis_ = 0;
// Constant Integers
const int buttonPin = 12;
const int ledPin = 13;
//Variables Integers
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
//Defining the Joystick
Joystick_ Joystick={
Joystick_(0x03, JOYSTICK_TYPE_JOYSTICK, 1, 0, true, true, true, false, false, false, false, false, false, false, false),};
void setup() {
//Initialize pin states
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
// Starting joystick
Joystick.begin();
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
digitalWrite(ledPin, HIGH);
} else {
// if the current state is LOW then the button went from on to off:
digitalWrite(ledPin, LOW);
}
// Delay a little bit to avoid bouncing
delay(50);
}
xAxis_ = analogRead(joyX);
xAxis_ = map(yAxis_, 0, 1023, 0, 255);
Joystick.setYAxis(xAxis_);
yAxis_ = analogRead(joyY);
yAxis_ = map(yAxis_, 0, 1023, 0, 255);
Joystick.setYAxis(yAxis_);
zAxis_ = analogRead(joyZ);
zAxis_ = map(zAxis_, 0, 1023, 0, 255);
Joystick.setRxAxis(zAxis_);
}
If you mean, how is the PC going to know which type of joystick input it is receiving, then that is a problem that I can't help you with and it is not an Arduino question
How does the PC know what is being controlled by the joystick when the Arduino is not involved ?
Not sure if this will really answer your question? But when in game I go to setting and configure controls. The saitek side panel control has a button that allows the joystick to be togged just as I want this code to do. From what I understand the Leonardo will work as a HID and the joystick library helps to set it up as a joystick. I felt that it was some thing I could program Leonardo to do. That's why the post stared out with my thinking I would need to write code as two joysticks and then use a pin to identifier to what joystick the input was to go.
Is this a physical button or a button on a screen window
A physical button on top of the joystick. That's why I though this should be possible.
Ok so as I think about this more if I define the pins I want to read I could program 6 axis then when toggle pin high send reading to 3 of the axis and when low to the others. Of course it sounds easy here how complex would it really be?
Now I am confused. Back in your original post you said
IS this the button on the joystick or a button on the Arduino ? If the first then I have misunderstood what you are trying to do