Skip to content

Instantly share code, notes, and snippets.

@dkgrieshammer
Created November 20, 2018 17:15
Show Gist options
  • Save dkgrieshammer/c59e0a24abb4c01df3981c82021932f8 to your computer and use it in GitHub Desktop.
Save dkgrieshammer/c59e0a24abb4c01df3981c82021932f8 to your computer and use it in GitHub Desktop.
Arduino Leonardo 5-Button Keyboard
/*
Keyboard Message test
For the Arduino Leonardo and Micro.
Sends a text string when a button is pressed.
The circuit:
- pushbuttons attached from pin 2 pin 6 connected to Ground
- Interal Pullups used
created 24 Oct 2011
modified 27 Mar 2012
by Tom Igoe
modified 11 Nov 2013
by Scott Fitzgerald
modified 20 Nov 2018
by David Grieshammer
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/KeyboardMessage
*/
#include "Keyboard.h"
const int leftPin = 2; // input pin for pushbutton
const int centerPin = 3;
const int topPin = 4;
const int rightPin = 5;
const int bottomPin = 6;
int previousButtonState = 0; // for checking the state of the pushButtons
void setup() {
// set Pins Input:
Serial.begin(9600);
pinMode(leftPin, INPUT_PULLUP);
pinMode(centerPin, INPUT_PULLUP);
pinMode(topPin, INPUT_PULLUP);
pinMode(rightPin, INPUT_PULLUP);
pinMode(bottomPin, INPUT_PULLUP);
// initialize control over the keyboard:
Keyboard.begin();
}
void loop() {
// read the pushbutton:
if(checkState(leftPin) == true) {
Keyboard.print("n");
}
if(checkState(rightPin) == true) {
Keyboard.print("r");
}
if(checkState(topPin) == true) {
Keyboard.print("t");
}
if(checkState(bottomPin) == true) {
Keyboard.print("b");
}
if(checkState(centerPin) == true) {
Keyboard.print("l");
}
}
boolean checkState(int tmpPin) {
if(digitalRead(tmpPin) == LOW && previousButtonState == 0) {
previousButtonState = tmpPin;
return true;
} else if (digitalRead(tmpPin) == HIGH && previousButtonState == tmpPin) {
previousButtonState = 0;
delay(20); //needed for deboucing of the buttons!
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment