| 1 |
/* |
| 2 |
Arduino Programs Blink |
| 3 |
|
| 4 |
This sketch demonstrates the Keyboard library. |
| 5 |
|
| 6 |
For Leonardo and Due boards only. |
| 7 |
|
| 8 |
When you connect pin 2 to ground, it creates a new window with a key |
| 9 |
combination (CTRL-N), then types in the Blink sketch, then auto-formats the |
| 10 |
text using another key combination (CTRL-T), then uploads the sketch to the |
| 11 |
currently selected Arduino using a final key combination (CTRL-U). |
| 12 |
|
| 13 |
Circuit: |
| 14 |
- Arduino Leonardo, Micro, Due, LilyPad USB, or Yún |
| 15 |
- wire to connect D2 to ground |
| 16 |
|
| 17 |
created 5 Mar 2012 |
| 18 |
modified 29 Mar 2012 |
| 19 |
by Tom Igoe |
| 20 |
modified 3 May 2014 |
| 21 |
by Scott Fitzgerald |
| 22 |
|
| 23 |
This example is in the public domain. |
| 24 |
|
| 25 |
https://www.arduino.cc/en/Tutorial/BuiltInExamples/KeyboardReprogram |
| 26 |
*/ |
| 27 |
|
| 28 |
#include "Keyboard.h" |
| 29 |
|
| 30 |
// use this option for OSX. |
| 31 |
// Comment it out if using Windows or Linux: |
| 32 |
char ctrlKey = KEY_LEFT_GUI; |
| 33 |
// use this option for Windows and Linux. |
| 34 |
// leave commented out if using OSX: |
| 35 |
// char ctrlKey = KEY_LEFT_CTRL; |
| 36 |
|
| 37 |
|
| 38 |
void setup() { |
| 39 |
// make pin 2 an input and turn on the pull-up resistor so it goes high unless |
| 40 |
// connected to ground: |
| 41 |
pinMode(2, INPUT_PULLUP); |
| 42 |
// initialize control over the keyboard: |
| 43 |
Keyboard.begin(); |
| 44 |
} |
| 45 |
|
| 46 |
void loop() { |
| 47 |
while (digitalRead(2) == HIGH) { |
| 48 |
// do nothing until pin 2 goes low |
| 49 |
delay(500); |
| 50 |
} |
| 51 |
delay(1000); |
| 52 |
// new document: |
| 53 |
Keyboard.press(ctrlKey); |
| 54 |
Keyboard.press('n'); |
| 55 |
delay(100); |
| 56 |
Keyboard.releaseAll(); |
| 57 |
// wait for new window to open: |
| 58 |
delay(1000); |
| 59 |
|
| 60 |
// versions of the Arduino IDE after 1.5 pre-populate new sketches with |
| 61 |
// setup() and loop() functions let's clear the window before typing anything new |
| 62 |
// select all |
| 63 |
Keyboard.press(ctrlKey); |
| 64 |
Keyboard.press('a'); |
| 65 |
delay(500); |
| 66 |
Keyboard.releaseAll(); |
| 67 |
// delete the selected text |
| 68 |
Keyboard.write(KEY_BACKSPACE); |
| 69 |
delay(500); |
| 70 |
|
| 71 |
// Type out "blink": |
| 72 |
Keyboard.println("void setup() {"); |
| 73 |
Keyboard.println("pinMode(13, OUTPUT);"); |
| 74 |
Keyboard.println("}"); |
| 75 |
Keyboard.println(); |
| 76 |
Keyboard.println("void loop() {"); |
| 77 |
Keyboard.println("digitalWrite(13, HIGH);"); |
| 78 |
Keyboard.print("delay(3000);"); |
| 79 |
// 3000 ms is too long. Delete it: |
| 80 |
for (int keystrokes = 0; keystrokes < 6; keystrokes++) { |
| 81 |
delay(500); |
| 82 |
Keyboard.write(KEY_BACKSPACE); |
| 83 |
} |
| 84 |
// make it 1000 instead: |
| 85 |
Keyboard.println("1000);"); |
| 86 |
Keyboard.println("digitalWrite(13, LOW);"); |
| 87 |
Keyboard.println("delay(1000);"); |
| 88 |
Keyboard.println("}"); |
| 89 |
// tidy up: |
| 90 |
Keyboard.press(ctrlKey); |
| 91 |
Keyboard.press('t'); |
| 92 |
delay(100); |
| 93 |
Keyboard.releaseAll(); |
| 94 |
delay(3000); |
| 95 |
// upload code: |
| 96 |
Keyboard.press(ctrlKey); |
| 97 |
Keyboard.press('u'); |
| 98 |
delay(100); |
| 99 |
Keyboard.releaseAll(); |
| 100 |
|
| 101 |
// wait for the sweet oblivion of reprogramming: |
| 102 |
while (true); |
| 103 |
} |