Leonardo USB support doesn't work

Hello, i have Leonardo board. And i have problem with it. I am searching for hours and can't solve it.
I want use Leonardo as native keyboard. Leonardo has native usb support. I have win10.

Here is my code... If i open serial monitor in Arduino IDE so it works. If close serial monitor and close IDE so still keyboard working. If re-plug Leonardo so again nothing work. I don't know what is bad or what i am doing bad. Can you help me please ? Arduino IDE latest version and all libraries are udpated.

#include  <Arduino.h>
#include <Keyboard.h>
#include "Mux.h"

using namespace admux;

Mux mux(Pin(3, INPUT, PinType::Digital), Pinset(8, 9, 10, 11));

void setup() {
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB
  }
  Keyboard.begin();
}

void loop() {
  byte data;
  for (byte i = 0; i <= 2; i++) {

    if (i == 0 && mux.read(i) == 0) {
     Keyboard.press(KEY_LEFT_GUI); 
        Keyboard.press('r'); delay(150); 
        Keyboard.release(KEY_LEFT_GUI); 
        Keyboard.release('r'); 
        delay(150); //give your system time to catch up with these android-speed keyboard presses
        Keyboard.println("mspaint"); 
    }
    if (i == 1 && mux.read(i) == 0) {
      Keyboard.write('w');
    }
    if (i == 2 && mux.read(i) == 0) {
      Keyboard.write('e');
    }
  }
  delay(100);
}

Does it happen with the Keyboard library example sketches?

There is no need for the while (!Serial) if you are not using Serial. That is waiting for the serial connection over USB to be established.

1 Like

I suspect the use of the "Mux" library. Can you produce an example of the problem that doesn't use that library?

It works !!! Thank you !!!

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