Norway
Offline
Full Member
Karma: 1
Posts: 107
total n00b
|
 |
« on: December 20, 2012, 09:10:07 am » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 29
Posts: 1586
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #1 on: December 20, 2012, 09:40:56 am » |
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.
|
|
|
|
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Norway
Offline
Full Member
Karma: 1
Posts: 107
total n00b
|
 |
« Reply #2 on: December 20, 2012, 09:44:25 am » |
The buttons are just connected to ground, so when a button is pressed digitalRead(0) becomes low
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 29
Posts: 1586
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #3 on: December 20, 2012, 09:46:34 am » |
ok so then you dont need if(digitalRead(1) != LOW), you just need to check if at any point it goes LOW.
|
|
|
|
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Norway
Offline
Full Member
Karma: 1
Posts: 107
total n00b
|
 |
« Reply #4 on: December 20, 2012, 09:54:32 am » |
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  When you release the button, the arduino reads it as HIGH. I cound write if(digitalRead(0) == HIGH), that would give the same result
|
|
|
|
« Last Edit: December 20, 2012, 09:56:09 am by hansibull »
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 29
Posts: 1586
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #5 on: December 20, 2012, 10:13:36 am » |
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.
|
|
|
|
« Last Edit: December 20, 2012, 10:17:21 am by HazardsMind »
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Norway
Offline
Full Member
Karma: 1
Posts: 107
total n00b
|
 |
« Reply #6 on: December 20, 2012, 10:23:44 am » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 29
Posts: 1586
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #7 on: December 20, 2012, 10:30:01 am » |
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.
|
|
|
|
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Norway
Offline
Full Member
Karma: 1
Posts: 107
total n00b
|
 |
« Reply #8 on: December 20, 2012, 12:44:29 pm » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 29
Posts: 1586
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #9 on: December 20, 2012, 12:47:00 pm » |
yea, thats a debounce issue.
|
|
|
|
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 2
|
 |
« Reply #10 on: December 29, 2012, 08:24:04 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Des Moines, Iowa, USA
Offline
Newbie
Karma: 0
Posts: 27
|
 |
« Reply #11 on: February 13, 2013, 12:31:58 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
|