Hello, I am new to arduino and I am in a process of making a simple custom keyboard, which consists of four keys -WAD and V. I intend to use it to control a game I am making in Unity. This is a tutorial which I have followed:
https://www.arduino.cc/education/diy-game-controllers
there are however two problems which I have partly solved:
- simulate keyboards hold key function
- out of the box from this tutorial the keys after press were bursting about 10-20 characters at one press, making unity overhelmed. Also when I pressed it for longer periods, the characters were being printed even after I released the key. So I introduced to my code While function, which prints keys in delays only after first character is printed immediately. This works fine for walking the Unity character, but when I need the button to be on hold, there is some problem and it does not work
- simultaneous presses
I dont understand why this code does not allow to have two keys pressed at same time - in my unity game I want to go forward while turning left or right.
code here:
#include <Keyboard.h>
// Emulating keyboard or mouse functions only work on 32u4 or SAMD micro-based boards.
void setup() {
//Start serial connection
Serial.begin(9600);
//Configure pins as an input and enable the internal pull-up resistor,
pinMode(2, INPUT_PULLUP); //a
pinMode(3, INPUT_PULLUP); //w
pinMode(4, INPUT_PULLUP); //v
pinMode(5, INPUT_PULLUP); //d
pinMode(6, INPUT_PULLUP); //killswitch
Keyboard.begin();
}
boolean pressed = false;
int delaytime = 10;
int initialdelaytime = 150;
int d = 100;
boolean pressedD = false;
boolean holdD = false;
int w = 119;
boolean pressedW = false;
boolean holdW = false;
int a = 97;
boolean pressedA = false;
boolean holdA = false;
int v = 118;
boolean pressedV = false;
boolean holdV = false;
void loop() {
//If corresponding button is pressed
if (digitalRead(6) == LOW) {
pressed = true;
}
if(pressed == true)
{
//KEY a
if (digitalRead(2) == LOW && pressedA == false)
{
pressedA=true;
//Send an ASCII 'A',
Keyboard.write(a);
delay(initialdelaytime);
holdA = true;
}
while(digitalRead(2) == LOW && holdA == true)
{
Keyboard.write(a);
delay(delaytime);
}
if(digitalRead(2) == HIGH)
{
holdA = false;
pressedA=false;
}
//KEY w
if (digitalRead(3) == LOW && pressedW == false)
{
pressedW=true;
//Send an ASCII 'W',
Keyboard.write(w);
delay(initialdelaytime);
holdW = true;
}
while(digitalRead(3) == LOW && holdW == true)
{
Keyboard.write(w);
delay(delaytime);
}
if(digitalRead(3) == HIGH)
{
holdW = false;
pressedW=false;
}
//KEY v
if (digitalRead(4) == LOW && pressedV == false)
{
pressedV=true;
//Send an ASCII 'V',
Keyboard.write(v);
delay(initialdelaytime);
holdV = true;
}
while(digitalRead(4) == LOW && holdV == true)
{
Keyboard.write(v);
delay(0);
}
if(digitalRead(4) == HIGH)
{
holdV = false;
pressedV=false;
}
//KEY d
if (digitalRead(5) == LOW && pressedD == false)
{
pressedD=true;
//Send an ASCII 'D',
Keyboard.write(d);
delay(initialdelaytime);
holdD = true;
}
while(digitalRead(5) == LOW && holdD == true)
{
Keyboard.write(d);
delay(delaytime);
}
if(digitalRead(5) == HIGH)
{
holdD = false;
pressedD=false;
}
}
else {
Keyboard.releaseAll();
}
}
Thanks in advance for reading and help