Keyboard software question

Status
Not open for further replies.

bavman

New member
I made a small piece of code in Arduino to test out my 3x3 keyboard pad (below). Right now I have it programmed to returned 1-9 depending on the key pressed.

What I would like it to do though is to have different keys for different programs. For example in a game, the 9 keys could correspond to 1-9, while in a different program it could be directional arrows and some other keys.
I would like to be able to controller this on an software level, so the teensy doesn't need to be flashed every time. I'm not sure how to proceed with that though. Any thoughts?

Thanks

Code:
#include <Bounce.h>

Bounce button0 = Bounce(0, 10);
Bounce button1 = Bounce(1, 10);
Bounce button2 = Bounce(2, 10);
Bounce button3 = Bounce(3, 10);
Bounce button4 = Bounce(6, 10);
Bounce button5 = Bounce(4, 10);
Bounce button6 = Bounce(13, 10);
Bounce button7 = Bounce(14, 10);
Bounce button8 = Bounce(15, 10);

void setup() 
{
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);  
  pinMode(13, INPUT_PULLUP);
  pinMode(14, INPUT_PULLUP);
  pinMode(15, INPUT_PULLUP);  
}

void loop()
{
  button0.update(); 
  button1.update(); 
  button2.update(); 
  button3.update(); 
  button4.update(); 
  button5.update(); 
  button6.update(); 
  button7.update(); 
  button8.update(); 
  
  if (button0.fallingEdge()){
    Keyboard.print("1");
  }
  if (button1.fallingEdge()){
    Keyboard.print("2");
  }
  if (button2.fallingEdge()){
    Keyboard.print("3");
  }
  if (button3.fallingEdge()){
    Keyboard.print("4");
  }
  if (button4.fallingEdge()){
    Keyboard.print("5");
  }
  if (button5.fallingEdge()){
    Keyboard.print("6");
  }
  if (button6.fallingEdge()){
    Keyboard.print("7");
  }
  if (button7.fallingEdge()){
    Keyboard.print("8");
  }
  if (button8.fallingEdge()){
    Keyboard.print("9");
  }
}
 
I would like to be able to controller this on an software level, so the teensy doesn't need to be flashed every time. I'm not sure how to proceed with that though. Any thoughts?

Maybe set Tools > USB Type to "Serial + Keyboard + Mouse + Joystick", and in your loop() function, use Serial.available() to check if any data has arrived. If so, read it with Serial.read(), and use the contents to configure your program to work differently?

At the very simplest, you could store the single byte from Serial.read() into a variable. Of course, initialize the variable where it's created or in setup() to some default value, in case no instruction ever arrives on Serial. Then inside of each button*.fallingEdge() check, add a check for the contents of that variable, where you send different stuff depending on whatever was received from Serial.

You could use the EEPROM library to store the variable in non-volatile memory, and restore it in setup(). You could also implement a more sophisticated communication protocol than receiving a single byte. But it's usually best to start simple and get something at least working, then expand from there.
 
Status
Not open for further replies.
Back
Top