I know this is not timely, but i stumbled on this post while trying to solve the issue of using a digital (four microswitch type) joystick for a similar project. the issue I had was isolating the diagonal values when you use two switches. What I did was assign each button a number then sum the four buttons together to get a single value. Then I can use that vale to trigger the motors.
// Joy Stick Test
#define ju 6 // up Pin, (Pushes lower switch when the joystick is up.
#define jr 7 //Right Pin (jr for joystick right)
#define jl 8 //Left Pin
#define jb 9 //Back Pin
#define LED 13// onboard LED
int u=0;// Variable for up button
int r=0;//Variable for right button
int l=0;//Variable for left button
int b=0;//Variable for back button
int juState=LOW;//Variable for up button state
int jlState=LOW;//Variable for left button state
int jrState=LOW;//Variable for right button state
int jbState=LOW;//Variable for up button state
int Action=0;//Variable for up the sum of the u+r+l+b
int OldAction=0;//Variable to compare changes in the button state
int motion=0;//Variable used to say what the motors will do, the hope is that the motors will run uninterrupted until the old action changes.
void setup()
{
pinMode (ju, INPUT);//up
pinMode (jr, INPUT);//right
pinMode (jl, INPUT);//left
pinMode (jb, INPUT);//back
pinMode (LED, OUTPUT);
Serial.begin(9600);//set serial comunication for 9600
}
void loop(){
//read the button states
juState=digitalRead(ju);
jrState=digitalRead(jr);
jlState=digitalRead(jl);
jbState=digitalRead(jb);
/the joystick button state will return a 1 for high or a 0 for low.
multiply that times the asigned value for the button.
example:if the joystick button is high, then 1x5=5; if it's low then 0x5=0
/
u=(juState5);
r=(jrState1);
l=(jlState7);
b=(jbState3);
Action = (u+r+l+b);//by adding button variable together Action will contain 1 of 9 values(5,1,7,3,12,6,4,10,0)
Serial.println (Action); //Serial print the sum of u+r+l+b
OldAction=Action;//compare changes in the button state
if (OldAction != motion){ //the hope is this will give the stepper motors an uninterrupted variable to run off of until the button state changes.
motion = OldAction;}
if (motion==5){
Serial.println ("Forward");
}
if (motion==6){
Serial.println ("Forward Right");
}
if (motion==12){
Serial.println ("Forward Left");
}
if (motion==3){
Serial.println ("Back");
}
if (motion==4){
Serial.println ("Back Right");
}
if (motion==10){
Serial.println ("Back Left");
}
if (motion==1){
Serial.println ("Righ");
}
if (motion==7){
Serial.println ("Left");
}
if (motion==0){
Serial.println ("Stop");
}
}//End of Void Loop