Duemilanove vusb fail

So i have a Arduino Duemilanove and im trying to build this http://www.practicalarduino.com/projects/virtual-usb-keyboard
using this code

#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() {
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() {

UsbKeyboard.update();

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

if (digitalRead(BUTTON_PIN) == 0) {
digitalWrite(13, HIGH);
//UsbKeyboard.sendKeyStroke(KEY_B, MOD_GUI_LEFT);

UsbKeyboard.sendKeyStroke(KEY_H);
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);
}

Everything compiles and uploads ok but when i run it, it hangs at this line

UsbKeyboard.sendKeyStroke(KEY_H);

i have no idea why, any ideas?

where did you initialize V-USB?

frank26080115:
where did you initialize V-USB?

first let me say sorry if this is noobish but i thought this line did that

UsbKeyboard.update();

so i take it thats not it?

emada:
so i take it thats not it?

update is probably a wrapper for "usbPoll"

usually the initialization sequence is:

usbInit();

then do

usbDeviceDisconnect();

delay for a few milliseconds to make the computer think you've disconnected, then

usbDeviceConnect();

and then enable interrupts


if you have time, watch my video here usnoobie_rfid_keyboard | Search Results | Eleccelerator so you actually understand what's happening

well here is my new code

#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() {
pinMode(BUTTON_PIN, INPUT);
digitalWrite(BUTTON_PIN, HIGH);
usbInit();

#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() {

UsbKeyboard.update();

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

if (digitalRead(BUTTON_PIN) == 0) {
digitalWrite(13, HIGH);
//usbDeviceDisconnect();
delay(1);
//usbDeviceConnect();
//UsbKeyboard.sendKeyStroke(KEY_B, MOD_GUI_LEFT);
/**
UsbKeyboard.sendKeyStroke(KEY_H);
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);
}

now it hangs on delay(1); :frowning: i say that cuz i push the button on pin 12 and the 13led goes on and never turns back off i even have all the send commands commented out. what am i missing?

didn't you turn off the timer ISR? delay(1) is going to get stuck since it depends on it.

I just read the source code for the "UsbKeyboard.h" library, I'm sorry but it's horrible. The initialization apparently takes place in a constructor and it doesn't even offer a delay during the disconnect-reconnect sequence (10 ms minimum is needed for it to actually work). The while loops for checking the interrupt pipe ready state doesn't call usbPoll.

frank26080115:
didn't you turn off the timer ISR? delay(1) is going to get stuck since it depends on it.

I just read the source code for the "UsbKeyboard.h" library, I'm sorry but it's horrible. The initialization apparently takes place in a constructor and it doesn't even offer a delay during the disconnect-reconnect sequence (10 ms minimum is needed for it to actually work). The while loops for checking the interrupt pipe ready state doesn't call usbPoll.

Can you recommend another library? I am just trying to use the example that comes with the library from the project from the link above :slight_smile: and duh yes your right, i disabled ISR timer, ill try to change it to delayMs(10); when i get out of work. tyvm for all your help so far!!! i just hope i can get this thing working. other then my delay issue does the code look like it should work?

frank26080115:
didn’t you turn off the timer ISR? delay(1) is going to get stuck since it depends on it.

I just read the source code for the “UsbKeyboard.h” library, I’m sorry but it’s horrible. The initialization apparently takes place in a constructor and it doesn’t even offer a delay during the disconnect-reconnect sequence (10 ms minimum is needed for it to actually work). The while loops for checking the interrupt pipe ready state doesn’t call usbPoll.

ok delay issue fixed, tyvm for the tip!! but it still hangs when trying to send. here is the new code.

#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() {
  usbInit();
  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() {
  
  UsbKeyboard.update();

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

  if (digitalRead(BUTTON_PIN) == 0) {
    digitalWrite(13, HIGH);
    usbDeviceDisconnect();
    delayMs(5);
    usbDeviceConnect();
    //UsbKeyboard.sendKeyStroke(KEY_B, MOD_GUI_LEFT);
    UsbKeyboard.sendKeyStroke(KEY_A);
#if BYPASS_TIMER_ISR  // check if timer isr fixed.
    delayMs(20);
#else
    delay(20);
#endif
   }
 digitalWrite(13, LOW);
}