Help

Hello,
I’m having a problem with the code 6-1 in the “Getting Started With Arduino” book. This is my written out code. I haven’t been able to find anything wrong. Is there maybe something I didn’t notice?

const int buttonPin = 4; // input pin for pushbutton
int previousButtonState = HIGH; // for checking the state
// of pushButton
int counter = 0; // button push counter

void setup() {
// make the pushButton pin an input:
pinMode(buttonPin, INPUT);
// initialize control over the keyboard:
Keyboard.begin();
}

void loop() {
// read the pushbutton:
int buttonState = digitalRead(buttonPin);
// if the button state has changed
if ((buttonState != previousButtonState)
// and it’s currently pressed:
&& (buttonState == HIGH)) {
// increment the button counter
counter++;
// type out a message
Keyboard.print(“You pressed the button “);
Keyboard.print(counter);
Keyboard.println(” times.”);
}
// save the current button state for comparison next time:
previousButtonState = buttonState;
}

This is the error it gave me.

Users/laney/Documents/Arduino/sketch_sep18a/sketch_sep18a.ino: In function ‘void setup()’:
sketch_sep18a:11:3: error: ‘Keyboard’ not found. Does your sketch include the line ‘#include <Keyboard.h>’?
Keyboard.begin();
^
/Users/laney/Documents/Arduino/sketch_sep18a/sketch_sep18a.ino: In function ‘void loop()’:
sketch_sep18a:24:5: error: ‘Keyboard’ not found. Does your sketch include the line ‘#include <Keyboard.h>’?
Keyboard.print("You pressed the button ");
^
exit status 1
‘Keyboard’ not found. Does your sketch include the line ‘#include <Keyboard.h>’?

If you can help, that would be great. Thank you

What board are you compiling for? The Keyboard library does not work on all Arduinos.

Did you look at the errors? They all ask “Does your sketch include the line ‘#include <Keyboard.h>’?”

It doesn’t. It should and you should have installed that library.

Steve

and you should have installed that library.

No need to install a standard library.

Kelly_Macd: Hello,

Is there maybe something I didn't notice?

Yes.

Code Tags

PaulS: What board are you compiling for? The Keyboard library does not work on all Arduinos.

I'm using Elegoo Uno R3

You can’t use the Keyboard library with your Uno. You can only use it with Arduino boards that have native USB capabilities like the Leonardo, Pro Micro, Micro, or any of the newer SAMD boards.

It appears that your “Getting Started With Arduino” book is a bit outdated. Years ago, you didn’t need to add an #include directive for Keyboard.h to use the Keyboard library. But now you do need to add this line to your sketch:

#include <Keyboard.h>

The same is true with the Mouse library.

Kelly_Macd: I'm using Elegoo Uno R3

In that case, Keyboard will not work. It only works on Arduinos with native USB support and the Uno is not one of them.

Kelly_Macd: The code says to have it there. I've tried removing it but it does the same thing, but with something else. I tried some other codes and it did the same thing when there was no error to be found.

The code doesn't say that you need to include the header file. The COMPILER does. But, the compiler is assuming that you are compiling for a board where using the Keyboard library makes sense. You are not, so the compiler output is all wrong BECAUSE YOU SELECTED AN INCOMPATIBLE BOARD.

sterretje: In that case, Keyboard will not work. It only works on Arduinos with native USB support and the Uno is not one of them.

Thank you for your help, I didn't realize that it was the wrong board. I'm new at this so it's slightly confusing. You've been a great help.

You can use either a USB shield or a USD-TTL convertor as is used for ICSP programming.