I would need help for my first arduino-project.
My aim ist to realise following:
I want to built a handheld for my WinXP-driven, cnc-mill. The controllsoftware do not have much shortcuts for important functions. My device should send sequences of keystrokes by bushing only one button. E.g. "alt + A + U". With my modifyed examplesketch (http://www.practicalarduino.com/projects/virtual-usb-keyboard) this works already fine. But I would need more buttons than the Arduino has digital inputs, so i thought I could use a keypad-matrix. When I tried to "merge" the virtual usbkeyboardsketch and the custum keypadsketch (delivered with the arduini IDE) the keypad will not work.
my testsketch has a small 2x2 matrix, the Test-keys are for testing the usb-part. With this sketch the buttons enter, red & blue work fine but my keypad does nothing.
Who could help an arduino-newbie, I can't see why it does not work.
#include <Keypad.h>
#include "UsbKeyboard.h"
// Test Keys
#define BUTTON_ENTER 8
#define BUTTON_RED 9
#define BUTTON_BLUE 10
// definition of the matrix
const byte ROWS = 2;
const byte COLS = 2;
char hexaKeys[ROWS][COLS] = {
{'A','B'},
{'C','D'},
};
byte rowPins[ROWS] = {11, 12};
byte colPins[COLS] = {6, 7};
Keypad cusomKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
// LED für Extra Schalter
int ledPin = 3;
void setup()
{
Serial.begin(9600);
// Initialisieren der LED
pinMode (ledPin, OUTPUT);
digitalWrite (ledPin, HIGH);
pinMode (BUTTON_RED, INPUT);
pinMode (BUTTON_BLUE, INPUT);
pinMode (BUTTON_ENTER, INPUT);
// aktiviert die internen PullUp
digitalWrite (BUTTON_RED, HIGH);
digitalWrite (BUTTON_BLUE, HIGH);
digitalWrite (BUTTON_ENTER, 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();
}
/**
* Main program loop. Scan for keypresses and send a matching keypress
* event to the host
* FIXME: currently repeats as fast as it can. Add transition detection
*/
void loop()
{
// Einlesen der Matrix und serielle Ausgabe des Ergebnisses
char customKey = cusomKeypad.getKey();
if (customKey != NO_KEY){
Serial.println(customKey);
}
// Einlesen der Extraschalter
UsbKeyboard.update();
if (digitalRead(BUTTON_RED) == LOW) {
delayMs(80);
UsbKeyboard.sendKeyStroke(KEY_R);
digitalWrite(ledPin, !digitalRead(ledPin)); // Toggle status LED
}
if (digitalRead(BUTTON_BLUE) == LOW) {
UsbKeyboard.sendKeyStroke(KEY_H, MOD_SHIFT_LEFT);
UsbKeyboard.sendKeyStroke(KEY_E);
UsbKeyboard.sendKeyStroke(KEY_L);
UsbKeyboard.sendKeyStroke(KEY_L);
UsbKeyboard.sendKeyStroke(KEY_O);
UsbKeyboard.sendKeyStroke(KEY_SPACE);
UsbKeyboard.sendKeyStroke(KEY_W, MOD_SHIFT_LEFT);
UsbKeyboard.sendKeyStroke(KEY_O);
UsbKeyboard.sendKeyStroke(KEY_R);
UsbKeyboard.sendKeyStroke(KEY_L);
UsbKeyboard.sendKeyStroke(KEY_D);
UsbKeyboard.sendKeyStroke(KEY_ENTER);
digitalWrite(ledPin, !digitalRead(ledPin)); // Toggle status LED
}
if (digitalRead(BUTTON_ENTER) == LOW) {
delayMs(80);
UsbKeyboard.sendKeyStroke(KEY_E);
digitalWrite(ledPin, !digitalRead(ledPin)); // Toggle status LED
}
}
/**
* 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);
}
}