I'm using arduino uno in order to experiment with two keys keyboard.
Say that in order to write letter A I use:
Serial.write(00400000,8);
And for B I use
Serial.write(00500000,8);
I wrote a code that allows me to to use both keys and even hold them and everything works fine, the problem is that whenever I hold both of them, it goes crazy and instead of holding both keys it presses both of them really fast. I assume that it is because the loop always repeats those keys back and fourth. I tried to only Serial.write one time and release the button only when the keypress is over, but it is considered as one press. What can I do?
In case that you hate yourself, heres the code:>
//Array that holds 8 bits that needs to be sent
uint8_t buf[8] = {0};
//Array that holds inofrmation about which keys are pressed (true == pressed)
bool keyPressed[2];
#define Pin_A 13
#define Pin_B 12void setup(){
Serial.begin(9600);
pinMode(Pin_A, INPUT);
pinMode(Pin_B, INPUT);
keyPressed[0] = false;
keyPressed[1] = false;
}void loop() {
// A key
if(keyPressed[0] == false)
{
if(digitalRead(Pin_A) == LOW)
{
buf[2] = 4;
keyPressed[0] = true;
Serial.write(buf,8);
}else
{
// do nothing
}}
else
{
if(digitalRead(Pin_A) == HIGH)
{
keyRelease(0);
}
else{
buf[2] = 4;
Serial.write(buf,8);;
}
}//B key
if(keyPressed[1] == false)
{
if(digitalRead(Pin_B) == LOW)
{
buf[2] = 5;
keyPressed[1] = true;
Serial.write(buf,8);
}else
{
// do nothing
}}
else
{
if(digitalRead(Pin_B) == HIGH)
{
keyRelease(1);
}
else{
buf[2] = 5;
Serial.write(buf,8);;
}
}}
//release key numbered x in keypressed array
void keyRelease(int x){
keyPressed[x] = false;
buf[0] = 0;
buf[2] = 0;
Serial.write(buf,8);
}
