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>'?
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:
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.