Arduino newbie - coding buttons as keyboard.press input


hi all

i'm trying code keyboard.press commands can create simple prototype gaming controller uni project. using leonardo recommended folk! managed modify bit of example code 1 button working w key used move character forward i'm having trouble getting code working additional buttons thereafter.

when tried add second button (s key) second button doesn't , first button operates w , s keys ws printed in textedit when first button press (i'm using mac)

this current code

// use option osx:
char ctrlkey = key_left_gui;
// use option windows , linux:
//  char ctrlkey = key_left_ctrl; 

void setup() {
  // make pin 2 input , turn on
  // pullup resistor goes high unless
  // connected ground:
  pinmode(2, input_pullup);
  pinmode(3, input_pullup);
  while (digitalread(2) == low);
  while (digitalread(3) == low);
  // initialize control on keyboard:
  keyboard.begin();
 
}

void loop() {
  while (digitalread(2) == low) {
    // nothing until pin 2 goes low
    delay(500);
  }
  {
  keyboard.press('w');
  delay(100);
  keyboard.release('w');
  delay(10); }
  {
  while (digitalread(3) == low)
  delay(500);
  }
  {
  keyboard.press('s');
  delay(100);
  keyboard.release('s');
  delay(10); }
}

i'm looking tell me i'm going wrong can program second button. can second button working , find out i'm going wrong can program remaining 8 buttons need.

any appreciated

many thanks

mitch

i use of keyboard.press() , keyboard.release(). gives far more control keyboard.write().

i see 4 "while" loops. why ? , many delay().

could explain trying ? there special reason wait buttons in setup() ?
are buttons active when low ? buttons bounce ? want keep emulated keyboard button down long real button active ?

i think can remove "while" , "delay". detect change in button (by remembering previous state) , send keyboard.press() or keyboard.release() accordingly.



Arduino Forum > Using Arduino > Project Guidance > Arduino newbie - coding buttons as keyboard.press input


arduino

Comments