Changing an axis in joystick.h based on a switch state

Hello everyone
I am new to this and have worked through a bunch of tutorials. I understand the basics of coding, having used Fortran a lot at university. I need to figure out a way to change the output of an axis in using the Joystick.h library based on the state of a switch. This is because I am building a throttle for DCS that will have functional autothrottles moved by a stepper motor. The hardware is all working, and when used a basic axis with buttons, it works beautifuly. The problem that I am having is that I need to set the value of the axis to a set value that does not change if a switch (the autothrottle switch) is engaged. I have tried this code and the axis does not work at all - the axis is completely stuck, ie does not move, regardless of the switch state.

//Arduino Joystick 2.0 Library, by MHeironimus (https://github.com/MHeironimus)
//Beginners Guide, by Daniel Cantore
//Example Code (Oct 2020), with in-depth commenting 

//Initial Definitions and Setup
//Libary Inclusion
#include <Joystick.h>

//Define and Allocate Input Pins to memorable names
#define joyX A0
#define joyButton1 0
//Initializing Axis as Integers, at a 0 default value
int xAxis_ = 0;

//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
 
//Defining the Joystick
//The Joystick is defined in the following setup:
//Joystick(Joystick HID ID, Joystick Type, Button Count, Hat Switch Count, Include X, Include Y, Include Z, Include Rx, Include Ry, Include Rz, Include Rudder, Include Throttle, Include Accelerator, Include Brake, Include Steering
//Joystick HID ID: A Hex value identifier for HID Device Recognition (default: 0x03). DO NOT USE 0x01 or 0x02
//Joystick type: Define the type of joystick from the types supported. Types: DEFAULT Joystick (0x04 or JOYSTICK_TYPE_JOYSTICK), Gamepad (0x05 or JOYSTICK_TYPE_GAMEPAD), Multi-Axis Controller (0x08 or JOYSTICK_TYPE_MULTI_AXIS)
//Button Count: Number of Buttons shown to HID system (default: 32)
//Hat Switch Count: Number of Hat Switches, max 2. (default:2)
//Include X Axis: Determines whether the X axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Y Axis: Determines whether the Y axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Z Axis: Determines whether the Z axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Rx Axis: Determines whether the X Rotational axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Ry Axis: Determines whether the Y Rotational axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Rz Axis: Determines whether the Z Rotational axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Rudder: Determines whether a Rudder axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Throttle: Determines whether a Throttle axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Accelerator: Determines whether an Accelerator axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Brake: Determines whether a Brake axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Steering: Determines whether a Steering axis is avalible for used by the HID system, defined as a bool value (default:true)

Joystick_ Joystick(0x12, JOYSTICK_TYPE_JOYSTICK, 0, 0,true,false,false,false,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);


  //Start Joystick - Needed to start the Joystick function libary
  Joystick.begin();
}

void loop() {

  if (joyButton1 == HIGH){
    loop1();
  }
  if (joyButton1 == LOW){
    loop2();
  }
 

//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

}

void loop1(){
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_);
}

void loop2(){
xAxis_ = 400;
  //Set the Joystick X Axis value as the new, smoother, value
  Joystick.setXAxis(xAxis_);
}

Any help with this would be truly appreciated. I suppose i should clarify- the reason why I need to send a constant value as the output of the axis when the leonardo is an HID, is that in the simulator, when in autothrottle, if the throttles are physically moved, the autothrottle is immediately disengaged. Hence I need to 'fool' the simulator into thinking that the throttles are not moving when the autothrottle switch is engaged.
Thanks
Gareth

should be empty, to allow autothrottle

should be:

  if (digitalRead(joyButton1) == HIGH){
    loop1();
  }
#include <Joystick.h>
const byte  joyX = A0;
const byte joyButton1 = 0;

Joystick_ Joystick(0x12, JOYSTICK_TYPE_JOYSTICK, 0, 0, true, false, false, false, false, false, false, false, false, false, false);

void setup() {
  pinMode(joyButton1, INPUT_PULLUP);
  Joystick.begin();
}

void loop() {
  if (digitalRead(joyButton1))  Joystick.setXAxis(analogRead(joyX));
}

Wow thanks so much I REALLY appreciate the help! I will give it a try a bit later and will report back. I think I was making a stupid mistake of not including the digitalRead part of the code.

Thanks, it works! I can carry on with my project now until I run into the next issue :slight_smile:

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