0

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.

  • Change state1 == HIGH etc. to state1 == 1. Does that solve it? – MichaelT Oct 11 '18 at 23:19
  • @MichaelT ,as I just discovered through serial output, no it doesn't change anything. But I did discover that The code gets stuck at the first statement when I set to chech against HIGH but it racks through all of the statements if i set it LOW – pcs3rd Oct 12 '18 at 2:05
  • 2
    how did you wire the 'keys'? to 5 V pin? do you have pull-down resistors? did you really connect some button to pin1? and HID on Uno? – Juraj Oct 12 '18 at 20:28
  • Welcome to Arduino:SE. You might find this post on our Meta site helpful: How to ask a good question for Arduino Stack Exchange. – sempaiscuba Oct 12 '18 at 21:06
  • 2
    after you edited the question, you still have it wrong: with input_pullup pins are HIGH, if not connected to ground – Juraj Oct 13 '18 at 5:17
1

Microcontroller pins tend to read LOW when left floating (but not always!). However, pin 1 is used to communicate with the USB-UART converter, and I believe it is set HIGH in the idle state. So assuming you are not pressing any buttons, keys 2-8 will be in the LOW state by default, but key 1 will be HIGH. Hence when defaultCase = HIGH;, the first if is triggered. When defaultCase = LOW; all if's but the first one fire up. Solution — don't use digital pin 1 (edit: avoid pin 0 and maybe 13 as well).

Edit: The answer above refers to the original code without the pull-up resistors enabled.

  • So i tried shifting all my pins to 2-9 and its still racking though all of the commands – pcs3rd Oct 13 '18 at 4:03
  • 1
    @pcs3rd: you've tried it with the new version of the code? If so please please take note of Juraj's recent comment. You now have pull-ups enabled, and all the pins should be reading HIGH unless you start pressing buttons (assuming the buttons are connected to ground). Your ifs should be checking ==LOW in order to work correctly. – ex-punctis Oct 13 '18 at 5:40
  • Oh. thats probably the issue then – pcs3rd Oct 13 '18 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.