将输入更改为更小的范围

Change input to a smaller range

第一次发帖,请放过我好吗? :)

我目前正在玩一个 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;
}

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


代码有效是因为 keyboard.presskeyboard.release 接受一个字符到 select 按下和释放哪个键。它使用 sprintf 获取 counter 的值到 char 数组 pin,从 int 转换为 string,使用变量 fakecounter 的值作为中间人。 counter 的值增加,并随着 enter 键在虚拟键盘中输入。