Go Down

Topic: Keyboard problem with Arduino Leonardo (Read 2817 times) previous topic - next topic

hansibull

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:
Code: [Select]


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?
MightyCore -  ATmega1284, mega644, mega324, mega164, mega32, mega16, mega8535
Github.com/MCUdude/MightyCore

MiniCore - ATmega8, mega48, mega88, mega168, mega328
Github.com/MCUdude/MiniCore

HazardsMind

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.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

hansibull

The buttons are just connected to ground, so when a button is pressed digitalRead(0) becomes low
MightyCore -  ATmega1284, mega644, mega324, mega164, mega32, mega16, mega8535
Github.com/MCUdude/MightyCore

MiniCore - ATmega8, mega48, mega88, mega168, mega328
Github.com/MCUdude/MiniCore

HazardsMind

ok so then you dont need if(digitalRead(1) != LOW), you just need to check if at any point it goes LOW.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

hansibull

#4
Dec 20, 2012, 03:54 pm Last Edit: Dec 20, 2012, 03:56 pm by hansibull Reason: 1
The arduino is going to simulate the iCace controller for iPad.
this is the ASCII charaters form it:


Code: [Select]

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 :)
When you release the button, the arduino reads it as HIGH. I cound write if(digitalRead(0) == HIGH), that would give the same result
MightyCore -  ATmega1284, mega644, mega324, mega164, mega32, mega16, mega8535
Github.com/MCUdude/MightyCore

MiniCore - ATmega8, mega48, mega88, mega168, mega328
Github.com/MCUdude/MiniCore

HazardsMind

#5
Dec 20, 2012, 04:13 pm Last Edit: Dec 20, 2012, 04:17 pm by HazardsMind Reason: 1
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.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

hansibull

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?
MightyCore -  ATmega1284, mega644, mega324, mega164, mega32, mega16, mega8535
Github.com/MCUdude/MightyCore

MiniCore - ATmega8, mega48, mega88, mega168, mega328
Github.com/MCUdude/MiniCore

HazardsMind

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.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

hansibull

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
MightyCore -  ATmega1284, mega644, mega324, mega164, mega32, mega16, mega8535
Github.com/MCUdude/MightyCore

MiniCore - ATmega8, mega48, mega88, mega168, mega328
Github.com/MCUdude/MiniCore

HazardsMind

My GitHub:
https://github.com/AndrewMascolo?tab=repositories

cfishy

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.

allenhuffman


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:

http://appleause.com/category/ios/icade/

I am not done with the project, but it may give you some ideas.
Embedded Software Engineer
UNO | Leonardo | Due | Teensy | Raspberry Pi | BASIC Stamp | OS-9 RTOS
http://www.subethasoftware.com - Established in 1990.

Go Up