I wanted to use HID with arduino nano 33 ble and I have figured out how use hid and print messages on it but I want it to print the message when a button is pressed but I have not been able to figure out how to do so.
#include "mbed.h"
#include "USBKeyboard.h"
USBKeyboard key;
int main(void)
{
while (1) {
key.printf("H/r/n");
delay(1000);
}
}
The code to print "H" but it prints it after a delay but I want it to print it on a button press.
I'm glad to see you found the solution @smitdagreat. Thank you for taking the time to post an update with your solution. I'm sure the others who are trying to do similar things and find this thread will be grateful, especially since there is not so much information about doing HID on the Nano 33 BLE sense as there is for the other USB Arduino boards.
smitdagreat:
I figured it out and here is some sample code -
#include "mbed.h"
#include "USBKeyboard.h"
USBKeyboard keyboard;
int button1Pin = 5;
int button2Pin = 6;
int main(void) {
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
while (1) {
if (digitalRead(button1Pin) == 0) {
keyboard.media_control(KEY_PLAY_PAUSE);
delay(250);
}
if (digitalRead(button2Pin) == 0) {
keyboard.media_control(KEY_VOLUME_UP);
}
}
}
IMPORTANT: this sketch will cause your Nano 33 BLE to no longer have a port, meaning you can't upload to it. The solution is to press and release the reset button on the Nano 33 BLE twice quickly. After that, you should see the "L" LED pulsing to indicate it's in bootloader mode. If you don't see the LED pulsing, try the double reset again until you get the timing right. Once the board is in bootloader mode, you will see a port and be able to upload again. Note that the port number may change when it's in bootloader mode, so make sure to select the correct port from the Arduino IDE's Tools > Port menu before uploading.