This discussion centers on code by:
http://code.rancidbacon.com/ProjectLogArduinoUSB
www.avdweb.nl/arduino/hardware-interfacing/temperature-measurement.html
et al.
ARDUINO 1.0.1
I have built many V-USB toys using the ATtiny85. They all work, they are all fun projects, but they do require that you drop down to C code and use the command line to compile, link, and flash the '85. Rancid Bacon's V-USB library for Arduino Google Code Archive - Long-term storage for Google Code Project Hosting. provides a great enhancement and is easy to utilize, especially the samples he provides.
However, as a HID keyboard emulator, it is awkward to send data to the PC on a keystroke by keystroke method. As an example of this difficulty, I took on the task of reading the internal temperature of the ATmega328P-PU on the UNO from the code library by Albert van Dalen (AvdWeb link above.) I pondered the issues for a while and considered two generalized methods; character array or Strings.
The code below needs to be polished, but works. The hardware required is minimal, two zener diodes, and three resistors. Please see refer to Rancid Bacon's site for the hardware build. I built mine on a UNO prototype shield from Adafruit. The nice thing about this is that you can run both the Arduino GUI and the USB HID port simultaneously. (Just do not connect +5 on the port being used for HID... ground, D+ and D- are all that is needed and reference the +5 back to the UNO main board.)
PLEASE NOTE that my ChipTemp.h file is local for quick changes, not in my user library.
I used a pushbutton on pin 10 so that the program would not start spooling characters across HID after I programmed the UNO and before I could change focus to Notepad with my mouse! Program the UNO, open Notepad and/or click in Notepad to set focus, then press the button on pin 10 to start the program. I cannot attest to the accuracy of the temperature, but I did one test where the UNO was off for an hour and then powered on. The UNO started with a reading near room temperature and the indication was that the temperature climbed higher, 6F degrees total, over a period of about 5 minutes. This leads me to believe that the temperature diode is monitoring the internal temperature.
I had to "mess around" with the ChipTemp.h file and change the calibration values before I could get a decent room temperature. Using a different 'known good' 386P chip on a breadboard resulted in a large change in the calibration value. Your results may vary.
Obviously you can use any temperature sensor and simply change the assignment in the line
sOut.concat(String(chipTemp.fahrenheit()));
The String object is very versatile in Arduino, so have a look at the Reference page
String() - Arduino Reference Have a look at the UsbKeyboard.h file for all of the defined key combinations... this will allow you to build alphanumeric strings for output.
Have fun.
Update:
Here is the same with alphanumeric output:
// MRB 20121201 Tested using String Object
// UNO Profile: Binary sketch size: 7,694 bytes (of a 32,256 byte maximum)
#include "UsbKeyboard.h"
#include <avr/pgmspace.h>
#include "./ChipTemp.h"
#define BUTTON_PIN 10
ChipTemp chipTemp;
String sOut;
void delayMs(unsigned int ms) { // Safe delay
for (int i = 0; i < ms; i++) {
delayMicroseconds(1000);
}
}
void setup() {
pinMode(BUTTON_PIN, INPUT);
digitalWrite(BUTTON_PIN, HIGH);
TIMSK0&=!(1<<TOIE0); // disable timer 0 overflow interrupt (used for millis)
while (digitalRead(BUTTON_PIN) != 0){
UsbKeyboard.update();
delayMs(20);
}
}
void loop() {
digitalWrite(13, !digitalRead(13)); // flash LED
UsbKeyboard.update();
sOut = "INTERNAL CHIP TEMP = ";
sOut.concat(String(chipTemp.fahrenheit())); // chipTemp.celsius() | chipTemp.deciCelsius() | chipTemp.fahrenheit() | chipTemp.deciFahrenheit()
KeyStrokeAlpNum(sOut);
delayMs(20); // Interrupt safe since V-USB utilizes interrupt
}
void KeyStrokeAlpNum(String Sc){
int sPoint = Sc.length(); // Pointer to end-of-String (inc. null)
for (int x = 0; x < sPoint; x++){
int y = x + 1;
if (Sc.substring(x, y) == "A"){ // substring single character: y = x+1
UsbKeyboard.sendKeyStroke(KEY_A, MOD_SHIFT_LEFT);
}
else if (Sc.substring(x, y) == "B"){
UsbKeyboard.sendKeyStroke(KEY_B, MOD_SHIFT_LEFT);
}
else if (Sc.substring(x, y) == "C"){
UsbKeyboard.sendKeyStroke(KEY_C, MOD_SHIFT_LEFT);
}
else if (Sc.substring(x, y) == "D"){
UsbKeyboard.sendKeyStroke(KEY_D, MOD_SHIFT_LEFT);
}
else if (Sc.substring(x, y) == "E"){
UsbKeyboard.sendKeyStroke(KEY_E, MOD_SHIFT_LEFT);
}
else if (Sc.substring(x, y) == "F"){
UsbKeyboard.sendKeyStroke(KEY_F, MOD_SHIFT_LEFT);
}
else if (Sc.substring(x, y) == "G"){
UsbKeyboard.sendKeyStroke(KEY_G, MOD_SHIFT_LEFT);
}
else if (Sc.substring(x, y) == "H"){
UsbKeyboard.sendKeyStroke(KEY_H, MOD_SHIFT_LEFT);
}
else if (Sc.substring(x, y) == "I"){
UsbKeyboard.sendKeyStroke(KEY_I, MOD_SHIFT_LEFT);
}
else if (Sc.substring(x, y) == "J"){
UsbKeyboard.sendKeyStroke(KEY_J, MOD_SHIFT_LEFT);
}
else if (Sc.substring(x, y) == "K"){
UsbKeyboard.sendKeyStroke(KEY_K, MOD_SHIFT_LEFT);
}
else if (Sc.substring(x, y) == "L"){
UsbKeyboard.sendKeyStroke(KEY_L, MOD_SHIFT_LEFT);
}
else if (Sc.substring(x, y) == "M"){
UsbKeyboard.sendKeyStroke(KEY_M, MOD_SHIFT_LEFT);
}
else if (Sc.substring(x, y) == "N"){
UsbKeyboard.sendKeyStroke(KEY_N, MOD_SHIFT_LEFT);
}
else if (Sc.substring(x, y) == "O"){
UsbKeyboard.sendKeyStroke(KEY_O, MOD_SHIFT_LEFT);
}
else if (Sc.substring(x, y) == "P"){
UsbKeyboard.sendKeyStroke(KEY_P, MOD_SHIFT_LEFT);
}
else if (Sc.substring(x, y) == "Q"){
UsbKeyboard.sendKeyStroke(KEY_Q, MOD_SHIFT_LEFT);
}
else if (Sc.substring(x, y) == "R"){
UsbKeyboard.sendKeyStroke(KEY_R, MOD_SHIFT_LEFT);
}
else if (Sc.substring(x, y) == "S"){
UsbKeyboard.sendKeyStroke(KEY_S, MOD_SHIFT_LEFT);
}
else if (Sc.substring(x, y) == "T"){
UsbKeyboard.sendKeyStroke(KEY_T, MOD_SHIFT_LEFT);
}
else if (Sc.substring(x, y) == "U"){
UsbKeyboard.sendKeyStroke(KEY_U, MOD_SHIFT_LEFT);
}
else if (Sc.substring(x, y) == "V"){
UsbKeyboard.sendKeyStroke(KEY_V, MOD_SHIFT_LEFT);
}
else if (Sc.substring(x, y) == "W"){
UsbKeyboard.sendKeyStroke(KEY_W, MOD_SHIFT_LEFT);
}
else if (Sc.substring(x, y) == "X"){
UsbKeyboard.sendKeyStroke(KEY_X, MOD_SHIFT_LEFT);
}
else if (Sc.substring(x, y) == "Y"){
UsbKeyboard.sendKeyStroke(KEY_Y, MOD_SHIFT_LEFT);
}
else if (Sc.substring(x, y) == "Z"){
UsbKeyboard.sendKeyStroke(KEY_Z, MOD_SHIFT_LEFT);
}
else if (Sc.substring(x, y) == " "){
UsbKeyboard.sendKeyStroke(KEY_SPACE);
}
else if (Sc.substring(x, y) == "1"){
UsbKeyboard.sendKeyStroke(KEY_1);
}
else if (Sc.substring(x, y) == "2"){
UsbKeyboard.sendKeyStroke(KEY_2);
}
else if (Sc.substring(x, y) == "3"){
UsbKeyboard.sendKeyStroke(KEY_3);
}
else if (Sc.substring(x, y) == "4"){
UsbKeyboard.sendKeyStroke(KEY_4);
}
else if (Sc.substring(x, y) == "5"){
UsbKeyboard.sendKeyStroke(KEY_5);
}
else if (Sc.substring(x, y) == "6"){
UsbKeyboard.sendKeyStroke(KEY_6);
}
else if (Sc.substring(x, y) == "7"){
UsbKeyboard.sendKeyStroke(KEY_7);
}
else if (Sc.substring(x, y) == "8"){
UsbKeyboard.sendKeyStroke(KEY_8);
}
else if (Sc.substring(x, y) == "9"){
UsbKeyboard.sendKeyStroke(KEY_9);
}
else if (Sc.substring(x, y) == "0"){
UsbKeyboard.sendKeyStroke(KEY_0);
}
}
UsbKeyboard.sendKeyStroke(KEY_ENTER);
// Return
}
Notepad display:
INTERNAL CHIP TEMP 78
INTERNAL CHIP TEMP 78
INTERNAL CHIP TEMP 78
INTERNAL CHIP TEMP 78
INTERNAL CHIP TEMP 78
INTERNAL CHIP TEMP 78
Note: The "=" does not display because I have not mapped this keystroke! The pix shows the simplicity of the USB interface... I have both USB ports active during development (but only 1 active +5V source.)