debugging a library

So im trying to use vusb library from here - Google Code Archive - Long-term storage for Google Code Project Hosting.

the example code compiles and uploads fine but when ever i hit a sendKeyStroke it makes the arduino hang, how i can i see whats going on?

Edit the library. Add some Serial.println calls to show what it is doing.

That's not the best idea with V-USB, Serial TX functions are blocking

V-USB documentation says usbPoll must be called every 10 ms or less

emada, when things gets stuck, try looking for while loops that might get stuck, there are a few in there that checks whether or not the interrupt endpoint is ready, try sticking usbPoll inside them.

ok well i was able to use serial but that didnt really tell me anything i didnt already know, can i do a try/catch kinda thing with that? i also added usbPoll to the while loops in UsbKeyboard.h but it still hangs on sendKeyStroke :frowning: could it just be its waiting for a response maybe and my hardware is messed up? thanx for the tips so far guys!! what else can i try to get this damn thing working? :slight_smile:

maybe time to post your non working code, maybe we see something in it ... (no experience with the named lib BTW)

robtillaart:
maybe time to post your non working code, maybe we see something in it ... (no experience with the named lib BTW)

This is the example code modified for debugging.

#include "UsbKeyboard.h"

#define BUTTON_PIN 12

// If the timer isr is corrected
// to not take so long change this to 0.
#define BYPASS_TIMER_ISR 1

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT);
  digitalWrite(BUTTON_PIN, HIGH);
  
#if BYPASS_TIMER_ISR
  // disable timer 0 overflow interrupt (used for millis)
  TIMSK0&=!(1<<TOIE0); // ++
#endif
}

#if BYPASS_TIMER_ISR
void delayMs(unsigned int ms) {
   /*
  */ 
  for (int i = 0; i < ms; i++) {
    delayMicroseconds(1000);
  }
}
#endif

void loop() {
  Serial.println("start loop");
  UsbKeyboard.update();
  delayMs(50);

//  digitalWrite(13, !digitalRead(13));

  if (digitalRead(BUTTON_PIN) == 1) {
    digitalWrite(13, HIGH);
    //UsbKeyboard.sendKeyStroke(KEY_B, MOD_GUI_LEFT);
    Serial.println("send first key");
    UsbKeyboard.sendKeyStroke(KEY_H);
    Serial.println("key sent"); //the script never gets this far
    /**
    UsbKeyboard.sendKeyStroke(KEY_E);
    UsbKeyboard.sendKeyStroke(KEY_L);
    UsbKeyboard.sendKeyStroke(KEY_L);
    UsbKeyboard.sendKeyStroke(KEY_O);

    UsbKeyboard.sendKeyStroke(KEY_SPACE);

    UsbKeyboard.sendKeyStroke(KEY_W);
    UsbKeyboard.sendKeyStroke(KEY_O);
    UsbKeyboard.sendKeyStroke(KEY_R);
    UsbKeyboard.sendKeyStroke(KEY_L);
    UsbKeyboard.sendKeyStroke(KEY_D);
    //UsbKeyboard.sendKeyStroke(KEY_B, MOD_GUI_LEFT);

    UsbKeyboard.sendKeyStroke(KEY_ENTER);*/
#if BYPASS_TIMER_ISR  // check if timer isr fixed.
    delayMs(20);
#else
    delay(20);
#endif
    
   }
  digitalWrite(13, LOW);
}

Ah, this code. I have something similar working. But it is highly timing critical. I withdraw my suggestion about putting in debug prints.

In fact, remove all the Serial.print calls and just flash an LED.

V-USB documentation says usbPoll must be called every 10 ms or less

UsbKeyboard.update();
delayMs(50);

err...

also you never mentioned whether or not you've successfully enumerated

no it does not enumerate properly and it happens even with the hardware not set up.

Forget about debugging anything until you can enumerate properly... start with blank code that does nothing but a loop that calls usbPoll, aka, Update

If it can't enumerate, figure out why, triple check your usbconfig.h , make sure the delay between the simulated disconnect-then-reconnect is long enough (according to that library's source code, there is no delay at all, insert one in there)

ok i took everything out except usbUpdate(), still no enumeration love.

im using:
2.2k-Ohms resistor ".5 watt - %5 tolerance"
2x 68-Ohm resistors ".5 watts - %5 tolerance"
2x .5W 3.6V Voltage Regulator Zener Diodes
and a hacked up usb cable

I wasn't able to add any connects/disconnects to usbconfig.h, every time I added something it wouldn't compile

im fairly sure i have the right stuff for the hardware, dose anyone see any issues with the components?

Photo?

Does your circuit agree with this?

http://www.practicalarduino.com/projects/virtual-usb-keyboard

It's a little hard to tell from the photos. Also did you use the 3.6V 0.5W (or less) recommended for the zener diodes?

yes sir i did, what cant you see? i can take a close up.

emada:
The example code compiles and uploads fine but when ever i hit a sendKeyStroke it makes the arduino hang, how i can i see whats going on?

How do you know it hangs?

well i know it hangs because when i push the button on pin 12 the onboard red led should turn on "pin 13" then it should send data and turn the led back off. However when i push the button the led turns on and never turns back off so its getting stuck on sendKeyStroke. If i comment all them out the led turns on and off as expected.

I fear this has something to do with it not enumerating properly on the system before trying to send data to it.

I fear this has something to do with it not enumerating properly on the system before trying to send data to it.

Well, duh!

PaulS:
Well, duh!

:stuck_out_tongue: any idea why this wouldn't enumerate?