Keyboard problem with Arduino Leonardo

Hi!
I'm trying to make an arcade controller for iPad (iCade controller) with my arduino Leonardo, but I some probelms.

when I push a button, the arduino will send a character, and when I release the button, the arduino will send a different character.

my code looks like this:

void setup() {
  pinMode(0, INPUT_PULLUP); 
  pinMode(1, INPUT_PULLUP); 
  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(11, INPUT_PULLUP);
  
  Keyboard.begin();
}

void loop() {
  if(digitalRead(0) == LOW)                     //UP
  { Keyboard.press('w'); }
    else(Keyboard.release('w'));
    if(digitalRead(0) != LOW)
    { Keyboard.press('e'); }    
    else(Keyboard.release('e'));

  if(digitalRead(1) == LOW)                      //DOWN
  { Keyboard.press('x'); }
    else(Keyboard.release('x'));
    if(digitalRead(1) != LOW)
    { Keyboard.press('z'); }    
    else(Keyboard.release('z'));


  if(digitalRead(2) == LOW)                      //LEFT
  { Keyboard.press('a'); }
    else(Keyboard.release('a'));
    if(digitalRead(2) != LOW)
    { Keyboard.press('q'); }    
    else(Keyboard.release('q'));

  if(digitalRead(3) == LOW)                      //RIGHT
  { Keyboard.press('d'); }
    else(Keyboard.release('d'));
    if(digitalRead(3) != LOW)
    { Keyboard.press('c'); }    
    else(Keyboard.release('c'));

  if(digitalRead(4) == LOW)                      //A
  { Keyboard.press('y'); }
    else(Keyboard.release('y'));
    if(digitalRead(4) != LOW)
    { Keyboard.press('t'); }    
    else(Keyboard.release('t'));
    
    if(digitalRead(5) == LOW)                      //B
  { Keyboard.press('h'); }
    else(Keyboard.release('h'));
    if(digitalRead(5) != LOW)
    { Keyboard.press('r'); }    
    else(Keyboard.release('r'));    
    
     if(digitalRead(6) == LOW)                      //C
  { Keyboard.press('u'); }
    else(Keyboard.release('u'));
    if(digitalRead(6) != LOW)
    { Keyboard.press('f'); }    
    else(Keyboard.release('f'));    
    
    if(digitalRead(7) == LOW)                    //D
  { Keyboard.press('j'); }
    else(Keyboard.release('j'));
    if(digitalRead(7) != LOW)
    { Keyboard.press('n'); }    
    else(Keyboard.release('n'));        

    if(digitalRead(8) == LOW)                    //E
  { Keyboard.press('i'); }
    else(Keyboard.release('i'));
    if(digitalRead(8) != LOW)
    { Keyboard.press('m'); }    
    else(Keyboard.release('m'));    
    
    if(digitalRead(9) == LOW)                    //F
  { Keyboard.press('k'); }
    else(Keyboard.release('k'));
    if(digitalRead(9) != LOW)
    { Keyboard.press('p'); }    
    else(Keyboard.release('p'));       

    if(digitalRead(10) == LOW)                   //G
  { Keyboard.press('o'); }
    else(Keyboard.release('o'));
    if(digitalRead(10) != LOW)
    { Keyboard.press('g'); }    
    else(Keyboard.release('g'));     
   
    if(digitalRead(11) == LOW)                   //H
  { Keyboard.press('l'); }
    else(Keyboard.release('l'));
    if(digitalRead(11) != LOW)
    { Keyboard.press('y'); }    
    else(Keyboard.release('y'));  
    
    }

This code worked flawlessly with only six buttons in the code (including //B), but now, with 12 buttons, the arduino don't respont on button presses and sometimes output wrong caracters.
Whats wrong? can it be RAM limitations?

Quick question, why do you need "digitalRead(0) == LOW" if all your inputs are pulled high?

Also you dont need the extra () in "else ( Keyboard.release('w') ) ;" just do this "else Keyboard.release('w') ;"

It might just be the timing, because the loop it is going throught a lot of IF statements, use if, else If, else for up/down, and left/ right, they dont need to be separate statements. It could make you processing a little faster.

The buttons are just connected to ground, so when a button is pressed digitalRead(0) becomes low

ok so then you dont need if(digitalRead(1) != LOW), you just need to check if at any point it goes LOW.

The arduino is going to simulate the iCace controller for iPad.
this is the ASCII charaters form it:

iCade ASCII:
UP ON,OFF  = w,e  
RT ON,OFF  = d,c  
DN ON,OFF  = x,z  
LT ON,OFF  = a,q  
A  ON,OFF  = y,t  
B  ON,OFF  = h,r  
C  ON,OFF  = u,f  
D  ON,OFF  = j,n  
E  ON,OFF  = i,m  
F  ON,OFF  = k,p  
G  ON,OFF  = o,g  
H  ON,OFF  = l,v

the reason whi I have if(digitalRead(0) != LOW) is because I want the arduino to print a different character when you release the button :slight_smile:
When you release the button, the arduino reads it as HIGH. I cound write if(digitalRead(0) == HIGH), that would give the same result

hmm, ok could you maybe post the errors your getting, like a picture or screenshot?

WAIT, I think you have maybe a debounce issue. You have more code to process so your read time might not be good enough, to compensate for all the button presses.

Ok, so what shoul I do? does that means this project is too heavy to do for an Arduino Leoardo, or is there any way you can go around it?

Look into the debouncing example and try it out.

But first, see exactly at what point you get those issues and comment out a button one at a time to see your current limits. Then adjust it with either the debounce or a simple delay.

most of the time none of the buttons respond. but if I hit many buttons, it may be able to send out a single character

yea, thats a debounce issue.

Keypad library now has debounce and, from what i can tell, edge detection of some sort, too. Not quite documented but you can look at the multi-key example included in the lib.

hansibull:
Hi!
I'm trying to make an arcade controller for iPad (iCade controller) with my arduino Leonardo, but I some probelms.

If it helps, I have code I wrote for a Teensy 2.0 using the Arduino IDE that contains debounce code, etc. It also works on Arduino. If it is of help, you may find it here:

I am not done with the project, but it may give you some ideas.