Pages: [1]   Go Down
Author Topic: Keyboard.press question  (Read 504 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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();}
  
}

Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8471
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
 
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

Logged

Rob Gray aka the GRAYnomad www.robgray.com

Pages: [1]   Go Up
Jump to: