Nano as Virtual Keyboard

No idea what's wrong but I suggest you add further trace statements so you can see whether it's actually completing setup() before it resets, and whether it's getting into loop(), and where within loop() it's crashing.

On a sketch that small it seems unlikely you'd be running out of memory but that is one possible causes of crashes, so you could check the free memory when you get to loop. (A search of the forums will reveal a handy function one of the experts contributed to do this for you.)

I have done some debugging but removed it before posting so it was clear what I was trying to accomplish.

I added in endless loop in the setup like below:

void setup() {
  static uint8_t iCnt=0;
  Serial.begin(9600);
  Serial.print(" Setup Cnt="); Serial.println(++iCnt);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(BUTTON, INPUT_PULLUP);
  while (1);
  ...

With this change it only reset 5 or 6 times.

When I make this change instead.

void loop() {
  while (1);
  UsbKeyboard.update();
  ...

The resets when on and on.

I also tried putting debug statements in the object constructor for UsbKeyboard. Not surprising, there where repeated calls to that as well.

Any suggestions about other possible debugging?

Hey Randal, I tried your sketch with my setup and modified it slightly.

// Arduino Digital Pin 2 == Port D=Bit 2 == D+      (INT0)
// Arduino Digital Pin 4 == Port D=Bit 4 == D-
// Arduino Digital Pin 5 == Port D=Bit 5 == Pull-Up
#include "UsbKeyboard.h"
#define BUTTON 12
#define BYPASS_TIMER_ISR 1  // If the timer isr is corrected to not take so long change this to 0.

/* Define our own delay function so that we don't have to rely on operation of timer0, the interrupt used by the internal delay() */
void delayMs(unsigned int ms) {
#if BYPASS_TIMER_ISR
  for (; ms > 0; ms--) { delayMicroseconds(1000); }
#else
  delay(ms);
#endif
}

void setup() {
  static uint8_t iCnt=0;
  Serial.begin(9600);
  Serial.print(" Setup Cnt="); Serial.println(++iCnt);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(BUTTON, INPUT_PULLUP);
#if BYPASS_TIMER_ISR
  // Disable timer0 since it can mess with the USB timing.
  // Note: that this means some functions such as delay() will no longer work.
  TIMSK0&=!(1<<TOIE0);
#endif

  cli();  // Clear interrupts while performing time-critical operations
  
  usbDeviceDisconnect();   // Force re-enumeration so the host will detect us
  delayMs(250);
  usbDeviceConnect();

  sei(); // Set interrupts again
}

void loop() {
  UsbKeyboard.update();

  if (digitalRead(BUTTON) == LOW) {
    Serial.print("Botton Pressed");
    digitalWrite(LED_BUILTIN, HIGH);
    delayMs(30);
    digitalWrite(LED_BUILTIN, LOW);
    UsbKeyboard.sendKeyStroke(KEY_H, MOD_SHIFT_LEFT);
    
    UsbKeyboard.sendKeyStroke(KEY_E);
    UsbKeyboard.sendKeyStroke(KEY_L);
    UsbKeyboard.sendKeyStroke(KEY_L);
    UsbKeyboard.sendKeyStroke(KEY_O);

    UsbKeyboard.sendKeyStroke(KEY_SPACE);

    UsbKeyboard.sendKeyStroke(KEY_W, MOD_SHIFT_LEFT);
    UsbKeyboard.sendKeyStroke(KEY_O);
    UsbKeyboard.sendKeyStroke(KEY_R);
    UsbKeyboard.sendKeyStroke(KEY_L);
    UsbKeyboard.sendKeyStroke(KEY_D);

    UsbKeyboard.sendKeyStroke(KEY_ENTER);

    delayMs(20);
    //while (digitalRead(BUTTON) == LOW);
    Serial.println(",  Botton Released");
    
  }
}

It worked fine until I tried your libraries. I haven’t look st yours, but as you mentioned, they continuously reboot. Try mine attached instead as they seem to work on my setup. (BTW, I am using IDE 1.0.1 now)

Dave

UsbKeyboard.zip (40.4 KB)

Thank you Dave. This is a great help.

I will still need to compare the libraries and see why one worked and one didn't but I appreciate the time you took to look at this issue.

It now types the desired text. It is nice to know that at least the hardware works. Now for a little polish and shine.

I may try my hand at mouse data or trying to read the light status. But this is progress.

Again, Thanks

Awesome, what is your button setup like? Can you attach a schematic or a photo? I would love to know if yours hangs as well. BTW, my hanging problem is entirely random. As a test, I simply push the button lotsof times and eventually it hangs, sometimes rapidly pushing, sometimes relatively slowly. I figure it could be noise, but I tried a few different types of switches which all result in failures. Unfortunately I though everything was working great until I started testing.

Are you powering yours using the same USB that is providing your data link?

BTW, what's your end state of your project? I am working towards an Arcade machine and figured it would be a better challenge to build my controller rather than buy one.

huntd69:
Awesome, what is your button setup like? Can you attach a schematic or a photo?

I have attached a photo of my current board.

huntd69:
I would love to know if yours hangs as well. BTW, my hanging problem is entirely random. As a test, I simply push the button lotsof times and eventually it hangs, sometimes rapidly pushing, sometimes relatively slowly. I figure it could be noise, but I tried a few different types of switches which all result in failures. Unfortunately I though everything was working great until I started testing.

I will let you know.

huntd69:
Are you powering yours using the same USB that is providing your data link?

I have been. However, I will probably change that.

huntd69:
BTW, what’s your end state of your project? I am working towards an Arcade machine and figured it would be a better challenge to build my controller rather than buy one.

I am looking to add/change features of a USB keyboard. I want to read character from the keyboard and echo them to the computer. However, I may repurpose keys like Scroll Lock, Play, SysRq, …

I would like to program keyboard macros so maybe [Esc] [A] types out “arduino.cc/forum/index.php”. Or maybe long CLI commands. It would also be portable. I can take it to any computer and plugin.