Pages: [1]   Go Down
Author Topic: Brauche dringend Hilfe bei dem Befehl Keyboard.write oder .print  (Read 757 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hallo zusammen,

ich baue gerade eine Tastatur mit farbigen Tastern. Wenn ich einen Taster drücke, soll ein bestimmter Buchstabe "getippt" werden. Leider kommt immer gleich eine ganze Menge davon und nicht nur einer, egal ob ich .print oder .write nehme.
Ich hoffe mir kann jemand helfen.

Danke im voraus!!!
 
Code:
/*
  KeyboardAndMouseControl
 
 Controls the mouse from five pushbuttons on an Arduino Leonardo or Micro.

 Hardware:
 * 5 pushbuttons attached to D2, D3, D4, D5, D6
 
 
 The mouse movement is always relative. This sketch reads
 four pushbuttons, and uses them to set the movement of the mouse.
 
 WARNING:  When you use the Mouse.move() command, the Arduino takes
 over your mouse!  Make sure you have control before you use the mouse commands.
 
 created 15 Mar 2012
 modified 27 Mar 2012
 by Tom Igoe
 
 this code is in the public domain
 
 */

// set pin numbers for the five buttons:

// set pin numbers for the five buttons:
const int upButton = 2;    
const int downButton = 3;        
const int leftButton = 4;
const int rightButton = 5;
const int mouseButton = 6;

void setup() { // initialize the buttons' inputs:
  pinMode(upButton, INPUT);      
  pinMode(downButton, INPUT);      
  pinMode(leftButton, INPUT);      
  pinMode(rightButton, INPUT);      
  pinMode(mouseButton, INPUT);
  
 
  Keyboard.begin();
}

void loop() {
  
  // use the pushbuttons to control the keyboard:
  if (digitalRead(upButton) == HIGH) {
    Keyboard.write('u');
  }
  if (digitalRead(downButton) == HIGH) {
    Keyboard.write('d');
  }
  if (digitalRead(leftButton) == HIGH) {
    Keyboard.print("A");
  }
  if (digitalRead(rightButton) == HIGH) {
    Keyboard.write('r');
  }
  if (digitalRead(mouseButton) == HIGH) {
    Keyboard.write('m');
  }
  }
« Last Edit: February 08, 2013, 02:57:51 pm by uwefed » Logged

Germany
Offline Offline
Edison Member
*
Karma: 48
Posts: 2342
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hast du externe Pull-Down-Widerstände verwendet? Wie gedenkst du die Tasten zu entprellen?
Logged

Mein Arduino-Blog: http://www.sth77.de/ - letzte Einträge: Teensy 3.0 - Teensyduino unter Window 7 - Teensyduino unter Windows 8

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

wenn du dem link folgst, so habe ich es ausgebaut.

wäre voll nett, wenn du mir helfen könntest
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hier der link

http://arduino.cc/en/Tutorial/KeybaordAndMouseControl
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 112
Posts: 5281
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Dein jetziges

Code:
  if (digitalRead(mouseButton) == HIGH) {
    Keyboard.write('m');
  }

liest den aktuellen Status der Taste und schickt 'm', wenn sie gedrückt ist. Solange Du also die Taste drückst, wird durchgehend dieser Buchstabe an den PC geschickt. Wenn Du das nicht willst, musst Du den Status abspeichern und vergleichen.

Code:
byte stateMouseButton = LOW;
void loop() {
  byte currentMouseButton = digitalRead(mouseButton);
  if (currentMouseButton == HIGH && stateMouseButton != currentMouseButton) {
    Keyboard.write('m');
    stateMouseButton = currentMouseButton;
  }
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

danke schon mal.

wie muss ich es mit deinem code machen, da ich ja mehrere Tasten habe.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

die Zeile, die mit if beginnt verursacht eine Fahlermeldung.

stateleftbutton not declared
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 112
Posts: 5281
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In meinem Code-Block gibt's kein stateleftbutton. Ist das von Dir? Copy/Paste-Fehler?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

war ein fehler beim kopieren.

jetzt sieht es so aus, aber jetzt macht er nur einmal ein m dann kein m mehr

Code:
// set pin numbers for the five buttons:

// set pin numbers for the five buttons:
const int upButton = 2;    
const int downButton = 3;        
const int leftButton = 4;
const int rightButton = 5;
const int mouseButton = 6;

void setup() { // initialize the buttons' inputs:
  pinMode(upButton, INPUT);      
  pinMode(downButton, INPUT);      
  pinMode(leftButton, INPUT);      
  pinMode(rightButton, INPUT);      
  pinMode(mouseButton, INPUT);
  
  Keyboard.begin();
}
byte stateleftButton = LOW;
void loop() {
  
  // use the pushbuttons to control the keyboard:
  if (digitalRead(upButton) == HIGH) {
    Keyboard.write('u');
  }  

  byte currentleftButton = digitalRead(leftButton);
  if (currentleftButton == HIGH && stateleftButton != currentleftButton) {
    Keyboard.write('m');
    stateleftButton = currentleftButton;
  }
    
  if (digitalRead(downButton) == HIGH) {
    Keyboard.write('d');
  }
  //if (digitalRead(leftButton) == HIGH) {
    //Keyboard.print("A");
  //}
  if (digitalRead(rightButton) == HIGH) {
    Keyboard.write('r');
  }
  if (digitalRead(mouseButton) == HIGH) {
    Keyboard.write('m');
  }
  }
« Last Edit: February 08, 2013, 04:31:05 pm by uwefed » Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 112
Posts: 5281
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ja, richtig, Fehler von mir. entschuldige. Die Zuweisung muss aus dem if-Block raus.

Code:
  byte currentleftButton = digitalRead(leftButton);
  if (currentleftButton == HIGH && stateleftButton != currentleftButton) {
    Keyboard.write('m');
  }
  stateleftButton = currentleftButton;
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

99 Prozent geschafft. Danke schon mal.

Problem: er schreibt den Buchstaben immer zweimal

Code:
// set pin numbers for the five buttons:
const int upButton = 2;    
const int downButton = 3;        
const int leftButton = 4;
const int rightButton = 5;
const int mouseButton = 6;

void setup() { // initialize the buttons' inputs:
  pinMode(upButton, INPUT);      
  pinMode(downButton, INPUT);      
  pinMode(leftButton, INPUT);      
  pinMode(rightButton, INPUT);      
  pinMode(mouseButton, INPUT);
  
 
  Keyboard.begin();
}
byte stateleftButton = LOW;
void loop() {
  
  // use the pushbuttons to control the keyboard:
  if (digitalRead(upButton) == HIGH) {
    Keyboard.write('u');
  }
  
  

  byte currentleftButton = digitalRead(leftButton);
  if (currentleftButton == HIGH && stateleftButton != currentleftButton) {
    Keyboard.write('d');
  }
    stateleftButton = currentleftButton;
  
    
  if (digitalRead(downButton) == HIGH) {
    Keyboard.write('d');
  }
  //if (digitalRead(leftButton) == HIGH) {
    //Keyboard.print("A");
  //}
  if (digitalRead(rightButton) == HIGH) {
    Keyboard.write('r');
  }
  if (digitalRead(mouseButton) == HIGH) {
    Keyboard.write('m');
  }
  }
« Last Edit: February 08, 2013, 04:31:29 pm by uwefed » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hab noch ein Problem von meiner Seite her gefunden:

eine Taste muss bei einem Test gehalten werden und die andere Taste muss Enter simulieren

Hast du eine Idee, wie man das noch hinbringt?

hier mal mein aktueller kompletter code:

Code:
// set pin numbers for the five buttons:
const int fuenfButton = 1;    
const int vierButton = 2;        
const int dreiButton = 3;
const int zweiButton = 4;
const int einsButton = 5;
const int gruenButton = 6;
const int gelbButton = 7;
const int rotButton = 8;
const int blauButton = 9;
const int weissButton = 10;
const int spaceButton = 11;
const int highButton = 12;
const int downButton = 13;

void setup() { // initialize the buttons' inputs:
  pinMode(einsButton, INPUT);      
  pinMode(zweiButton, INPUT);      
  pinMode(dreiButton, INPUT);      
  pinMode(vierButton, INPUT);      
  pinMode(fuenfButton, INPUT);
    pinMode(gruenButton, INPUT);
      pinMode(blauButton, INPUT);
        pinMode(gelbButton, INPUT);
          pinMode(rotButton, INPUT);
            pinMode(weissButton, INPUT);
              pinMode(spaceButton, INPUT);
                pinMode(highButton, INPUT);
                  pinMode(downButton, INPUT);
                  
  
 
  Keyboard.begin();
}
byte stateeinsButton = LOW;
byte statezweiButton = LOW;
byte statedreiButton = LOW;
byte statevierButton = LOW;
byte statefuenfButton = LOW;
byte stategelbButton = LOW;
byte stateblauButton = LOW;
byte stategruenButton = LOW;
byte staterotButton = LOW;
byte stateweissButton = LOW;
byte statespaceButton = LOW;
byte statehighButton = LOW;
byte statedownButton = LOW;




void loop() {
  
  // use the pushbuttons to control the keyboard:
  
  

  byte currentgruenButton = digitalRead(gruenButton);
  if (currentgruenButton == HIGH && stategruenButton != currentgruenButton) {
    Keyboard.write('g');
  }
    stategruenButton = currentgruenButton;
  
  
  byte currentgelbButton = digitalRead(gelbButton);
  if (currentgelbButton == HIGH && stategelbButton != currentgelbButton) {
    Keyboard.write('z');
  }
    stategelbButton = currentgelbButton;
    
  byte currentrotButton = digitalRead(rotButton);
  if (currentrotButton == HIGH && staterotButton != currentrotButton) {
    Keyboard.write('z');
  }
    staterotButton = currentrotButton;
  byte currentweissButton = digitalRead(weissButton);
  if (currentweissButton == HIGH && stateweissButton != currentweissButton) {
    Keyboard.write('z');
  }
    stateweissButton = currentweissButton;
  byte currentblauButton = digitalRead(blauButton);
  if (currentblauButton == HIGH && stateblauButton != currentblauButton) {
    Keyboard.write('z');
  }
    stateblauButton = currentblauButton;
  byte currentspaceButton = digitalRead(spaceButton);
  if (currentspaceButton == HIGH && statespaceButton != currentspaceButton) {
    Keyboard.write('z');
  }
    statespaceButton = currentspaceButton;
  byte currenteinsButton = digitalRead(einsButton);
  if (currenteinsButton == HIGH && stateeinsButton != currenteinsButton) {
    Keyboard.write('z');
  }
    stateeinsButton = currenteinsButton;
  byte currentzweiButton = digitalRead(zweiButton);
  if (currentzweiButton == HIGH && statezweiButton != currentzweiButton) {
    Keyboard.write('z');
  }
    statezweiButton = currentzweiButton;
  byte currentdreiButton = digitalRead(dreiButton);
  if (currentdreiButton == HIGH && statedreiButton != currentdreiButton) {
    Keyboard.write('z');
  }
    statedreiButton = currentdreiButton;
  byte currentvierButton = digitalRead(vierButton);
  if (currentvierButton == HIGH && statevierButton != currentvierButton) {
    Keyboard.write('z');
  }
    statevierButton = currentvierButton;
  byte currentfuenfButton = digitalRead(fuenfButton);
  if (currentfuenfButton == HIGH && statefuenfButton != currentfuenfButton) {
    Keyboard.write('z');
  }
    statefuenfButton = currentfuenfButton;
  byte currenthighButton = digitalRead(highButton);
  if (currenthighButton == HIGH && statehighButton != currenthighButton) {
    Keyboard.write('z');
  }
    statehighButton = currenthighButton;
  byte currentdownButton = digitalRead(downButton);
  if (currentdownButton == HIGH && statedownButton != currentdownButton) {
    Keyboard.write('z');
  }
    statedownButton = currentdownButton;  
  }
« Last Edit: February 08, 2013, 04:31:59 pm by uwefed » Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 112
Posts: 5281
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Bei so vielen Tasten würde ich das in Arrays verpacken, schon rein der Übersichtlichkeit wegen.

Ich zeige es Dir mal für 3 Tasten, für Deine 12 kannst Du es dann selbst erweitern:

Code:
byte[] pins = { 1, 2, 3 };
byte[] states = { LOW, LOW, LOW };
byte[] keys = { 'm', 'b', 'k' };

void setup() {
  for (byte i = 0; i < 3; i++) {
    pinMode(pins[i], INPUT);
  }
  Keyboard.begin();
}

void loop() {
  for (byte i = 0; i < 3; i++) {
    byte current = digitalRead(pins[i]);
    if (current == HIGH && states[i] != current) {
      Keyboard.write(keys[i]);
    }
    states[i] = current;
  }
}

Die doppelte Eingabe könnte von der fehlenden Entprellung her rühren.
Logged

Pages: [1]   Go Up
Jump to: