source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/examples/09.USB/Keyboard/KeyboardReprogram/KeyboardReprogram.ino @ 4837

Last change on this file since 4837 was 4837, checked in by daduve, 2 years ago

Adding new version

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