keystroke depending on previous position of lever switch

Hi guys, Im pretty new to arduino, but I know the basics. Im a train guy, so now im working on project (using leonardo) where im going to have a box with some levers and buttons, that can be plugged into a PC and used for driving for example in train simulator. Today, I spent 2 hours thinking of way to have a 3-position lever switch, That can send one long keystroke, about 500ms, and the key will depend on the previous position of the lever (only 2 different keys). That lever will be used for switching direction - forward, neutral and backward. The main problem is, that there is no key for straightly neutral, only way to get to neutral is by hitting S when in forward or hitting W when in backward, and if you hit it once more, it will switch into the opposite direction. Thanks in advance, Andrew. PS: Im able to obtain 6 pin lever, and 3 pin too, so tell me witch is going to be simpler and send me some example code. Thanks

While you are thinking about it, how about actually wiring it up and making a drawing so we have an idea of what you are thinking. Then how are you thinking of attaching it to Arduino pins. Then you can consider writing some code to make it work.

Paul

presumably you have a way to read one of three values from the lever which can be stored in a variable.

if so, keep track of the current and previous state of the lever and compare both to determine which keystroke to send. And then update the previous lever state variable with the current one.

for eaxmple, if current val is neutral and prevVal was forward, send appropriate keystroke

I think that is the word ive been looking for! A variable! Btw tried some sketch, I actually forgot my leo in school so using uno and serial print, and cant find any lever, so temporary using joystick. but I still keep getting error: label PrevState used but not defined. My code that does not work so far:

``````// Arduino pin numbers
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
int PrevState = 0;

void setup() {
Serial.begin(9600);
pinMode(Y_pin, INPUT);
PrevState = 2;
}

void loop() {
PrevState = 2;
if (analogRead(1) < 519 && analogRead(1) > 501) && PrevState == 1; {  //THE ERROR GETS KNOCKED BY THIS LINE
delay(100);
Serial.print("PRESSING SHORT W");
PrevState = 1; //SET IT TO NEUTRAL
}
if (analogRead(1) < 519 && analogRead(1) > 501) && PrevState == 3; {  //AND THIS ONE TOO
delay(100);
Serial.print("PRESSING SHORT S");
PrevState = 2; //SET IT TO NEUTRAL
}
delay(100);
Serial.print("PRESSING LONG W");
PrevState = 3; //SET IT TO FW
}
delay(100);
Serial.print("PRESSING LONG S");
PrevState = 1; //SET IT TO BW
}

}
``````

since you’re using analogRead(), it looks like the lever is a potentiometer

if so, determine a state by comparing the value to ranges starting at one end. Then independently check the state and prevState to determine what to do

``````#define M   1
#define S   2
#define L   3

if (520 < val)
state = S;
else if (500 < val)
state = M;
else
state = W;

if (M == state && L == prevState)  {
;
}

else if (M == state && S == prevState)  {
;
}

else if (S == state) {
;
}

else if (L == state)  {
;
}

prevState = state;
``````

Hi, I mentioned earlier that I'm using a joystick temporarily, for testing, cuz I don't have any lever right now, but in the final solution, there will be a 3-position lever. 3 pin, there will be 100% digital signal(s). So if the lever is up, it connects digital pin 2 with GND, when in middle, nothing will be connected and when backwards, 3 and GND connects. Also as I said I'm a beginer so you can comment out the code a little. Thanks.

in the code i posted, there are two parts: one parts determines the state of the device and the 2nd part figures out what keystroke to send depending on the state and prior state.

so you'll need to replace the first part when you replace your joystick

Hi, my code is partly working now, but it keeps repeating the if. As I mentioned earlier, I just need short or long press some key once. Also you can replace it with digital pin 2 and 3. Thanks.

maybe you need to check for a change

``````    else if (S == state && state != prevState) {
``````

Thank you very much, this code did the trick for me:

``````#define M   1
#define S   2
#define W   3
int state ;
int prevState ;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);

}

void loop() {
// put your main code here, to run repeatedly:

if (520 < val)
state = S;
else if (500 > val)
state = W;
else
state = M;

if (M == state && W == prevState && state != prevState)  {
Serial.print("PRESSING SHORT S  ")
;
}

else if (M == state && S == prevState && state != prevState)  {
Serial.print("PRESSING SHORT W  "  )
;
}

else if (S == state && state != prevState) {
Serial.print("PRESSING LONG S  ")
;
}

else if (W == state && state != prevState) {
Serial.print("PRESSING LONG W  ")
;
}

prevState = state;

}
``````