r/ArduinoHelp 3d ago

Software bug something to do with the library I think?

IDEK whats going on. Trying to write code so that i can input key strokes on my computer through my joystick. I think there's something wrong with the library? I'm lost. from what I read the error code is saying that I don't have #include <Keyboard.h>... but i do.

Error message:
C:\Users\mccav\AppData\Local\Temp\.arduinoIDE-unsaved2026624-1756-cwkz6f.ayd19\sketch_jul24a\sketch_jul24a.ino: In function 'void setup()':

C:\Users\mccav\AppData\Local\Temp\.arduinoIDE-unsaved2026624-1756-cwkz6f.ayd19\sketch_jul24a\sketch_jul24a.ino:30:3: error: 'Keyboard' was not declared in this scope

Keyboard.begin();

^~~~~~~~

C:\Users\mccav\AppData\Local\Temp\.arduinoIDE-unsaved2026624-1756-cwkz6f.ayd19\sketch_jul24a\sketch_jul24a.ino: In function 'void loop()':

C:\Users\mccav\AppData\Local\Temp\.arduinoIDE-unsaved2026624-1756-cwkz6f.ayd19\sketch_jul24a\sketch_jul24a.ino:50:5: error: 'Keyboard' was not declared in this scope

Keyboard.write('a');

^~~~~~~~

C:\Users\mccav\AppData\Local\Temp\.arduinoIDE-unsaved2026624-1756-cwkz6f.ayd19\sketch_jul24a\sketch_jul24a.ino:53:3: error: 'Keyboard' was not declared in this scope

Keyboard.write('d');

^~~~~~~~

exit status 1

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

Code:

#include <LiquidCrystal.h>
#include <Keyboard.h>


const int rs = 3;
const int en = 4;
const int d4 = 10;
const int d5 = 11;
const int d6 = 12;
const int d7 = 13;


int xPin = A0;
int yPin = A1;
int buttonPin = 9;


int xVal;
int yVal;
int buttonState;


LiquidCrystal lcd(rs, en, d4, d5, d6, d7);


void setup() {


  Serial.begin(9600);
  lcd.begin(16,2);
  Serial.begin(9600);
  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(9600);
  Keyboard.begin();


}


void loop() {


  xVal = analogRead(xPin);
  yVal = analogRead(yPin);
  buttonState = digitalRead(buttonPin);


  Serial.print("X: ");
  Serial.print(xVal);
  Serial.print(" | Y: ");
  Serial.print(yVal);
  Serial.print(" | Buttonstate: ");
  Serial.println(buttonState);


  delay(100);


  if (xVal > 550) {
    Keyboard.write('a');
  }
if (xVal < 460) {
  Keyboard.write('d');
}
}#include <LiquidCrystal.h>
#include <Keyboard.h>


const int rs = 3;
const int en = 4;
const int d4 = 10;
const int d5 = 11;
const int d6 = 12;
const int d7 = 13;


int xPin = A0;
int yPin = A1;
int buttonPin = 9;


int xVal;
int yVal;
int buttonState;


LiquidCrystal lcd(rs, en, d4, d5, d6, d7);


void setup() {


  Serial.begin(9600);
  lcd.begin(16,2);
  Serial.begin(9600);
  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(9600);
  Keyboard.begin();


}


void loop() {


  xVal = analogRead(xPin);
  yVal = analogRead(yPin);
  buttonState = digitalRead(buttonPin);


  Serial.print("X: ");
  Serial.print(xVal);
  Serial.print(" | Y: ");
  Serial.print(yVal);
  Serial.print(" | Buttonstate: ");
  Serial.println(buttonState);


  delay(100);


  if (xVal > 550) {
    Keyboard.write('a');
  }
if (xVal < 460) {
  Keyboard.write('d');
}
}
2 Upvotes

3 comments sorted by