OK, so I'm still banging my head... Frustration grows and patience thins... but I'm still confident I will one day find the solution to my woes... In an effort to figure out what's going on I have simplified my code as much as possible and cleaned up the libraries which I downloaded to as few files as possible (or as close as I could get it).
Setup
I would like to start over explaining my setup where I am right now, step by step to make sure I'm not missing anything silly and ensure everyone is on the same page.
- I downloaded and installed the latest Arduino IDE 1.0.1
- I added the attached libraries to my arduino/libraries/UsbKeyboard folder
- I built the circuit found on the Practical Arduino website here http://www.practicalarduino.com/projects/virtual-usb-keyboard
- Added to the circuit is a single push button as defined in the Arduino Digital Button example
- The Arduino is powered from the USB cable and I have added a 220uF Capacitor where the USB enters the breadboard
- I added a 1uF Capacitor where the power enters the Arduino +5V rail to try and reduce niose
- i added a 1uF Capacitor where the Arcade Button enters pin 8 of the Arduino
I have compiled and installed the following code onto my Arduino board using the standard AVRISP programming method.
// Derived from this project
// http://www.practicalarduino.com/projects/virtual-usb-keyboard
// Requires the use of the "UsbKeyboard" library available from
// http://code.google.com/p/vusb-for-arduino/
#include "UsbKeyboard.h"
/*
============== PIN CONNECTIONS ==============
D2 - USB (D+)
D4 - USB (D-)
D5 - USB (RESET)
D8 - ARCADE BUTTON
D13 - LED
=============================================
*/
// Use the on-board LED as an activity display
int ledPin = 13;
void setup()
{
// Set up the activity display LED
pinMode (ledPin, OUTPUT);
digitalWrite (ledPin, LOW);
// initialize the pushbutton pins as an inputs
pinMode(buttonPin8, INPUT);
// set the internal pull-up resistors
digitalWrite(buttonPin8, HIGH);
// 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);
// Clear interrupts while performing time-critical operations
cli();
// Force re-enumeration so the host will detect us
usbDeviceDisconnect();
delayMs(250);
usbDeviceConnect();
// Set interrupts again
sei();
// initialization period....
// I find that sending keystrokes too soon can cause the USB connection
// to fail, so instead let's just wait ten seconds while placing updates to USB
for (int initCounter = 0; initCounter < 150; initCounter++) {
delayMs(45);
UsbKeyboard.update();
}
}
void loop()
{
UsbKeyboard.update();
digitalWrite (ledPin, HIGH);
// Send Down Arrow through USB
UsbKeyboard.sendKeyStroke(0x51);
digitalWrite (ledPin, LOW);
delayMs(40);
}
// 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)
{
for (int i = 0; i < ms; i++) {
delayMicroseconds(1000);
}
}
My Problem
So my problem is the action of pushing the button appears to cause enough noise to hang the Arduino (or the USB Bus?). Using the code above, the button is not defined in the sketch and I am not even worried about it's state, I simply send a down keystroke roughly every 50 ms. before calling the UsbKeyboard.sendKeyStroke function I first turn on the LED which I then turn off immediately after the function is complete. Every single time the Arduino hangs, the light is on.
Interestingly I can even remove the orange wire between the button and the Arduino pin and the board will still hang. I believe this may be important as it leads me to believe that it MIGHT noise affecting the USB bus itself.
As usual the floor is open for discussion.
Thanks for any input!
Dave
UsbKeyboard.zip (40.4 KB)
