ButtonsBox Code
ButtonsBox Code
**********/
/*
*This code is for creating a button box with 36 buttons with 3 rotaries and 0 axis
*This code is for Arduino pro micro
*The table below is showing how the buttons are connected to the board (pin)
*A3, A2, A1, A0, 15 and 14 Are the Row Pins for the Keypad 16 and 10-6 are the
Column Pins
*the buttons are connected to each others according to this table
* : A3 A2 A1 A1 15 14
* ....................................................
* 16 : 0 1 2 3 4 5 I
* 10 : 6 7 8 9 10 11
* 9 : 12 13 14 15 16 17
* 8 : 18 19 20 21 22 23
* 7 : 24 25 26 27 28 29
* 6 : 30 31 32 33 34 35
//Including libraries and define variables and give a constant value name
// Creating a 2D array that contains the buttons and also symboling them
byte buttons[numOfRows][numOfColumns] = {
{0,1,2,3,4,5},
{6,7,8,9,10,11},
{12,13,14,15,16,17},
{18,19,20,21,22,23},
{24,25,26,27,28,29},
{30,31,32,33,34,35},
};
};
rotariesStruct rotariesEncoders[numOfRotaries] {
//this will create 3 rotaries
{0,1,36,37,0}, //pin 0 and 1 are connected to button 36, 37 (rotary)
{2,3,38,39,0},
{4,5,40,41,0},
};
//rotary table
#define DIR_CCW 0x10 //DIR = Directory
#define DIR_CW 0x20 //ccw = counter clockwise CW = clockwise
#define R_START 0x0 //R_start = rotary start
#ifdef HALF_STEP
//use half step
#define R_CCW_BEGIN 0x1
#define R_CW_BEGIN 0x2
#define R_START_M 0x3
#define R_CW_BEGIN_M 0x4
#define R_CCW_BEGIN_M 0x5
const unsigned char rotaryTable[6][4] = { //[6][4] mean 6 rows and 4 columns
// R_START (00)
{R_START_M, R_CW_BEGIN, R_CCW_BEGIN, R_START},
// R_CCW_BEGIN
{R_START_M | DIR_CCW, R_START, R_CCW_BEGIN, R_START},
// R_CW_BEGIN
{R_START_M | DIR_CW, R_CW_BEGIN, R_START, R_START},
// R_START_M (11)
{R_START_M, R_CCW_BEGIN_M, R_CW_BEGIN_M, R_START},
// R_CW_BEGIN_M
{R_START_M, R_START_M, R_CW_BEGIN_M, R_START | DIR_CW},
// R_CCW_BEGIN_M
{R_START_M, R_CCW_BEGIN_M, R_START_M, R_START | DIR_CCW},
};
#else
//use full step
#define R_CW_FINAL 0x1
#define R_CW_BEGIN 0x2
#define R_CW_NEXT 0x3
#define R_CCW_BEGIN 0x4
#define R_CCW_FINAL 0x5
#define R_CCW_NEXT 0x6
void setup() { //setup() is a method called when the program start, and create
the variabes and
//pin mode......
Joystick.begin();
initializeRotaryEncoders();
}
}
//CheckingButtons() function will check the state of the buttons
void CheckingButtons() {
if (ButtonBox.getKeys()){
for (int i=0; i<LIST_MAX; i++) { //LIST_MAX is a variabe created in
keypad.ccp and keypad.h
if ( ButtonBox.key[i].stateChanged ) { //this mean if the button has
been pressed/hold or released
switch (ButtonBox.key[i].kstate) {
case PRESSED: //if the button is pressed and/or hold
case HOLD:
Joystick.setButton(ButtonBox.key[i].kchar, 1);
//value will be set to 1 = ON in binary
break;
}
}
}
}
}