2011-12-16 21:58:42 +01:00
|
|
|
/*
|
|
|
|
Keyboard Button test
|
|
|
|
|
|
|
|
Sends a text string when a button is pressed.
|
|
|
|
|
|
|
|
The circuit:
|
2012-03-27 21:00:24 +02:00
|
|
|
* pushbutton attached from pin 2 to +5V
|
2011-12-16 21:58:42 +01:00
|
|
|
* 10-kilohm resistor attached from pin 4 to ground
|
|
|
|
|
|
|
|
created 24 Oct 2011
|
2012-03-27 21:00:24 +02:00
|
|
|
modified 27 Mar 2012
|
2011-12-16 21:58:42 +01:00
|
|
|
by Tom Igoe
|
|
|
|
|
|
|
|
This example code is in the public domain.
|
|
|
|
|
|
|
|
http://www.arduino.cc/en/Tutorial/KeyboardButton
|
|
|
|
*/
|
|
|
|
|
2012-03-27 21:00:24 +02:00
|
|
|
const int buttonPin = 2; // input pin for pushbutton
|
2011-12-16 21:58:42 +01:00
|
|
|
int previousButtonState = HIGH; // for checking the state of a pushButton
|
|
|
|
int counter = 0; // button push counter
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
// make the pushButton pin an input:
|
|
|
|
pinMode(buttonPin, INPUT);
|
2012-03-27 21:00:24 +02:00
|
|
|
// initialize control over the keyboard:
|
|
|
|
Keyboard.begin();
|
2011-12-16 21:58:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
// read the pushbutton:
|
|
|
|
int buttonState = digitalRead(buttonPin);
|
|
|
|
// if the button state has changed,
|
|
|
|
if ((buttonState != previousButtonState)
|
2012-03-27 21:00:24 +02:00
|
|
|
// and it's currently pressed:
|
|
|
|
&& (buttonState == HIGH)) {
|
|
|
|
// increment the button counter
|
2011-12-16 21:58:42 +01:00
|
|
|
counter++;
|
|
|
|
// type out a message
|
2012-03-19 17:02:48 +01:00
|
|
|
Keyboard.print("You pressed the button ");
|
2011-12-16 21:58:42 +01:00
|
|
|
Keyboard.print(counter);
|
|
|
|
Keyboard.println(" times.");
|
|
|
|
}
|
|
|
|
// save the current button state for comparison next time:
|
|
|
|
previousButtonState = buttonState;
|
|
|
|
}
|
2012-03-27 21:00:24 +02:00
|
|
|
|