Here is my code. My issue is is that the keys are getting "pressed" even though there is nothing jumping any of the pins on the Arduino.
// UNO-HIDKeyboard-Library - Version: Latest
// This program has been created by pcs3rd.
// Please do not change this line.
#include <HIDKeyboard.h>
HIDKeyboard keyboard; // Initiate keyboard
// Pin deffanitions and variables...
int key0 = 2; // Button 0 (Unused but optional)
int key1 = 3; // Button 1
int key2 = 4; // Button 2
int key3 = 5; // Button 3
int key4 = 6; // Button 4
int key5 = 7; // Button 5
int key6 = 8; // Button 6
int key7 = 9; // Button 7
int key8 = 10; // Button 8
int led0 = 13; // Indicator light.
int state0 = 0; // Button 0
int state1 = 0; // Button 1
int state2 = 0; // Button 2
int state3 = 0; // Button 3
int state4 = 0; // Button 4
int state5 = 0; // Button 5
int state6 = 0; // Button 6
int state7 = 0; // Button 7
int state8 = 0; // Button 8
void setup() {
keyboard.begin(); // Start keyboard emulation
Serial.begin(9600); // Start Serial Output
delay(300); // Wait to be recognized be PC.
pinMode(key1, INPUT_PULLUP); // set pin mode
pinMode(key2, INPUT_PULLUP); // set pin mode
pinMode(key3, INPUT_PULLUP); // set pin mode
pinMode(key4, INPUT_PULLUP); // set pin mode
pinMode(key5, INPUT_PULLUP); // set pin mode
pinMode(key6, INPUT_PULLUP); // set pin mode
pinMode(key7, INPUT_PULLUP); // set pin mode
pinMode(key8, INPUT_PULLUP); // set pin mode
pinMode(led0, OUTPUT); // set pin mode
Serial.println("Startup Completed!");
}
void loop() {
// write state info to vars
state0 = digitalRead(key0); // Button 0
state1 = digitalRead(key1); // Button 1
state2 = digitalRead(key2); // Button 2
state3 = digitalRead(key3); // Button 3
state4 = digitalRead(key4); // Button 4
state5 = digitalRead(key5); // Button 5
state6 = digitalRead(key6); // Button 6
state7 = digitalRead(key7); // Button 7
state8 = digitalRead(key8); // Button 8
if (state0 == HIGH) { // Button 0
digitalWrite(led0, HIGH);
// keyboard.pressSpecialKey(0x68); (You can enter a scancode instead of 0x68,)
Serial.println("state0");
delay(30);
}
if (state1 == HIGH) { // Button 1
digitalWrite(led0, HIGH);
keyboard.pressSpecialKey(0x68);
Serial.println("state1");
delay(30);
}
if (state2 == HIGH) { // Button 2
digitalWrite(led0, HIGH);
keyboard.pressSpecialKey(0x69);
Serial.println("state2");
delay(30);
}
if (state3 == HIGH) { // Button 3
digitalWrite(led0, HIGH);
keyboard.pressSpecialKey(0x6A);
Serial.println("state3");
delay(30);
}
if (state4 == HIGH) { // Button 4
digitalWrite(led0, HIGH);
keyboard.pressSpecialKey(0x6B);
Serial.println("state4");
delay(30);
}
if (state5 == HIGH) { // Button 5
digitalWrite(led0, HIGH);
keyboard.pressSpecialKey(0x6C);
Serial.println("state5");
delay(30);
}
if (state6 == HIGH) { // Button 6
digitalWrite(led0, HIGH);
keyboard.pressSpecialKey(0x6D);
Serial.println("state6");
delay(30);
}
if (state7 == HIGH) { // Button 7
digitalWrite(led0, HIGH);
keyboard.pressSpecialKey(0x6E);
Serial.println("state7");
delay(30);
}
if (state8 == HIGH) { // Button 8
digitalWrite(led0, HIGH);
keyboard.pressSpecialKey(0x0F);
Serial.println("state8");
delay(30);
}
digitalWrite(led0, LOW);
Serial.println("scan");
delay(30);
}
Yet, even though I have 'if' statements, the code still gets executed, as shown in my monitor:
Startup Completed!
state0
hstate1
istate2
jstate3
kstate4
lstate5
mstate6
nstate7
state8
scan
state0
hstate1
istate2
jstate3
kstate4
lstate5
mstate6
nstate7
state8
scan
Are there any hints to what could be going on? I reformatted the code for readability, but it is pretty much the same as before. Also, I am not using pin 0 or one. I am using 13 for onboard LED.
state1 == HIGH
etc. tostate1 == 1
. Does that solve it? – MichaelT Oct 11 '18 at 23:19