Arduino newbie - coding buttons as keyboard.press input

Hi all

I'm currently trying to code some Keyboard.press commands so I can create a simple prototype gaming controller for a uni project. Using a Leonardo as recommended by you folk! basically I managed to modify a bit of example code to get one button working so the W key was used to move the character forward but i'm having trouble getting the code working for additional buttons thereafter.

When i tried to add a second button (S key) the second button doesn't do anything and the first button operates the W and S keys so WS is printed in textedit when the first button is press (i'm using a Mac)

This is the current code

// use this option for OSX:
char ctrlKey = KEY_LEFT_GUI;
// use this option for Windows and Linux:
// char ctrlKey = KEY_LEFT_CTRL;

void setup() {
// make pin 2 an input and turn on the
// pullup resistor so it goes high unless
// connected to ground:
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
while (digitalRead(2) == LOW);
while (digitalRead(3) == LOW);
// initialize control over the keyboard:
Keyboard.begin();

}

void loop() {
while (digitalRead(2) == LOW) {
// do nothing until pin 2 goes low
delay(500);
}
{
Keyboard.press('w');
delay(100);
Keyboard.release('w');
delay(10); }
{
while (digitalRead(3) == LOW)
delay(500);
}
{
Keyboard.press('s');
delay(100);
Keyboard.release('s');
delay(10); }
}

I'm just looking for someone to tell me where i'm going wrong so that I can program the second button. As soon as I can get the second button working and find out where I'm going wrong i can program the remaining 8 buttons I need.

Any help would be greatly appreciated

Many thanks

Mitch

I like the use of Keyboard.press() and Keyboard.release(). It gives far more control that just the Keyboard.write().

I see four "while" loops. Why ? and so many delay().

Could you explain what you are trying to do ? Is there a special reason to wait for the buttons in the setup() ?
Are the buttons active when LOW ? Do the buttons bounce ? Do you want to keep the emulated keyboard button down as long as the real button is active ?

I think you can remove all the "while" and "delay". Detect a change in the button (by remembering its previous state) and send a Keyboard.press() or Keyboard.release() accordingly.

Peter_n:
I like the use of Keyboard.press() and Keyboard.release(). It gives far more control that just the Keyboard.write().

I see four "while" loops. Why ? and so many delay().

Could you explain what you are trying to do ? Is there a special reason to wait for the buttons in the setup() ?
Are the buttons active when LOW ? Do the buttons bounce ? Do you want to keep the emulated keyboard button down as long as the real button is active ?

I think you can remove all the "while" and "delay". Detect a change in the button (by remembering its previous state) and send a Keyboard.press() or Keyboard.release() accordingly.

It was just an example code I used. I've taken out the delays in the code and it still works as normal as it did before.

The code works so when I press the button it acts as if the W key is being pressed on the keyboard so it only recognises a button press when the button is pushed down. Essentially I want to do this for a number of keyboard of keys. Just imagine that the arduino leonardo is another external keyboard just with a set amount of buttons.
I've got one button working but I've having trouble getting additional buttons set up

I have it set up so each button goes to a different pin and each button is connected to ground by a 10k resistor.

The end goal is to control a PC game as an example of a gaming controller I want to create.

Below is the code without the delays and with one button working properly:

void setup() {
// make pin 2 an input and turn on the
// pullup resistor so it goes high
// connected to ground:
pinMode(2, INPUT_PULLUP);
// initialize control over the keyboard:
Keyboard.begin();
}

void loop() {
while(digitalRead(2) == LOW);
Keyboard.press('w');
Keyboard.release('w');
}

The next step is to add more buttons but I'm having trouble doing so.

void loop() {
 while(digitalRead(2) == LOW); 
 Keyboard.press('w');
 Keyboard.release('w');
}

The way this is written NOTHING happens while digitalRead(2) is low - that is the effect of the ; at the end of the line.

I suspect it should be

while (digitalRead(2) == LOW) {
     Keyboard.press('w');
     Keyboard.release('w');
}

And then other pins could be used in like manner to send other keystrokes

However this will repeat extremely quickly - hundreds or thousands of times per second. A crude solution would be to add delay(100); which would limit it to 5 repeats per second.

Keep in mind that using WHILE will prevent another pin being detected until pin2 is returned HIGH.

You might get some useful ideas from several things at a time.

...R

I had many questions, and now I have even more.
Is the button connected to 5V ? and the 10k resistor to GND ?

Mitchella91, can you read about the if-statement an the while-loop ?
A ';' (semicolon) on its own means : do nothing.
This code : while ( digitalRead ( 2 ) == LOW ) ;
reads as : As long as the digital input pin 2 is low, do nothing.

Robin2, You are making an auto-fire with the 'w' ?
It think that is something the computer should do.
For example, World Of Tanks wants to have the 'w' pressed as long as I keep the button to my Leonardo pressed.
Button pressed : a single Keyboard.press('w');
Button released : a single Keyboard.release('w');

Peter_n:
Robin2, You are making an auto-fire with the 'w' ?
It think that is something the computer should do.
For example, World Of Tanks wants to have the 'w' pressed as long as I keep the button to my Leonardo pressed.
Button pressed : a single Keyboard.press('w');
Button released : a single Keyboard.release('w');

I had assumed from the OPs code that it needed a series of key-down / key-up pairs.

If it recognizes separate key-down and key-up that would make things simpler. But then there is no need for a WHILE at all.

...R