Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //arduino leonardo - one button keyboard
- #include "Keyboard.h"
- const int button = 4;
- const int led = 13;
- const int debounce = 18;
- const long interval = 200;
- const long timeout = 5000;
- bool lastState = HIGH;
- unsigned long startTime = 0;
- int currentBit = 7;
- int character = 0;
- int lastCharacter = 0;
- bool repeat = false;
- void setup() {
- pinMode(button, INPUT_PULLUP);
- pinMode(led, OUTPUT);
- Keyboard.begin();
- }
- void loop() {
- bool state = digitalRead(button);
- if (state != lastState) {
- if (repeat) {
- Keyboard.releaseAll();
- repeat = false;
- }
- delay(debounce);
- lastState = state;
- if (state == LOW) {
- startTime = millis();
- digitalWrite(led, HIGH);
- }
- if (state == HIGH) { //button released
- if (millis() - startTime < interval) { //1
- digitalWrite(led, LOW);
- character |= 1<<currentBit;
- if (character == 255) { //repeat
- currentBit = 8;
- character = 0;
- repeat = true;
- Keyboard.press(lastCharacter);
- }
- }
- currentBit--;
- if (currentBit < 0) { //finished character
- Keyboard.write(character);
- lastCharacter = character;
- currentBit = 7;
- character = 0;
- }
- }
- }
- if (millis() - startTime > interval) {
- digitalWrite(led, LOW);
- }
- if (millis() - startTime > timeout && currentBit != 7) {
- startTime = millis();
- digitalWrite(led, HIGH);
- currentBit = 7;
- character = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement