I have been playing around with a "virtual" USB shield and library – it's pretty incredible. Very very simple, but the power in being able to send HID via Arduino is so rad.
I'm having an issue that I can't identify, when I try to use it in conjunction with an ultrasonic rangefinder. I'm telling it to look at whether the distance is increasing or decreasing, and send a keypress for each if statement. It works 5% of the time at best. I know my sensor is fine and the shield is working with it, I've tested other sketches not using the vUSB library, and I'm getting good readings from the sensor. I also know that the vUSB end of things is working – I'm able to do basics, such as sending a keypress when a button is pressed. It's when I combine the two that it stops working? what am I not seeing here?
#include "UsbKeyboard.h"
// for the usb --------------------------------
// If the timer isr is corrected
// to not take so long change this to 0.
#define BYPASS_TIMER_ISR 1
// for the usb --------------------------------
int aVolt=A1; //naming analog-in 1 as "aVolt"
int distance; // voltage from sensor
int pdistance; // previous voltage
void setup() {
pinMode(aVolt, INPUT); // sets "aVolt" as an input
//timer for usb ---------------------------------------
#if BYPASS_TIMER_ISR
// disable timer 0 overflow interrupt (used for millis)
TIMSK0&=!(1<<TOIE0); // ++
#endif
//timer for usb -----------------------------------------
}
//timer for usb -------------------------------------------
#if BYPASS_TIMER_ISR
void delayMs(unsigned int ms) {
/*
*/
for (int i = 0; i < ms; i++) {
delayMicroseconds(1000);
}
}
#endif
//timer for usb -------------------------------------------
void loop() {
UsbKeyboard.update();
distance = analogRead(aVolt); //defines "distance" as the analog voltage input to aVolt
// comenting this out for now // distance = map(distance,170,20,0,32);
// commenting this out for now // distance = constrain(distance,0,32);
digitalWrite(13, !digitalRead(13)); // don't know what this is for
if (distance > pdistance) { // if you move further away
UsbKeyboard.sendKeyStroke(KEY_F10); // F10 is pressed
#if BYPASS_TIMER_ISR // check if timer isr fixed.
delayMs(20);
#else
delay(20);
#endif
}
if (distance < pdistance) { // if you move closer
UsbKeyboard.sendKeyStroke(KEY_F12); // F12 is pressed
#if BYPASS_TIMER_ISR // check if timer isr fixed.
delayMs(20);
#else
delay(20);
#endif
}
pdistance = distance; // stores the value of distance
}
Ideas?
Here's the library: Google Code Archive - Long-term storage for Google Code Project Hosting.
Here's the shield: practicalarduino.com
I'm using an UNO and IDE v.21
The library is known to work in v.20, which I tried to no avail.
plz ignore the iron-burn on the side of the sensor :