Hallo!
Ich habe mir ein KeyPad (Tasten 1,2,...,9,*,0,#) selber gebaut. Dazu habe ich mir auch eine Library erstellt. Das Funktioniert gut. Nun möchte ich, dass die Taste, die ich gedrückt habe, über den Arduino micro über die Keyboard.press Funktion an den Pc gesendet wird. Das Funktioniert nur bei den Zahlen. Wenn ich aber * drücke, wird ( gesendet. Wenn ich # drücke, wird § gesendet. Wie kann das sein? Danke im Voraus.
PBahner
Weil du was falsch machst.
Und jetzt verrate ich dir ein geheimes Geheimnis:
Ich sehe nicht, was du falsch machst.
Bist übrigens nicht der Erste, mit dem Problem
Auch hier
Der gesendete Tastaturcode entspricht nicht ganz dem Ashii-Code. Außerdem hängt das Empfangene Zeichen noch von der eingestellten Sprache in PC ab.
https://www.arduino.cc/en/Reference/KeyboardModifiers
https://forum.arduino.cc/index.php?topic=418813.0
https://www.sparkfun.com/tutorials/337
Diese Infos könnten genauer sein wenn wir Deinen Sketch kennen würden.
Grüße Uwe
Hallo combie und uwefed.
Das "Keyboard_DE" habe ich getestet. Für meinen Sketch funktioniert das leider nicht, da ich mir eine Library für mein KeyPad erstellt habe, indem die Tasten gespeichert sind.
Hier mal die Library:
AnalogKeypad.h:
#include <WProgramm.h>
#else
#include <Arduino.h>
#endif
#define KEY_NOT_PRESSED '-'
class AnalogKeyPad{
public:
AnalogKeyPad(byte analogPin);
void setDebounceTime(unsigned int debounceTime);
void setThresholdValue(byte tv);
void setKey(byte Key, byte KeyWert);
char readKey();
private:
byte analogPin;
unsigned int debounceTime;
long lastValue;
byte threshold;
byte KEY_1 = '1';
byte KEY_2 = '2';
byte KEY_3 = '3';
byte KEY_4 = '4';
byte KEY_5 = '5';
byte KEY_6 = '6';
byte KEY_7 = '7';
byte KEY_8 = '8';
byte KEY_9 = '9';
byte KEY_0 = '0';
byte KEY_STAR = '*';
byte KEY_HASH = '#';
};
#endif
AnalogKeyPad.cpp:
#include "AnalogKeyPad.h"
AnalogKeyPad::AnalogKeyPad(byte ap){
analogPin = ap;
debounceTime = 300;
threshold = 5;
}
void AnalogKeyPad::setDebounceTime(unsigned int time){
debounceTime = time;
}
void AnalogKeyPad::setThresholdValue(byte tv){
threshold = tv;
}
void AnalogKeyPad::setKey(byte Key,byte KeyWert){
if (Key == 1){
KEY_1 = KeyWert;
}
if (Key == 2){
KEY_2 = KeyWert;
}
if (Key == 3){
KEY_3 = KeyWert;
}
if (Key == 4){
KEY_4 = KeyWert;
}
if (Key == 5){
KEY_5 = KeyWert;
}
if (Key == 6){
KEY_6 = KeyWert;
}
if (Key == 7){
KEY_7 = KeyWert;
}
if (Key == 8){
KEY_8 = KeyWert;
}
if (Key == 9){
KEY_9 = KeyWert;
}
if (Key == 10){
KEY_STAR = KeyWert;
}
if (Key == 11){
KEY_0 = KeyWert;
}
if (Key == 12){
KEY_HASH = KeyWert;
}
}
char AnalogKeyPad::readKey(){
char key = KEY_NOT_PRESSED;
byte aValue = analogRead(analogPin);
if((aValue > 0)&&(millis() - lastValue >= debounceTime)){
if((aValue > (176 - threshold)) && (aValue < (176 + threshold))) key = KEY_1;
if((aValue > (163 - threshold)) && (aValue < (163 + threshold))) key = KEY_2;
if((aValue > (149 - threshold)) && (aValue < (149 + threshold))) key = KEY_3;
if((aValue > (136 - threshold)) && (aValue < (136 + threshold))) key = KEY_4;
if((aValue > (122 - threshold)) && (aValue < (122 + threshold))) key = KEY_5;
if((aValue > (108 - threshold)) && (aValue < (108 + threshold))) key = KEY_6;
if((aValue > (94 - threshold)) && (aValue < (94 + threshold))) key = KEY_7;
if((aValue > (79 - threshold)) && (aValue < (79 + threshold))) key = KEY_8;
if((aValue > (64 - threshold)) && (aValue < (64 + threshold))) key = KEY_9;
if((aValue > (48 - threshold)) && (aValue < (48 + threshold))) key = KEY_STAR;
if((aValue > (32 - threshold)) && (aValue < (32 + threshold))) key = KEY_0;
if((aValue > (15 - threshold)) && (aValue < (15 + threshold))) key = KEY_HASH;
lastValue = millis();
}
return key;
}
und noch mein Sketch:
#include "Keyboard.h"
#include "AnalogKeyPad.h"
#define analogPin 8
AnalogKeyPad myOwnKeyPad(analogPin);
void setup() {
Keyboard.begin();
myOwnKeyPad.setDebounceTime(500);
//Serial.begin(9600);
}
void loop(){
//Keypad
char myKey = myOwnKeyPad.readKey();
if(myKey != KEY_NOT_PRESSED){
Keyboard.press(myKey);
delay(200);
Keyboard.release(myKey);
//Serial.println(myKey);
}
bei der Funktion Serial.println(myKey) geht das Wunderbar. Wie kann ich das am besten machen? Was die Keys vom KeyPad senden sollen, kann ich über die Funktion "setKey(Key, zuSendenderTastaturCode)" einfach ändern.
PBahner
Du mußt
byte KEY_STAR = '*'; in der AnalogKeypad.h:
durch den richtigen Scancode ersetzen.
Könnte "}" sein.
Grüße Uwe
Danke bei '*' hat '}' funktioniert und bei '#' hab ich den Scancode 185 eingegeben.
PBahner