Hi there
So, I have searched through the forums and found a few other with the same question as me (I think), but I might have an additional issue I hope someone may have an input to.
Basically, I have built a surprisingly well functioning flight stick. One of the buttons however is a SPDT on-on toggle switch with one leg to pin 14, one to pin 15, and the last to ground. I then do pullup and the switch works as it is meant to, i.e., in one of its two possible positions 14 is always low and 15 high, and in the other position it is reversed.
Now, as this is a flight stick it would need to register the position it is pushed into as a single push and release, in contrast to the constant push it is currently doing. I regret that I have as of yet not figured it out, despite tips form the existing forum posts.
I have seen suggestions hinting at state change and using millis, but I am having difficulties making it work. It might be due to the joystick library used, which can be found here Arduino Joystick Library.
My code is as follows, please bear with the toggle switch parts, as this a little nonsense written in my frustration.
Note that the Joystick.releaseButton(10); is for releasing pin14 which does appear to work, but digitalWrite does not? odd.
#include "Joystick.h"
#define X_PIN A2
#define Y_PIN A3
#define X_MIN 0
#define X_MAX 1023
#define X_TRIM 89
#define X_INVERT 1
#define Y_MIN 0
#define Y_MAX 1023
#define Y_TRIM 12
#define Y_INVERT -1
// joystick characteristics (numer of buttons, hats, throttle etc.)
Joystick_ Joystick(0x04,JOYSTICK_TYPE_JOYSTICK,
14, 0,
true, true, false,
false, false, false,
false, false,
false, false, false);
// toggle switch part
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
unsigned long previousMillis = 0;
const long interval = 500;
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);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(16, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP);
pinMode(18, INPUT_PULLUP);
pinMode(19, INPUT_PULLUP);
Joystick.begin(false);
Joystick.setXAxisRange(-512, 512);
Joystick.setYAxisRange(-512, 512);
Joystick.setRudderRange(-512, 512);
Joystick.setThrottleRange(-512, 512);
}
void loop() {
// set joystick buttons
Joystick.setButton(0, !digitalRead(10));
Joystick.setButton(1, !digitalRead(4));
Joystick.setButton(2, !digitalRead(8));
Joystick.setButton(3, !digitalRead(2));
Joystick.setButton(4, !digitalRead(7));
Joystick.setButton(5, !digitalRead(5));
Joystick.setButton(6, !digitalRead(3));
Joystick.setButton(7, !digitalRead(6));
Joystick.setButton(8, !digitalRead(9));
Joystick.setButton(9, !digitalRead(16));
Joystick.setButton(10, !digitalRead(14));
Joystick.setButton(11, !digitalRead(15));
Joystick.setButton(12, !digitalRead(18));
Joystick.setButton(13, !digitalRead(19));
//----------- toggle switch part ------------------------------------------//
unsigned long currentMillis = millis();
buttonState = digitalRead(14);
if (buttonState != lastButtonState) {
if (buttonState == HIGH && currentMillis - previousMillis >= interval) {
buttonPushCounter++;
Joystick.pressButton(10);
} else {
Joystick.releaseButton(10);
}
delay(50);
}
lastButtonState = buttonState;
//----------- toggle switch part end ---------------------------------------//
// read axis, adjust for trim and send state
int value = map(analogRead(X_PIN) + X_TRIM , X_MIN, X_MAX, -512, 512) * X_INVERT;
Joystick.setXAxis(value);
value = map(analogRead(Y_PIN) + Y_TRIM , Y_MIN, Y_MAX, -512, 512) *Y_INVERT;
Joystick.setYAxis(value);
Joystick.sendState();
delay(10);
}
I hope there is something to go on for a bright mind, otherwise I am more than happy to provide more info for a little hint and/or help.
-Spaziba