将输入更改为较小的范围

时间:2015-06-01 01:49:18

标签: arduino

这里的第一次海报,请放轻松一下吧? :)

我目前正在使用Teensy 3.1 Arduino兼容主板,只是为了看看我能做些什么。所以我目前有一些代码,我发现它基本上只是模拟键盘并输入0000 ENTER,0001 ENTER,一直到9999.

我想调整代码,以便我可以将范围更改为我喜欢的范围,例如从2000-3000或2500-2600等开始。如果有人可以告诉我需要调整哪些行来实现,我会非常感激。

    #include <usb_keyboard.h>
// This code is licensed under Apache 2.0 License
// http://www.apache.org/licenses/LICENSE-2.0.txt
// Limitation of Liability. In no event and under no legal theory,
// whether in tort (including negligence), contract, or otherwise,
// unless required by applicable law (such as deliberate and grossly
// negligent acts) or agreed to in writing, shall any Contributor be
// liable to You for damages, including any direct, indirect, special,
// incidental, or consequential damages of any character arising as a
// result of this License or out of the use or inability to use the
// Work (including but not limited to damages for loss of goodwill,
// work stoppage, computer failure or malfunction, or any and all
// other commercial damages or losses), even if such Contributor
// has been advised of the possibility of such damages.
// This code is indented for people who are not able to contact
// apple support and I am in no way liable for any damage or
// problems this code might cause.

const int ledPin = 13; // choose the pin for the LED
int counter = 0;
int fakecounter = counter;
char pin[] = "xxxx";

void setup() {
  pinMode(ledPin, OUTPUT); // declare LED as output
  delay(10000);
}

void loop() {
  keyboard_modifier_keys = 0;
  if (counter <= 9999) {
    delay(8000);
    digitalWrite(ledPin, LOW);
    delay(5500);
    digitalWrite(ledPin, HIGH);
    sprintf(pin, "%04d", fakecounter);
    //sending first digit
    Keyboard.press(pin[0]);
    delay(450);
    Keyboard.release(pin[0]);
    delay(420);
    //sending second digit
    Keyboard.press(pin[1]);
    delay(398);
    Keyboard.release(pin[1]);
    delay(510);
    //sending third digit
    Keyboard.press(pin[2]);
    delay(421);
    Keyboard.release(pin[2]);
    delay(423);
    //sending forth digit
    Keyboard.press(pin[3]);
    delay(430);
    Keyboard.release(pin[3]);
    delay(525);
    //sending enter
    Keyboard.press(KEY_ENTER);
    delay(305);
    Keyboard.release(KEY_ENTER);
  }
  //reached 4 digit PIN max value
  if (counter > 9999) {
    for (int blinkies = 0; blinkies < 8; blinkies++) {
      digitalWrite(ledPin, HIGH);
      delay(20);
      digitalWrite(ledPin, LOW);
      delay(200);
    }
    delay(6000);
  }
  ++counter;
  fakecounter = counter;
}

1 个答案:

答案 0 :(得分:0)

int counter = 0;是开始,if (counter <= 9999) {定义结束。根据您选择的开头更改0,为您选择的结尾更改9999。

代码有效,因为keyboard.presskeyboard.release接受一个字符来选择要按下和释放的键。它使用sprintf将counter的值获取到char数组pin,使用变量fakecounter的值作为中间人,从int转换为string。计数器的值增加,并且在输入键的同时在虚拟键盘中键入。