Example of reading all 6 of the default analog (port C0 - C5) values:
(Still uses pushbutton on PIN 10 to start the process to give me time to setup the notepad focus in Windows)
Updated to show use of for() loop construct:
// 20121208 Runs on UNO-clone strip board
// UNO Profile: Binary sketch size: 7,486 bytes (of a 32,256 byte maximum)
#include "UsbKeyboard.h"
#include <avr/pgmspace.h>
#define BUTTON_PIN 10
//const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
String sOut;
void delayMs(unsigned int ms) { // Safe delay helper function
for (int i = 0; i < ms; i++) {
delayMicroseconds(1000);
}
}
void setup() {
pinMode(BUTTON_PIN, INPUT);
digitalWrite(BUTTON_PIN, 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();
while (digitalRead(BUTTON_PIN) != 0){
UsbKeyboard.update();
delayMs(20);
}
}
void loop() {
digitalWrite(13, !digitalRead(13)); // flash LED
UsbKeyboard.update();
for (int i = 0; i < 6; i++){
// read the analog in value:
sensorValue = analogRead( i );
// Label the value
sOut = "ANALOG";
// String the characters together
sOut.concat(String(i));
sOut.concat(" ");
sOut.concat(String(sensorValue));
// spool the combined strings
KeyStrokeAlpNum(sOut);
UsbKeyboard.sendKeyStroke(KEY_ENTER);
}
UsbKeyboard.sendKeyStroke(KEY_ENTER);
delayMs(5000); // 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
}
Example of the resulting output in notepad:
(I walked each analog pin with a 10K resistor to ground... other pins were floating.)
ANALOG0 0
ANALOG1 120
ANALOG2 220
ANALOG3 265
ANALOG4 294
ANALOG5 294ANALOG0 296
ANALOG1 0
ANALOG2 109
ANALOG3 213
ANALOG4 268
ANALOG5 282ANALOG0 313
ANALOG1 315
ANALOG2 0
ANALOG3 120
ANALOG4 217
ANALOG5 261ANALOG0 312
ANALOG1 332
ANALOG2 313
ANALOG3 0
ANALOG4 128
ANALOG5 216ANALOG0 290
ANALOG1 322
ANALOG2 320
ANALOG3 312
ANALOG4 0
ANALOG5 120ANALOG0 120
ANALOG1 234
ANALOG2 272
ANALOG3 297
ANALOG4 282
ANALOG5 0