uint8_t buf[8] = { 0 }; //Keyboard report buffer
#define PIN_W 4 // Pin for w
#define PIN_A 5 // Pin for a
#define PIN_S 6 // Pin for s
#define PIN_D 7 // Pin for d
int potVal = 0;
void setup() {
Serial.begin(9600); // Setup Serial communication
//Set pinmode of Input pins
pinMode(PIN_W, INPUT);
pinMode(PIN_A, INPUT);
pinMode(PIN_S, INPUT);
pinMode(PIN_D, INPUT);
}
void loop() {
//When button representing W is pressed
if (digitalRead(PIN_W) == HIGH) {
buf[2] = 26; // W keycode
Serial.write(buf, 8); // Send keypress
Serial.println("Button 4 Pressed");
releaseKey();
}
//When button representing A is pressed
if (digitalRead(PIN_A) == HIGH) {
buf[2] = 4; // A keycode
Serial.write(buf, 8); // Send keypress
Serial.println("Button 5 Pressed");
releaseKey();
}
//When button representing S is pressed
if (digitalRead(PIN_S) == HIGH) {
buf[2] = 22; // S keycode
Serial.write(buf, 8); // Send keypress
Serial.println("Button 6 Pressed");
releaseKey();
}
//When button representing D is pressed
if (digitalRead(PIN_D) == HIGH) {
buf[2] = 7; // D keycode
Serial.write(buf, 8);
Serial.println("Button 7 Pressed");
releaseKey();
}
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
if ( sensorValue != potVal ) {
// print out the value you read:
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
potVal = sensorValue;
}
}
// Function for Key Release
void releaseKey() {
buf[0] = 0;
buf[2] = 0;
Serial.write(buf, 8); // Send Release key
}