HID device status between power on and system stable

I met a strange problem to make my arduino UNO to be a HID keyboard.
If I use "Flip" software and change the firmware to "Arduino-keyboard-0.3.hex", with the following code, it works very well when my PC bootup (I want to send a 'space' char because my PC requires it when just at powerup stage)

unsigned long startTime;
unsigned long currentTime;
const unsigned long delay_period = 30000;
int flag = 0;
int send_number = 2;
// Arduino USB Keyboard HID demo
uint8_t buf[8] = {0};    /* Keyboard report buffer */
void setup() {
  Serial.begin(9600);
  startTime = millis();  // initial start time  
  delay(200);}
void loop() {
  currentTime = millis();
  if (currentTime - startTime >= delay_period && flag < send_number) {
    buf[2] = 44;     // Space key
    Serial.write(buf, 8);   // Send keypress
    keyRelease();
    flag = flag + 1;
  }
  if (flag == send_number) {
    keyRelease();
    flag = flag + 1; }
  if(Serial.available()>0){}
}
void keyRelease() {
  buf[0] = 0;
  buf[2] = 0;
  Serial.write(buf, 8);}

But I try to change my UNO bootloader to NicoHood/Hoodloader2 HID project, and with the following code, it becomes unworkable at my PC powerup stage. However, if I try this code after PC going to Windows environment, it works again.

#include <Keyboard.h>
#include <HID.h>
String RCV;
int bootflag = 0, i;
unsigned long bootMillis;
unsigned long currentMillis;
void setup() {
  //while (!Serial);
  Serial.begin(115200);
  Keyboard.begin();
  bootMillis = millis();
}
void loop() {
  while(bootflag == 0) {
    //Wait 1 minutes then send blank to host after powerup
    while(millis() - bootMillis > 12000 && millis() - bootMillis < 24000) {
      Keyboard.print(" ");
      delay(100);
      bootflag = 1;  } } } 

Is there any difference between these two modes? How can I make my PC successfully recognize my UNO as a HID keyboard in powerup stage?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.