Kann dir leider nicht folgen...... ![]()
void setup() {
Serial.begin(250000);
if (HIGH == LOW);
Serial.println(F("LOW ist anscheinend gleich HIGH (mit Semikolon ;)"));
if (HIGH == LOW)
Serial.println(F("LOW ist anscheinend gleich HIGH (ohne Semikolon))"));
if (HIGH == LOW) {
Serial.println(F("LOW ist anscheinend gleich HIGH (mit Klammern {})"));
}
}
void loop() {}
LOW ist anscheinend gleich HIGH (mit Semikolon ;)
OK, Danke.
Grundsätzlich funktioniert das ganze ja jetzt wie folgt schon sehr gut.
void loop() {
// read the pushbutton:
int buttonState2 = digitalRead(buttonPin2);
// if the button state has changed,
while ((buttonState2 != previousButtonState2)
// and it's currently pressed:
&& (buttonState2 == LOW)){
// type out a message
Keyboard.press(KEY_LEFT_ARROW);
delay(30);
Keyboard.release(KEY_LEFT_ARROW);
delay(30);
buttonState2 = digitalRead(buttonPin2);
}
Man drückt den Button und die Schleife läuft so lange bis man los lässt.
Was ich allerdings noch benötigen würde wäre folgendes:
Button wird gedrückt --> Dann soll nur ein Tastendruck ausgeführt werden --> dann soll eine einstellbare Wartezeit folgen --> dann soll geprüft werden ob der Status vom Button noch immer gedrückt ist --> wenn dies der Fall ist dann soll eine Schleife zum laufen beginnen wie oben.
Um das Abzubilden bin ich noch komplett rahtlos.
Vielen Dank an Euch ....
Ich bastele an keinen while/delay Programmen rum, sorry.
OK, trotzdem vielen Dank für deine Hilfe. Hoffe es fällt sonst noch jemandem eine Lösung für meine gewünschte Abfolge ein. ![]()
Dann setze Deinen Code bitte erst mal in Codetags (</>-Button oben links im Forumseditor), dann ist er für alle (besonders die Mobilteilnehmer) besser lesbar.
Gruß Tommy
Sorry, hab ich nicht gewusst. Hier der gesamte Code in hoffentlich richtiger Form. Danke
#include "Keyboard.h"
const int buttonPin2 = 2; // input pin for pushbutton
const int buttonPin3 = 3;
const int buttonPin4 = 4;
const int buttonPin5 = 5;
const int buttonPin6 = 6;
const int buttonPin7 = 7;
const int buttonPin8 = 8;
const int buttonPin9 = 9;
const int buttonPin10 = 10;
int previousButtonState2 = LOW; // for checking the state of a pushButton
int previousButtonState3 = LOW;
int previousButtonState4 = LOW;
int previousButtonState5 = LOW;
int previousButtonState6 = LOW;
int previousButtonState7 = LOW;
int previousButtonState8 = LOW;
int previousButtonState9 = LOW;
int previousButtonState10 = LOW;
void setup() {
// make the pushButton pin an input:
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(buttonPin4, INPUT_PULLUP);
pinMode(buttonPin5, INPUT_PULLUP);
pinMode(buttonPin6, INPUT_PULLUP);
pinMode(buttonPin7, INPUT_PULLUP);
pinMode(buttonPin8, INPUT_PULLUP);
pinMode(buttonPin9, INPUT_PULLUP);
pinMode(buttonPin10, INPUT_PULLUP);
// initialize control over the keyboard:
Keyboard.begin();
}
void loop() {
// read the pushbutton:
int buttonState2 = digitalRead(buttonPin2);
// if the button state has changed,
while ((buttonState2 != previousButtonState2)
// and it's currently pressed:
&& (buttonState2 == LOW)){
// type out a message
Keyboard.press(KEY_LEFT_ARROW);
delay(30);
Keyboard.release(KEY_LEFT_ARROW);
delay(30);
buttonState2 = digitalRead(buttonPin2);
}
// read the pushbutton:
int buttonState3 = digitalRead(buttonPin3);
// if the button state has changed,
if ((buttonState3 != previousButtonState3)
// and it's currently pressed:
&& (buttonState3 == LOW)) {
// type out a message
Keyboard.press(KEY_RIGHT_ARROW);
delay(30);
Keyboard.release(KEY_RIGHT_ARROW);
delay(31);
if (buttonState3 = HIGH)
Keyboard.press(KEY_RIGHT_ARROW);
delay(30);
Keyboard.release(KEY_RIGHT_ARROW);
delay(31);
}
// read the pushbutton:
int buttonState4 = digitalRead(buttonPin4);
// if the button state has changed,
if ((buttonState4 != previousButtonState4)
// and it's currently pressed:
&& (buttonState4 == LOW)) {
// type out a message
Keyboard.press(KEY_UP_ARROW);
delay(30);
Keyboard.release(KEY_UP_ARROW);
delay(31);
if (buttonState4 = HIGH)
Keyboard.press(KEY_UP_ARROW);
delay(30);
Keyboard.release(KEY_UP_ARROW);
delay(31);
}
// read the pushbutton:
int buttonState5 = digitalRead(buttonPin5);
// if the button state has changed,
if ((buttonState5 != previousButtonState5)
// and it's currently pressed:
&& (buttonState5 == LOW)) {
// type out a message
Keyboard.press(KEY_DOWN_ARROW);
delay(30);
Keyboard.release(KEY_DOWN_ARROW);
delay(31);
if (buttonState5 = HIGH)
Keyboard.press(KEY_DOWN_ARROW);
delay(30);
Keyboard.release(KEY_DOWN_ARROW);
delay(31);
}
// read the pushbutton:
int buttonState6 = digitalRead(buttonPin6);
// if the button state has changed,
if ((buttonState6 != previousButtonState6)
// and it's currently pressed:
&& (buttonState6 == LOW)) {
// type out a message
Keyboard.press(KEY_PAGE_UP);
delay(30);
Keyboard.release(KEY_PAGE_UP);
delay(31);
if (buttonState6 = HIGH)
Keyboard.press(KEY_PAGE_UP);
delay(30);
Keyboard.release(KEY_PAGE_UP);
delay(31);
}
// read the pushbutton:
int buttonState7 = digitalRead(buttonPin7);
// if the button state has changed,
if ((buttonState7 != previousButtonState7)
// and it's currently pressed:
&& (buttonState7 == LOW)) {
// type out a message
Keyboard.press(KEY_PAGE_DOWN);
delay(30);
Keyboard.release(KEY_PAGE_DOWN);
delay(31);
if (buttonState7 = HIGH)
Keyboard.press(KEY_PAGE_DOWN);
delay(30);
Keyboard.release(KEY_PAGE_DOWN);
delay(31);
}
// read the pushbutton:
int buttonState8 = digitalRead(buttonPin8);
// if the button state has changed,
if ((buttonState8 != previousButtonState8)
// and it's currently pressed:
&& (buttonState8 == LOW)) {
// type out a message
Keyboard.press(KEY_F4);
delay(30);
Keyboard.release(KEY_F4);
}
// read the pushbutton:
int buttonState9 = digitalRead(buttonPin9);
// if the button state has changed,
if ((buttonState9 != previousButtonState9)
// and it's currently pressed:
&& (buttonState9 == LOW)) {
// type out a message
Keyboard.print("7");
}
// read the pushbutton:
int buttonState10 = digitalRead(buttonPin10);
// if the button state has changed,
if ((buttonState10 != previousButtonState10)
// and it's currently pressed:
&& (buttonState10 == LOW)) {
// type out a message
Keyboard.print("8");
}
// save the current button state for comparison next time:
previousButtonState2 = buttonState2;
previousButtonState3 = buttonState3;
previousButtonState4 = buttonState4;
previousButtonState5 = buttonState5;
previousButtonState6 = buttonState6;
previousButtonState7 = buttonState7;
previousButtonState8 = buttonState8;
previousButtonState9 = buttonState9;
previousButtonState10 = buttonState10;
}
/*
KEY_LEFT_CTRL
KEY_LEFT_SHIFT
KEY_LEFT_ALT
KEY_LEFT_GUI
KEY_RIGHT_CTRL
KEY_RIGHT_SHIFT
KEY_RIGHT_ALT
KEY_RIGHT_GUI
KEY_UP_ARROW
KEY_DOWN_ARROW
KEY_LEFT_ARROW
KEY_RIGHT_ARROW
KEY_BACKSPACE
KEY_TAB
KEY_RETURN
KEY_ESC
KEY_INSERT
KEY_DELETE
KEY_PAGE_UP
KEY_PAGE_DOWN
KEY_HOME
KEY_END
KEY_CAPS_LOCK
KEY_F1
KEY_F2
KEY_F3
KEY_F4
KEY_F5
KEY_F6
KEY_F7
KEY_F8
KEY_F9
KEY_F10
KEY_F11
KEY_F12
*/
Wenn Du Deinen Code ordentlich formatierst, wird er übersichtlicher. (+T in der IDE hilft Dir dabei).
Für Deine vielen Buttons würde sich ein Array aus Structs anbieten, auch wenn die Pins derzeit in Reihenfolge sind:
typedef struct button {
byte pin;
byte state;
};
const byte ANZAHL = 9;
button buttons[ANZAHL] = {{2,LOW},{3,LOW},{4,LOW},{5,LOW},{6,LOW},{7,LOW},{8,LOW},{9,LOW},{10,LOW}};
void setup() {
for(byte i=0; i<ANZAHL;i++) {
pinMode(buttons[i].pin,INPUT_PULLUP);
}
}
Das bringt auch mehr Übersicht.
Gruß Tommy
Danke.
Ich stelle hier meinen bisherigen Code nochmal ein. Ich habe vor ein Keypad mit Pushbuttons zu machen. 4 Buttons für die Richtungspfeile. weitere 4 Buttons für die Richtungspfeile, wobei hier gleichzeitig die linke ALT Taste gedrückt werden soll (also ALT+LEFT ARROW) 2 Buttons für Bild Auf und Bild AB und zwei Buttons für F4 und F5 Taste.
Grundsätzlich funktioniert die Tastenausgabe. Es dürfte aber entweder das Timing oder die Schleife, wenn man den Button gedrückt hält nicht passen, weil einige Programme den Input nicht registrieren und andere wieder schon.
Wie gesagt. Ich bin kein Programmierer und hab mir diesen Code von vielen zusammenkopiert und halt mal zum Laufen gebracht.
Wäre super wenn mir jemand bei dem beschriebenen Problem weiter helfen könnte.
Vielen Dank
//Tastatur Simulation mit Pfeiltasten usw.
//Hannes Dobersberger
//28.04.2018
#include "Keyboard.h"
// Input Definition für die Taster
const int buttonPin2 = 2;
const int buttonPin3 = 3;
const int buttonPin4 = 4;
const int buttonPin5 = 5;
const int buttonPin6 = 6;
const int buttonPin7 = 7;
const int buttonPin8 = 8;
const int buttonPin9 = 9;
const int buttonPin10 = 10;
const int buttonPin11 = 11;
const int buttonPin12 = 12;
// Zum Überprüfen des Status der einzelnen Taster
int previousButtonState2 = LOW;
int previousButtonState3 = LOW;
int previousButtonState4 = LOW;
int previousButtonState5 = LOW;
int previousButtonState6 = LOW;
int previousButtonState7 = LOW;
int previousButtonState8 = LOW;
int previousButtonState9 = LOW;
int previousButtonState10 = LOW;
int previousButtonState11 = LOW;
int previousButtonState12 = LOW;
//Aus den Tastern einen Input machen.
//Pullup ist für Verbindung zu Ground und nicht zu 5V.
void setup() {
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(buttonPin4, INPUT_PULLUP);
pinMode(buttonPin5, INPUT_PULLUP);
pinMode(buttonPin6, INPUT_PULLUP);
pinMode(buttonPin7, INPUT_PULLUP);
pinMode(buttonPin8, INPUT_PULLUP);
pinMode(buttonPin9, INPUT_PULLUP);
pinMode(buttonPin10, INPUT_PULLUP);
pinMode(buttonPin11, INPUT_PULLUP);
pinMode(buttonPin12, INPUT_PULLUP);
//Keyboard Steuerung wird inizialisiert, gestartet.
Keyboard.begin();
}
//Definition der einzelnen Taster.
void loop() {
// read the pushbutton:
int buttonState2 = digitalRead(buttonPin2);
// if the button state has changed,
while ((buttonState2 != previousButtonState2)
// and it's currently pressed:
&& (buttonState2 == LOW)) {
// type out a message
Keyboard.press(KEY_LEFT_ARROW);
delay(30);
Keyboard.release(KEY_LEFT_ARROW);
delay(30);
buttonState2 = digitalRead(buttonPin2);
}
// read the pushbutton:
int buttonState3 = digitalRead(buttonPin3);
// if the button state has changed,
while ((buttonState3 != previousButtonState3)
// and it's currently pressed:
&& (buttonState3 == LOW)) {
// type out a message
Keyboard.press(KEY_RIGHT_ARROW);
delay(30);
Keyboard.release(KEY_RIGHT_ARROW);
delay(31);
buttonState3 = digitalRead(buttonPin3);
}
// read the pushbutton:
int buttonState4 = digitalRead(buttonPin4);
// if the button state has changed,
while ((buttonState4 != previousButtonState4)
// and it's currently pressed:
&& (buttonState4 == LOW)) {
// type out a message
Keyboard.press(KEY_UP_ARROW);
delay(30);
Keyboard.release(KEY_UP_ARROW);
delay(31);
buttonState4 = digitalRead(buttonPin4);
}
// read the pushbutton:
int buttonState5 = digitalRead(buttonPin5);
// if the button state has changed,
while ((buttonState5 != previousButtonState5)
// and it's currently pressed:
&& (buttonState5 == LOW)) {
// type out a message
Keyboard.press(KEY_DOWN_ARROW);
delay(30);
Keyboard.release(KEY_DOWN_ARROW);
delay(31);
buttonState5 = digitalRead(buttonPin5);
}
//Ab hier die Pfeiltasten mit Linker ALT
// read the pushbutton:
int buttonState6 = digitalRead(buttonPin6);
// if the button state has changed,
while ((buttonState6 != previousButtonState6)
// and it's currently pressed:
&& (buttonState6 == LOW)) {
// type out a message
Keyboard.press(KEY_LEFT_ALT);
delay(30);
Keyboard.press(KEY_LEFT_ARROW);
delay(30);
Keyboard.releaseAll();
delay(30);
buttonState6 = digitalRead(buttonPin6);
}
// read the pushbutton:
int buttonState7 = digitalRead(buttonPin7);
// if the button state has changed,
while ((buttonState7 != previousButtonState7)
// and it's currently pressed:
&& (buttonState7 == LOW)) {
// type out a message
Keyboard.press(KEY_LEFT_ALT);
delay(30);
Keyboard.press(KEY_RIGHT_ARROW);
delay(30);
Keyboard.releaseAll();
delay(30);
buttonState7 = digitalRead(buttonPin7);
}
// read the pushbutton:
int buttonState8 = digitalRead(buttonPin8);
// if the button state has changed,
while ((buttonState8 != previousButtonState8)
// and it's currently pressed:
&& (buttonState8 == LOW)) {
// type out a message
Keyboard.press(KEY_LEFT_ALT);
delay(30);
Keyboard.press(KEY_UP_ARROW);
delay(30);
Keyboard.releaseAll();
delay(30);
buttonState8 = digitalRead(buttonPin8);
}
// read the pushbutton:
int buttonState9 = digitalRead(buttonPin9);
// if the button state has changed,
while ((buttonState9 != previousButtonState9)
// and it's currently pressed:
&& (buttonState9 == LOW)) {
// type out a message
Keyboard.press(KEY_LEFT_ALT);
delay(30);
Keyboard.press(KEY_DOWN_ARROW);
delay(30);
Keyboard.releaseAll();
delay(30);
buttonState9 = digitalRead(buttonPin9);
}
//Ab hier die Bild auf/ab und F Tasten
// read the pushbutton:
int buttonState10 = digitalRead(buttonPin10);
// if the button state has changed,
if ((buttonState10 != previousButtonState10)
// and it's currently pressed:
&& (buttonState10 == LOW)) {
// type out a message
Keyboard.press(KEY_PAGE_UP);
delay(30);
Keyboard.release(KEY_PAGE_UP);
delay(30);
buttonState10 = digitalRead(buttonPin10);
}
// read the pushbutton:
int buttonState11 = digitalRead(buttonPin11);
// if the button state has changed,
if ((buttonState11 != previousButtonState11)
// and it's currently pressed:
&& (buttonState11 == LOW)) {
// type out a message
Keyboard.press(KEY_PAGE_DOWN);
delay(30);
Keyboard.release(KEY_PAGE_DOWN);
delay(30);
buttonState11 = digitalRead(buttonPin11);
}
// read the pushbutton:
int buttonState12 = digitalRead(buttonPin12);
// if the button state has changed,
if ((buttonState12 != previousButtonState12)
// and it's currently pressed:
&& (buttonState12 == LOW)) {
// type out a message
Keyboard.press(KEY_F4);
delay(30);
Keyboard.release(KEY_F4);
delay(30);
buttonState12 = digitalRead(buttonPin12);
}
//Speichert aktuellen Status der Taster für den nächsten Vergleich.
previousButtonState2 = buttonState2;
previousButtonState3 = buttonState3;
previousButtonState4 = buttonState4;
previousButtonState5 = buttonState5;
previousButtonState6 = buttonState6;
previousButtonState7 = buttonState7;
previousButtonState8 = buttonState8;
previousButtonState9 = buttonState9;
previousButtonState10 = buttonState10;
previousButtonState11 = buttonState11;
previousButtonState12 = buttonState12;
}
//Richtige Schreibweise der Tastenbefehle, als Muster angehängt.
/*
KEY_LEFT_CTRL
KEY_LEFT_SHIFT
KEY_LEFT_ALT
KEY_LEFT_GUI
KEY_RIGHT_CTRL
KEY_RIGHT_SHIFT
KEY_RIGHT_ALT
KEY_RIGHT_GUI
KEY_UP_ARROW
KEY_DOWN_ARROW
KEY_LEFT_ARROW
KEY_RIGHT_ARROW
KEY_BACKSPACE
KEY_TAB
KEY_RETURN
KEY_ESC
KEY_INSERT
KEY_DELETE
KEY_PAGE_UP
KEY_PAGE_DOWN
KEY_HOME
KEY_END
KEY_CAPS_LOCK
KEY_F1
KEY_F2
KEY_F3
KEY_F4
KEY_F5
KEY_F6
KEY_F7
KEY_F8
KEY_F9
KEY_F10
KEY_F11
KEY_F12
*/
Hier ein Sketch, der bei mir funktioniert.
So ähnlich, wie deine Version (also noch ohne die elegante Variante mit Array aus Structs).
Es sind erst mal nur 3 Tasten. Wenn du mehr brauchst kannst du es ja erweitern.
#include "Keyboard.h"
const byte buttonPin8 = 8; // KEY_LEFT_ARROW
const byte buttonPin9 = 9; // KEY_RIGHT_ARROW
const byte buttonPin10 = 10; // KEY_LEFT_ALT + KEY_LEFT_ARROW
boolean previousButtonState8 = LOW;
boolean previousButtonState9 = LOW;
boolean previousButtonState10 = LOW;
unsigned long lastMillis;
unsigned long waitTime = 40; // Entprellzeit
void setup() {
pinMode(buttonPin8, INPUT_PULLUP);
pinMode(buttonPin9, INPUT_PULLUP);
pinMode(buttonPin10, INPUT_PULLUP);
Keyboard.begin();
}
void loop() {
// Einlesen der Taster
boolean buttonState8 = digitalRead(buttonPin8);
boolean buttonState9 = digitalRead(buttonPin9);
boolean buttonState10 = digitalRead(buttonPin10);
// Auswertung und Ausgabe
if ( (buttonState8 != previousButtonState8) && (millis() - lastMillis > waitTime) ) {
if (buttonState8 == LOW) { // Taste wurde gedrückt
Keyboard.press(KEY_LEFT_ARROW);
lastMillis = millis();
}
else { // Taste wurde losgelassen
Keyboard.release(KEY_LEFT_ARROW);
lastMillis = millis();
}
}
if ( (buttonState9 != previousButtonState9) && (millis() - lastMillis > waitTime) ) {
if (buttonState9 == LOW) {
Keyboard.press(KEY_RIGHT_ARROW);
lastMillis = millis();
}
else {
Keyboard.release(KEY_RIGHT_ARROW);
lastMillis = millis();
}
}
if ( (buttonState10 != previousButtonState10) && (millis() - lastMillis > waitTime) ) {
if (buttonState10 == LOW) {
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_ARROW);
lastMillis = millis();
}
else {
Keyboard.release(KEY_LEFT_ALT);
Keyboard.release(KEY_LEFT_ARROW);
lastMillis = millis();
}
}
previousButtonState8 = buttonState8;
previousButtonState9 = buttonState9;
previousButtonState10 = buttonState10;
}
Ein paar Hinweise:
Autowiederholung
- also jene Funktion, die dafür sorgt, dass länger gedrückte Tasten "ständig wiederholt" werden - darum muss man sich nicht extra kümmern, wenn der Leonardo/Micro an einen "normalen" PC angeschlossen wird. Denn der PC kümmert sich schon selbst darum. Das Programm muss sich aber so verhalten, wie ein "normales" Keyboard - das ist beim Beispiel oben der Fall. Allerdings hängt das Verhalten auch ein wenig vom verwendeten Betriebssystem ab (Windows reagiert zum Beispiel etwas anders als ein Mac).
Mehrere Tasten gleichzeitig
Das Beispiel oben funktioniert gut, wenn du niemals mehr als eine Taste gleichzeitig drücken willst. Ich spreche von Tasten(Buttons, Pushbuttons) am Arduino und nicht von "Keyboard-Tasten".
Meistens macht es ja auch relativ wenig Sinn PfeilLinks und PfeilRechts gleichzeitig zu drücken, aber bei bestimmten Programmen sind auch solche "eigenartigen" Tastenkombinationen möglich.
Wenn du im Beispiel oben mehrere Tasten gleichzeitig drückst, wird immer eine Taste "dominant".
Falls du also mehrere Tasten gleichzeitig verwenden willst, muss man das wahrscheinlich anders lösen, falls immer nur eine Taste gedrückt werden soll, dann kann es so funktionieren.
Hallo,
Vielen Dank für dein Code-Beispiel. Funktioniert perfekt. Nun auch mit den Programmen, wo es vorher noch nicht geklappt hat. Wird nämlich eine CNC-Fernbedienung.
Hier noch mein gesamter Code:
PS: Kann man vom Arduino Leonardo noch mehr Pins als Input verwenden, außer die Pins 2 - 10 und 14 - 16?
#include "Keyboard.h"
const byte buttonPin2 = 2; // KEY_LEFT_ARROW
const byte buttonPin3 = 3; // KEY_RIGHT_ARROW
const byte buttonPin4 = 4; // KEY_UP_ARROW
const byte buttonPin5 = 5; // KEY_DOWN_ARROW
const byte buttonPin6 = 6; // KEY_LEFT_ALT + KEY_LEFT_ARROW
const byte buttonPin7 = 7; // KEY_LEFT_ALT + KEY_RIGHT_ARROW
const byte buttonPin8 = 8; // KEY_LEFT_ALT + KEY_UP_ARROW
const byte buttonPin9 = 9; // KEY_LEFT_ALT + KEY_DOWN_ARROW
const byte buttonPin10 = 10; // KEY_PAGE_UP
const byte buttonPin11 = 11; // KEY_PAGE_DOWN
const byte buttonPin12 = 12; // KEY_F4
boolean previousButtonState2 = LOW;
boolean previousButtonState3 = LOW;
boolean previousButtonState4 = LOW;
boolean previousButtonState5 = LOW;
boolean previousButtonState6 = LOW;
boolean previousButtonState7 = LOW;
boolean previousButtonState8 = LOW;
boolean previousButtonState9 = LOW;
boolean previousButtonState10 = LOW;
boolean previousButtonState11 = LOW;
boolean previousButtonState12 = LOW;
unsigned long lastMillis;
unsigned long waitTime = 40; // Entprellzeit
void setup() {
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(buttonPin4, INPUT_PULLUP);
pinMode(buttonPin5, INPUT_PULLUP);
pinMode(buttonPin6, INPUT_PULLUP);
pinMode(buttonPin7, INPUT_PULLUP);
pinMode(buttonPin8, INPUT_PULLUP);
pinMode(buttonPin9, INPUT_PULLUP);
pinMode(buttonPin10, INPUT_PULLUP);
pinMode(buttonPin11, INPUT_PULLUP);
pinMode(buttonPin12, INPUT_PULLUP);
Keyboard.begin();
}
void loop() {
// Einlesen der Taster
boolean buttonState2 = digitalRead(buttonPin2);
boolean buttonState3 = digitalRead(buttonPin3);
boolean buttonState4 = digitalRead(buttonPin4);
boolean buttonState5 = digitalRead(buttonPin5);
boolean buttonState6 = digitalRead(buttonPin6);
boolean buttonState7 = digitalRead(buttonPin7);
boolean buttonState8 = digitalRead(buttonPin8);
boolean buttonState9 = digitalRead(buttonPin9);
boolean buttonState10 = digitalRead(buttonPin10);
boolean buttonState11 = digitalRead(buttonPin11);
boolean buttonState12 = digitalRead(buttonPin12);
// Auswertung und Ausgabe
//Button 2
if ( (buttonState2 != previousButtonState2) && (millis() - lastMillis > waitTime) ) {
if (buttonState2 == LOW) { // Taste wurde gedrückt
Keyboard.press(KEY_LEFT_ARROW);
lastMillis = millis();
}
else { // Taste wurde losgelassen
Keyboard.release(KEY_LEFT_ARROW);
lastMillis = millis();
}
}
//Button 3
if ( (buttonState3 != previousButtonState3) && (millis() - lastMillis > waitTime) ) {
if (buttonState3 == LOW) { // Taste wurde gedrückt
Keyboard.press(KEY_RIGHT_ARROW);
lastMillis = millis();
}
else { // Taste wurde losgelassen
Keyboard.release(KEY_RIGHT_ARROW);
lastMillis = millis();
}
}
//Button 4
if ( (buttonState4 != previousButtonState4) && (millis() - lastMillis > waitTime) ) {
if (buttonState4 == LOW) { // Taste wurde gedrückt
Keyboard.press(KEY_UP_ARROW);
lastMillis = millis();
}
else { // Taste wurde losgelassen
Keyboard.release(KEY_UP_ARROW);
lastMillis = millis();
}
}
//Button 5
if ( (buttonState5 != previousButtonState5) && (millis() - lastMillis > waitTime) ) {
if (buttonState5 == LOW) { // Taste wurde gedrückt
Keyboard.press(KEY_DOWN_ARROW);
lastMillis = millis();
}
else { // Taste wurde losgelassen
Keyboard.release(KEY_DOWN_ARROW);
lastMillis = millis();
}
}
//Button 6
if ( (buttonState6 != previousButtonState6) && (millis() - lastMillis > waitTime) ) {
if (buttonState6 == LOW) {
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press(KEY_LEFT_ARROW);
lastMillis = millis();
}
else {
Keyboard.release(KEY_LEFT_SHIFT);
Keyboard.release(KEY_LEFT_ARROW);
lastMillis = millis();
}
}
//Button 7
if ( (buttonState7 != previousButtonState7) && (millis() - lastMillis > waitTime) ) {
if (buttonState7 == LOW) {
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press(KEY_RIGHT_ARROW);
lastMillis = millis();
}
else {
Keyboard.release(KEY_LEFT_SHIFT);
Keyboard.release(KEY_RIGHT_ARROW);
lastMillis = millis();
}
}
//Button 8
if ( (buttonState8 != previousButtonState8) && (millis() - lastMillis > waitTime) ) {
if (buttonState8 == LOW) {
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press(KEY_UP_ARROW);
lastMillis = millis();
}
else {
Keyboard.release(KEY_LEFT_SHIFT);
Keyboard.release(KEY_UP_ARROW);
lastMillis = millis();
}
}
//Button 9
if ( (buttonState9 != previousButtonState9) && (millis() - lastMillis > waitTime) ) {
if (buttonState9 == LOW) {
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press(KEY_DOWN_ARROW);
lastMillis = millis();
}
else {
Keyboard.release(KEY_LEFT_SHIFT);
Keyboard.release(KEY_DOWN_ARROW);
lastMillis = millis();
}
}
//Button 10
if ( (buttonState10 != previousButtonState10) && (millis() - lastMillis > waitTime) ) {
if (buttonState10 == LOW) { // Taste wurde gedrückt
Keyboard.press(KEY_PAGE_UP);
lastMillis = millis();
}
else { // Taste wurde losgelassen
Keyboard.release(KEY_PAGE_UP);
lastMillis = millis();
}
}
//Button 11
if ( (buttonState11 != previousButtonState11) && (millis() - lastMillis > waitTime) ) {
if (buttonState11 == LOW) { // Taste wurde gedrückt
Keyboard.press(KEY_PAGE_DOWN);
lastMillis = millis();
}
else { // Taste wurde losgelassen
Keyboard.release(KEY_PAGE_DOWN);
lastMillis = millis();
}
}
//Button 12
if ( (buttonState12 != previousButtonState12) && (millis() - lastMillis > waitTime) ) {
if (buttonState12 == LOW) {
Keyboard.press(KEY_F4);
lastMillis = millis();
}
else {
Keyboard.release(KEY_F4);
lastMillis = millis();
}
}
previousButtonState2 = buttonState2;
previousButtonState3 = buttonState3;
previousButtonState4 = buttonState4;
previousButtonState5 = buttonState5;
previousButtonState6 = buttonState6;
previousButtonState7 = buttonState7;
previousButtonState8 = buttonState8;
previousButtonState9 = buttonState9;
previousButtonState10 = buttonState10;
previousButtonState11 = buttonState11;
previousButtonState12 = buttonState12;
}
Hi
Pinout Leonardo (rus)
Pin14 sollte A1 sein - da bis A5 vorhanden sind, sollte bis dahin auch auswählbar sein.
MfG
Edit: Korrigiert, kam aus dem von mir gefundendem Pinout aber nicht wirklich rüber
Beim Leonardo ist das etwas anders (als zum Beispiel beim UNO):
D18: A0
D19: A1
D20: A2
D21: A3
D22: A4
D23: A5
Und der Vollständigkeit halber:
D14: MISO (ICSP-Stecker)
D15: SCK (ICSP-Stecker
D16: MOSI (ICSP-Stecker
D17: RX_LED (SS) (nicht herausgeführt)
Du kannst also auch die "analogen Pins" verwenden (18 - 23).
Und am ICSP-Stecker kannst du 14, 15 und 16 "ansprechen".
Und je nachdem, ob du die "zweite" serielle Schnittstelle (Serial1) des Leonardo brauchst, kannst du auch D0 und D1 als Inputs oder Outputs verwenden und kannst ihn trotzdem über USB programmieren oder eine serielle Verbindung mit dem PC aufbauen.
postmaster-ino:
Edit: Korrigiert, kam aus dem von mir gefundendem Pinout aber nicht wirklich rüber
Stimmt, da lässt sich das gar nicht erkennen.
Es ist zum Beispiel hier "vermerkt" - recht versteckt: ![]()
... arduino\hardware\arduino\avr\variants\leonardo\pins_arduino.h
// Mapping of analog pins as digital I/O
#define PIN_A0 (18)
#define PIN_A1 (19)
#define PIN_A2 (20)
#define PIN_A3 (21)
#define PIN_A4 (22)
#define PIN_A5 (23)
und
static const uint8_t A0 = PIN_A0;
static const uint8_t A1 = PIN_A1;
static const uint8_t A2 = PIN_A2;
static const uint8_t A3 = PIN_A3;
static const uint8_t A4 = PIN_A4;
static const uint8_t A5 = PIN_A5;
Hannes1:
Kann man vom Arduino Leonardo noch mehr Pins als Input verwenden, außer die Pins 2 - 10 und 14 - 16?
Falls du noch mehr Tasten (Inputs) brauchst als in #32 "angeboten", gibt es mehrere Möglichkeiten, zum Beispiel:
- Tastatur Matrix
- Shift-Register (Schieberegister): 74HC165
- Portexpander, mit I2C oder SPI Interface. Sie belegen 2 bzw. 4 Pins, es gibt sie mit 8 oder 16 Pins.