Go Down

Topic: Brauche dringend Hilfe bei dem Befehl Keyboard.write oder .print (Read 796 times) previous topic - next topic

schiltkamp

Feb 08, 2013, 07:24 pm Last Edit: Feb 08, 2013, 08:57 pm by uwefed Reason: 1
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: [Select]

/*
 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');
 }
 }

sth77

Hast du externe Pull-Down-Widerstände verwendet? Wie gedenkst du die Tasten zu entprellen?
Mein Arduino-Blog: http://www.sth77.de/ - letzte Einträge: Teensy 3.0 - Teensyduino unter Window 7 - Teensyduino unter Windows 8

schiltkamp

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

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

schiltkamp

hier der link

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

pylon

Dein jetziges

Code: [Select]
  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: [Select]
byte stateMouseButton = LOW;
void loop() {
  byte currentMouseButton = digitalRead(mouseButton);
  if (currentMouseButton == HIGH && stateMouseButton != currentMouseButton) {
    Keyboard.write('m');
    stateMouseButton = currentMouseButton;
  }
}

schiltkamp

danke schon mal.

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

schiltkamp

die Zeile, die mit if beginnt verursacht eine Fahlermeldung.

stateleftbutton not declared

pylon

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

schiltkamp

#8
Feb 08, 2013, 09:06 pm Last Edit: Feb 08, 2013, 10:31 pm by uwefed Reason: 1
war ein fehler beim kopieren.

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

Code: [Select]
// 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');
 }
 }

pylon

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

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

schiltkamp

#10
Feb 08, 2013, 09:26 pm Last Edit: Feb 08, 2013, 10:31 pm by uwefed Reason: 1
99 Prozent geschafft. Danke schon mal.

Problem: er schreibt den Buchstaben immer zweimal

Code: [Select]
// 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');
 }
 }

schiltkamp

#11
Feb 08, 2013, 10:05 pm Last Edit: Feb 08, 2013, 10:31 pm by uwefed Reason: 1
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: [Select]
// 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;  
 }

pylon

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: [Select]

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.

Go Up