Go Down

Topic: Keyboard.press question (Read 598 times) previous topic - next topic

tangus

Ok, I have looked around the forum (as well as the rest of the internet), and can not figure this out..
  I am using a 4 switch joystick to control 4 buttons on a keyboard.   I have it work when only one direction is activated, however when 2 switches are closed, only one key is "pressed". Basically, I want to press both keys at the same time.  Here is my code so far:
Quote

int jyuVal = 0;
int jylVal = 0;
int jyrVal = 0;
int jydVal = 0;
int swaVal = 0;
int swbVal = 0;
int swcVal = 0;
void setup() {
  Keyboard.begin();
  pinMode(13, INPUT);// JOY U? PIN
  pinMode(12, INPUT);// JOY L? PIN
  pinMode(11, INPUT);// JOY R? PIN
  pinMode(10, INPUT);// JOY D? PIN
  pinMode(9, OUTPUT);// LED IND PIN
  digitalWrite(13, HIGH);// make all high for pull up
  digitalWrite(12, HIGH);
  digitalWrite(11, HIGH);
  digitalWrite(10, HIGH);
  digitalWrite(9, HIGH);

}

void loop() {
 jyuVal= digitalRead(12);
 jyrVal= digitalRead(13);
 jylVal= digitalRead(11);
 jydVal= digitalRead(10);
 if(jyuVal == 0 && jylVal == 1 && jyrVal == 1 && jydVal == 1){
  Keyboard.press('w');}
 if(jyrVal == 0 && jyuVal == 1 && jylVal == 1 && jydVal == 1){
  Keyboard.press('d');}
 if(jylVal == 0 && jyrVal == 1 && jyuVal == 1 && jydVal == 1){
  Keyboard.press('a');}
 if(jydVal == 0 && jylVal == 1 && jyrVal == 1 && jyuVal == 1){
  Keyboard.press('s');}
 if(jydVal == 1 && jyrVal == 1 && jylVal == 1 && jyuVal == 1){
  Keyboard.releaseAll();}
  
}


Graynomad

I would accumulate all the 4 jyxVal into a single value then use that value to index into an array of chars to use, something like this

Code: [Select]
 
char keys[16] = {'w', 'd', ...}; // as appropriate for each of the 16 combinations
 
keyVal = digitalRead(12);
keyVal |= digitalRead(13) << 1;
keyVal |= digitalRead(11) << 2;
keyVal |= digitalRead(10) << 3;

if (keyVal)
    Keyboard.press(keys[keyVal]);


_____
Rob

Rob Gray aka the GRAYnomad www.robgray.com

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy