Define end range of a potentiometer as a switch

Hello everybody,

I'am new to Arduino and starting a project to build a throttle controller for flight simulators and would like to program a defined end range of a potentiometer as switch. This should then activate the thrust reverser in the simulator.

Let's say, when I pull the lever to the idle position, there should be 10% left of the poti (the lever should snap in there). If I pull the lever further down an additional switch should then be activated, until I pull the lever up to idle again.

If you know the Saitek Throttle Quadrant, you know what I mean.

Is it possible to program something like this? I would be very grateful for your help.

Best Regards,
Marcell

For any significant help, you need to supply the value of the potentiometer, a schematic of how you have it hooked up and the voltage being applied.

Paul

If it helps I attach the ino file i'am playing around with to learn.
The test potentiometer is at A0.

Within a certain analogue value, switch D8, for example, should be triggered, which the PC recognises as a pressed button. How can I do that?

Arduino_throttle_v3.ino (4.84 KB)

Do you know what "digitalWrite() does?

Paul

I think digitalWrite() is to make LEDs blinking at the pinout or for other stuff. I want to trigger a button in Windows gamecontrol within a certain pot range value. How can I do this?

You will need one of the Arduinos that can send keystrokes to the PC e.g. Leonardo.

I'am playing with the Leonardo Pro Micro.

If I understand correctly what you want to do, you need to use an if statement, like this:

if (throttlePosition < 10) { //10% asks if the position of the throttle is greater than 10%
  output = 1; //high, sends command to computer, whatever
}

EDIT:

if (throttlePosition < 10) { //10% asks if the position of the throttle is greater than 10%
  output = 1; //high, sends command to computer, whatever
} else {
  output = 0; //low
}

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks. Tom... :slight_smile:

Sorry, fixed.

Thank you micahfriesen,
I tried with if statements yesterday. It works somehow and the switch is triggered in the lower range as it should. If I turn the poti back to the other direction the button flickers all the time. Something is permanently overwriting the status. I suppose it has something to do with the button states in the loop. How can I change this?

// Requires Arduino Joystick Library https://github.com/MHeironimus/ArduinoJoystickLibrary
#include <Joystick.h>

//JOYSTICK SETTINGS
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
  JOYSTICK_TYPE_JOYSTICK,
  8, //number of buttons
  0, //number of hat switches
  //Set as many axis to "true" as you have potentiometers for
  false, // y axis
  false, // x axis
  true, // z axis
  true, // rx axis
  false, // ry axis
  false, // rz axis
  false, // rudder
  false, // throttle
  false, // accelerator
  false, // brake
  false); // steering wheel

 
// put the max and min values from the analogRead in these arrays
// these are translated to a range of 0 - 1023
int axisLimits0[] = {170+70, 878};
int axisLimits1[] = {111, 844-70};
int axisLimits2[] = {0, 1023};
int axisLimits3[] = {0, 1023};

// turn axes on or off by setting these variables
bool a0Used = true;
bool a1Used = true;
bool a2Used = false;
bool a3Used = false;

// setting mode prints the pin value and translated value to the serial monitor
// int setting = -1; // no printing to the serial monitor
// int setting = 2; // values 0 - 3, print the pin values to the serial monitor
int setting = -1;

int currentButtonState0;
int lastButtonState0;
int currentButtonState1;
int lastButtonState1;
int currentButtonState2;
int lastButtonState2;
int currentButtonState3;
int lastButtonState3;
int currentButtonState4;
int lastButtonState4;
int currentButtonState5;
int lastButtonState5;
int currentButtonState6;
int lastButtonState6;

void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP); 
  if(a0Used) pinMode(A0, INPUT_PULLUP);
  if(a1Used) pinMode(A1, INPUT_PULLUP);
  if(a2Used) pinMode(A2, INPUT_PULLUP);
  if(a3Used) pinMode(A3, INPUT_PULLUP);
  

// Initialize Joystick Library
  Joystick.begin();
  Joystick.setZAxisRange(0, 1024); 
  Joystick.setRxAxisRange(1024, 0);
  
  if(setting >= 0) Serial.begin(9600);

}

void loop() {

  int value = 0;
  int pos = 0;

  if(a0Used){
    value = analogRead(A0);
    pos = translateValue(value, axisLimits0[0], axisLimits0[1]);
    Joystick.setZAxis(pos);
    if(setting == 0) settingPrint(value, pos);
  }
  
  if(a1Used){
    value = analogRead(A1);
    pos = translateValue(value, axisLimits1[0], axisLimits1[1]);
    Joystick.setRxAxis(pos);
    if(setting == 1) settingPrint(value, pos);
  }

  if(a2Used){
    value = analogRead(A2);
    pos = translateValue(value, axisLimits2[0], axisLimits2[1]);
    Joystick.setRyAxis(pos);
    if(setting == 2) settingPrint(value, pos);
  }
  
  if(a3Used){
    value = analogRead(A3);
    pos = translateValue(value, axisLimits3[0], axisLimits3[1]);
    Joystick.setRzAxis(pos);
    if(setting == 3) settingPrint(value, pos);
  }
if (analogRead(A0) < 200) { //10% asks if the position of the throttle is greater than 10%
  pinMode(8, HIGH); //high, sends command to computer, whatever
} else {
  pinMode(8, LOW); //low
}

// Read Switches
int currentButtonState0 = !digitalRead(2); // Button 0
  if (currentButtonState0 != lastButtonState0)
  {
  Joystick.setButton(0, currentButtonState0);
  lastButtonState0 = currentButtonState0;
  }

int currentButtonState1 = !digitalRead(3); // Button 1
  if (currentButtonState1 != lastButtonState1)
  {
  Joystick.setButton(1, currentButtonState1);
  lastButtonState1 = currentButtonState1;
  }
  
int currentButtonState2 = !digitalRead(4); // Button 2
  if (currentButtonState2 != lastButtonState2)
  {
  Joystick.setButton(2, currentButtonState2);
  lastButtonState2 = currentButtonState2;
  }

int currentButtonState3 = !digitalRead(5); // Button 3
  if (currentButtonState3 != lastButtonState3)
  {
  Joystick.setButton(3, currentButtonState3);
  lastButtonState3 = currentButtonState3;
  }
  
int currentButtonState4 = !digitalRead(6); // Button 4
  if (currentButtonState4 != lastButtonState4)
  {
  Joystick.setButton(4, currentButtonState4);
  lastButtonState4 = currentButtonState4;
  } 

int currentButtonState5 = !digitalRead(7); // Button 5
  if (currentButtonState5 != lastButtonState5)
  {
  Joystick.setButton(5, currentButtonState5);
  lastButtonState5 = currentButtonState5;
  } 
  
int currentButtonState6 = !digitalRead(8); // Button 6
  if (currentButtonState6 != lastButtonState6)
  {
  Joystick.setButton(6, currentButtonState6);
  lastButtonState6 = currentButtonState6;
  }  

  delay(5);
}

int translateValue(int v, int f1, int f2){
  // translates values to a 0 - 1023 range
  int result = 0;
  int start = 0;
  float range = 0;
  
  if(f1 < f2){
    start = f1;
    range = f2 - f1;
  }
  else{
    start = f2;
    range = f1 - f2;
  }
  
  result = (v - start) * (1023 / range);

  if(result < 0) result = 0;
  if(result > 1023) result = 1023;
  
  return result;
}

void settingPrint(int value, int pos){
  Serial.print(value); 
  Serial.print(" "); 
  Serial.println(pos);
}

Hey I got it! Now it works.
I set this:

if (analogRead(A0) < 200) { 
  Joystick.setButton(6, HIGH); 
} else {
  Joystick.setButton(6, LOW); 
}

and deactivated that:

//int currentButtonState6 = !digitalRead(8); // Button 6
//  if (currentButtonState6 != lastButtonState6)
//  {
//  Joystick.setButton(6, currentButtonState6);
//  lastButtonState6 = currentButtonState6;
//  }

Learning Arduino is a lot of fun! :slight_smile: Thanks for your help!