| 1 |
/* |
| 2 |
Keyboard Message test |
| 3 |
|
| 4 |
For the Arduino Leonardo and Micro. |
| 5 |
|
| 6 |
Sends a text string when a button is pressed. |
| 7 |
|
| 8 |
The circuit: |
| 9 |
- pushbutton attached from pin 4 to +5V |
| 10 |
- 10 kilohm resistor attached from pin 4 to ground |
| 11 |
|
| 12 |
created 24 Oct 2011 |
| 13 |
modified 27 Mar 2012 |
| 14 |
by Tom Igoe |
| 15 |
modified 11 Nov 2013 |
| 16 |
by Scott Fitzgerald |
| 17 |
|
| 18 |
This example code is in the public domain. |
| 19 |
|
| 20 |
https://www.arduino.cc/en/Tutorial/BuiltInExamples/KeyboardMessage |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "Keyboard.h" |
| 24 |
|
| 25 |
const int buttonPin = 4; // input pin for pushbutton |
| 26 |
int previousButtonState = HIGH; // for checking the state of a pushButton |
| 27 |
int counter = 0; // button push counter |
| 28 |
|
| 29 |
void setup() { |
| 30 |
// make the pushButton pin an input: |
| 31 |
pinMode(buttonPin, INPUT); |
| 32 |
// initialize control over the keyboard: |
| 33 |
Keyboard.begin(); |
| 34 |
} |
| 35 |
|
| 36 |
void loop() { |
| 37 |
// read the pushbutton: |
| 38 |
int buttonState = digitalRead(buttonPin); |
| 39 |
// if the button state has changed, |
| 40 |
if ((buttonState != previousButtonState) |
| 41 |
// and it's currently pressed: |
| 42 |
&& (buttonState == HIGH)) { |
| 43 |
// increment the button counter |
| 44 |
counter++; |
| 45 |
// type out a message |
| 46 |
Keyboard.print("You pressed the button "); |
| 47 |
Keyboard.print(counter); |
| 48 |
Keyboard.println(" times."); |
| 49 |
} |
| 50 |
// save the current button state for comparison next time: |
| 51 |
previousButtonState = buttonState; |
| 52 |
} |