Problem with Keyboard Code

Hello,

i need help with my Projekt i wanna program a Keyboard. I have downloaded the "HID Buttons", to have a keyboard libary. But i dont know the problem with the Code now.

#include <Keyboard.h>
#include <HID_Buttons.h>

const uint8_t pin1  =  1;
const uint8_t LED   = 25;

void setup() {

  pinMode(LED, OUTPUT);
  pinMode(pin1, INPUT_PULLDOWN);
    
}

void loop() {

  digitalWrite(LED, HIGH);
  delay(500);
  digitalWrite(LED, LOW);
  delay(500);
  if (digitalRead(pin1, HIGH) {

    Keyboard.Write('1')
  }
} 

Welcome and thanks for using code tags in your first post :+1:

Check the syntax of below. Hint: digitalRead() only takes one parameter.

PS
If you get errors, post them !! Use code tags for that as well.

Thanks.

The Code is:

#include <Keyboard.h>
#include <HID_Buttons.h>

const uint8_t pin1  =  1;
const uint8_t LED   = 25;

void setup() {

  pinMode(LED, OUTPUT);
  pinMode(pin1, INPUT_PULLDOWN);
    
}

void loop() {

  digitalWrite(LED, HIGH);
  delay(500);
  digitalWrite(LED, LOW);
  delay(500);
  if (digitalRead(pin1)) {

    Keyboard.write('1')
  }
} 

And the Error is:

C:\Users\steni\Desktop\Tastatur_Eingabe\neu\neu.ino: In function 'void loop()':
C:\Users\steni\Desktop\Tastatur_Eingabe\neu\neu.ino:22:5: error: 'Keyboard' was not declared in this scope
     Keyboard.write('1')
     ^~~~~~~~
C:\Users\steni\Desktop\Tastatur_Eingabe\neu\neu.ino:22:5: note: suggested alternative:
In file included from C:\Users\steni\Desktop\Tastatur_Eingabe\neu\neu.ino:1:0:
C:\Users\steni\Documents\Arduino\libraries\BlueFairy\src/Keyboard.h:73:15: note:   'ciag::bluefairy::Keyboard'
         class Keyboard {
               ^~~~~~~~
Mehrere Bibliotheken wurden für "Keyboard.h" gefunden
  Benutzt: C:\Users\steni\Documents\Arduino\libraries\BlueFairy
  Nicht benutzt: C:\Users\steni\AppData\Local\Arduino15\libraries\Keyboard
exit status 1

Compilation error: 'Keyboard' not found. Does your sketch include the line '#include <Keyboard.h>'?

Which board are you compiling for? The Keyboard library is only usable on boards with native USB; so this excludes boards like Uno, Nano, Pro Mini and Mega.

1 Like

I will make this on a raspberry pi pico.

I'm not familiar with that board so can't advise; sorry.

Also, I don't see Keyboard.begin(); anywhere

That won't work. It should be pinMode(pin1, INPUT_PULLUP);
and don't forget that the logic will now be reversed, so pressing a button will be LOW and will read HIGH by default.

also, don't forget to end the command with ;

Lastly, with those fixes, and removing #include <HID_Buttons.h> altogether (is it necessary if you already have the Keyboard library?), and following the wisdom of @kmin

and @sterretje (compiling for an appropriate Arduino - I selected Leonardo), the fixed code does indeed compile, That's not to say it will work as you intend, but it compiles. Here's the full fix:

#include <Keyboard.h>

const uint8_t pin1  =  1;
const uint8_t LED   = 25;

void setup() {
  pinMode(LED, OUTPUT);
  pinMode(pin1, INPUT_PULLUP);
  Keyboard.begin();
}

void loop() {
  digitalWrite(LED, HIGH);
  delay(500);
  digitalWrite(LED, LOW);
  delay(500);
  if (digitalRead(pin1)) {
    Keyboard.write('1');
  }
}

Also, did you know your sketch spends almost all of its time in delay(500);? That's blocking code; nothing can happen while those delay calls are being executed.
Not sure why you want a blinking light that has nothing to do with pressing some button and sending a char '1' but I thought I'd mention it since you will miss button presses and you won't be sending anything during that blinking LED routine.
Try using millis() instead.

There are boards (processors) that have internal pull-down resistors; if the board does not support it, it will be a future error message :wink:

1 Like

Oh, neat. That will be a handy feature. Are there any you're aware of that I can try it on currently?
Nevermind, it shows in blue if I select Teensy LC, surely others are the same.
Thanks, good to know when I select a board for a future project (if you can pry the Nano Every from my cold, dead hands! :metal: )
EDIT: just actually scrolled through the Boards list in IDE version 1.8.10 (if it ain't broke, you're not fixing it hard enough) and holy moly there are a lot of boards.
How do folks keep up? I feel like a dinosaur.

The more you hang around on the forum, the more you learn :rofl:

I have quite an extensive list of boards installed in one of my portable installs (mostly to help people here) and a grep gives

grep -Rin '.' -e 'INPUT_PULLDOWN'
./esp8266/hardware/esp8266/3.1.2/tests/host/common/HostWiring.cpp:66:    case INPUT_PULLDOWN_16:
./esp8266/hardware/esp8266/3.1.2/tests/host/common/HostWiring.cpp:67:        m = "INPUT_PULLDOWN_16";
./esp8266/hardware/esp8266/3.1.2/keywords.txt:21:INPUT_PULLDOWN_16	LITERAL1
./esp8266/hardware/esp8266/3.1.2/cores/esp8266/core_esp8266_wiring_digital.cpp:73:    if(mode == INPUT || mode == INPUT_PULLDOWN_16){
./esp8266/hardware/esp8266/3.1.2/cores/esp8266/core_esp8266_wiring_digital.cpp:74:      if(mode == INPUT_PULLDOWN_16){
./esp8266/hardware/esp8266/3.1.2/cores/esp8266/Arduino.h:51:#define INPUT_PULLDOWN_16 0x04 // PULLDOWN only possible for pin16
./arduino/hardware/megaavr/1.8.8/cores/arduino/api/Common.h:23:  INPUT_PULLDOWN   = 0x3,
./arduino/hardware/samd/1.8.14/keywords.txt:3:INPUT_PULLDOWN	LITERAL1	Constants	RESERVED_WORD_2
./arduino/hardware/samd/1.8.14/cores/arduino/api/Common.h:23:  INPUT_PULLDOWN   = 0x3,
./arduino/hardware/samd/1.8.14/cores/arduino/wiring_digital.c:51:    case INPUT_PULLDOWN:
./arduino/hardware/mbed_giga/4.1.1/cores/arduino/as_mbed_library/pinmode_arduino.h:19:#define INPUT_PULLDOWN  TempINPUT_PULLDOWN
./arduino/hardware/mbed_giga/4.1.1/cores/arduino/as_mbed_library/pinmode_arduino.h:49:#undef INPUT_PULLDOWN
./arduino/hardware/mbed_giga/4.1.1/cores/arduino/as_mbed_library/pinmode_arduino.h:67:    INPUT_PULLDOWN = TempINPUT_PULLDOWN
./arduino/hardware/mbed_giga/4.1.1/cores/arduino/api/Common.h:23:  INPUT_PULLDOWN   = 0x3,
./arduino/hardware/mbed_giga/4.1.1/cores/arduino/Interrupts.cpp:79:      pinMode(interruptNum, INPUT_PULLDOWN);
./arduino/hardware/mbed_giga/4.1.1/cores/arduino/wiring_digital.cpp:44:      case INPUT_PULLDOWN:
./arduino/hardware/mbed_giga/4.1.1/cores/arduino/wiring_digital.cpp:76:    case INPUT_PULLDOWN:
./arduino/hardware/mbed_giga/4.1.1/variants/GIGA/pinmode_arduino.h:18:#define INPUT_PULLDOWN  TempINPUT_PULLDOWN
./arduino/hardware/mbed_giga/4.1.1/variants/GIGA/pinmode_arduino.h:48:#undef INPUT_PULLDOWN
./arduino/hardware/mbed_giga/4.1.1/variants/GIGA/pinmode_arduino.h:66:    INPUT_PULLDOWN = TempINPUT_PULLDOWN
./arduino/hardware/mbed_giga/4.1.1/variants/GENERIC_STM32H747_M4/pinmode_arduino.h:18:#define INPUT_PULLDOWN  TempINPUT_PULLDOWN
./arduino/hardware/mbed_giga/4.1.1/variants/GENERIC_STM32H747_M4/pinmode_arduino.h:48:#undef INPUT_PULLDOWN
./arduino/hardware/mbed_giga/4.1.1/variants/GENERIC_STM32H747_M4/pinmode_arduino.h:66:    INPUT_PULLDOWN = TempINPUT_PULLDOWN
1 Like

I agree. I continue to learn and try new things.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.