GettingStarted.ino 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**************************************************************************
  2. *
  3. * File: GettingStarted.ino
  4. * Author: Julian Schuler (https://github.com/julianschuler)
  5. * License: GNU GPLv3, see LICENSE.txt
  6. * Description: This file is an example from the USBKeyboard library.
  7. * It types in a message after pressing a button and shows
  8. * the state of Caps Lock with the internal LED.
  9. *
  10. **************************************************************************/
  11. #include <USBKeyboard.h>
  12. /* connect a button to this pin and GND */
  13. #define BUTTON_PIN 7
  14. /* currently available layouts: LAYOUT_US, LAYOUT_DE */
  15. #define LAYOUT LAYOUT_US
  16. /* initialize class by creating object mKeyboard */
  17. USBKeyboard mKeyboard = USBKeyboard();
  18. bool lastButtonState = HIGH;
  19. bool lastCapsLockState;
  20. void setup() {
  21. /* USB timing has to be exact, therefore deactivate Timer0 interrupt */
  22. TIMSK0 = 0;
  23. /* set the button pin as input and activate the internal pullup resistor */
  24. pinMode(BUTTON_PIN, INPUT_PULLUP);
  25. /* set the internal LED pin (normally D13) as output */
  26. pinMode(LED_BUILTIN, OUTPUT);
  27. /* set the internal LED corresponding to the Caps Lock state */
  28. if (mKeyboard.isCapsLockActivated()) {
  29. digitalWrite(LED_BUILTIN, HIGH);
  30. lastCapsLockState = HIGH;
  31. }
  32. else {
  33. digitalWrite(LED_BUILTIN, LOW);
  34. lastCapsLockState = LOW;
  35. }
  36. }
  37. void loop() {
  38. /* call this function at least every 20ms, otherwise an USB timeout will occur */
  39. mKeyboard.update();
  40. /* check if button is pressed */
  41. if (digitalRead(BUTTON_PIN) == LOW) {
  42. if (lastButtonState == HIGH) {
  43. /* print() and println() can be used to type in chars, Strings
  44. * and numbers, similar to Serial.print() and Serial.println() */
  45. mKeyboard.println("Hello World!");
  46. lastButtonState = LOW;
  47. }
  48. }
  49. else if (lastButtonState == LOW) {
  50. lastButtonState = HIGH;
  51. }
  52. /* check if the Caps Lock state has changed */
  53. if (mKeyboard.isCapsLockActivated() != lastCapsLockState) {
  54. /* toggle lastCapsLockState */
  55. lastCapsLockState = !lastCapsLockState;
  56. /* toggle LED state */
  57. digitalWrite(LED_BUILTIN, lastCapsLockState);
  58. }
  59. /* due to the deactivation of the Timer0 interrupt, delay()
  60. * and millis() won't work, call delayMicroseconds() instead */
  61. delayMicroseconds(20000);
  62. }