I'm trying to use a keypad to generate midi program change messages. I'm using a variable X to store the number to be transmitted. At the moment, I'm just printing the number on the serial monitor.
pressing A then a number produces 1-10
pressing B then a number produces 11-20
pressing C then a number produces 21-30
pressing D then a number produces 31-40
This works fine. But I want to use the * and # key to increment or decrement the number to be sent and this isn't working.
Any suggestions or alternative methods greatly appreciated.
// read kepad & send a variety of midi messages
#include <Keypad.h>
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three columns
char keys[ROWS][COLS] = { // Define the Keymap
{'1','2','3', 'A'},
{'4','5','6', 'B'},
{'7','8','9', 'C'},
{'*','0','#', 'D'}
};
byte rowPins[ROWS] = { 9, 8, 7, 6 }; // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = { 5, 4, 3, 2 }; // Connect keypad COL0, COL1 and COL2 to these Arduino pins.
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); // Create the Keypad
char modes[6] = "ABCD*#"; // alpha keys to watch for
char mode = "";
int alpha = ""; // has alpha key been pressed
void setup()
{
Serial.begin(9600); // debugging
Serial.flush();
}
void loop()
{
char key = kpd.getKey();
alpha = 0;
if(key) // key pressed
{
int x= (key - '0'); // convert to suitable integer
for (int m=0; m <= (5); m++) // run through checking match in alphabetic array
{
if (key == modes[m])
{
mode = key;
alpha = 1;
}
}
if (mode == '*') // decrement x by 1
{
--x;
Serial.print(" * ");
Serial.println(x );
}
if (mode == '#') // increment x by 1
{
++x;
Serial.print("#");
Serial.println(x );
}
if (alpha <1) // ie it must be numeric
{
if (mode == 'A')
{
if (x == 0)
{x = 10;} // make 0 into 10
}
if (mode == 'B')
{
x = x+10;
if (x == 10)
{x = 20;}
}
if (mode == 'C')
{
x = x+20;
if (x == 20)
{x = 30;}
}
if (mode == 'D')
{
x = x+40;
if (x == 30)
{x = 40;}
}
Serial.print(mode);
Serial.println(x );
} // end numeric entry
} // end key pressed
} // end of void loop